2011-10-14 32 views
7

Tôi đang sử dụng Speex để mã hóa dữ liệu thô nhưng sau khi tôi giải mã dữ liệu, âm thanh phát ở tốc độ nhanh hơn vì nó khiến bạn phát ra âm thanh giống như chipmunk. Tôi đang sử dụng NSpeex và Silverlight 4.Silverlight Speex phát ở tốc độ nhanh

8kHz Sampling

Encoding Chức năng:

JSpeexEnc encoder = new JSpeexEnc(); 
    int rawDataSize = 0; 
    public byte[] EncodeAudio(byte[] rawData) 
    { 
     var encoder = new SpeexEncoder(BandMode.Narrow); 
     var inDataSize = rawData.Length/2; 
     var inData = new short[inDataSize]; 

     for (var index = 0; index < rawData.Length; index += 2) 
     { 
      inData[index/2] = BitConverter.ToInt16(rawData, index); 
     } 
     inDataSize = inDataSize - inDataSize % encoder.FrameSize; 

     var encodedData = new byte[rawData.Length]; 
     var encodedBytes = encoder.Encode(inData, 0, inDataSize, encodedData, 0, encodedData.Length); 

     byte[] encodedAudioData = null; 
     if (encodedBytes != 0) 
     { 
      encodedAudioData = new byte[encodedBytes]; 
      Array.Copy(encodedData, 0, encodedAudioData, 0, encodedBytes); 
     } 
     rawDataSize = inDataSize; // Count of encoded shorts, for debugging 
     return encodedAudioData; 
    } 

Giải mã Chức năng:

SpeexDecoder decoder = new SpeexDecoder(BandMode.Narrow); 
    public byte[] Decode(byte[] encodedData) 
    { 
     try 
     { 
      short[] decodedFrame = new short[8000]; // should be the same number of samples as on the capturing side 
      int decoderBytes = decoder.Decode(encodedData, 0, encodedData.Length, decodedFrame, 0, false); 

      byte[] decodedData = new byte[encodedData.Length]; 
      byte[] decodedAudioData = null; 

      decodedAudioData = new byte[decoderBytes * 2]; 
      for (int shortIndex = 0, byteIndex = 0; byteIndex < decoderBytes; shortIndex++) 
      { 
       BitConverter.GetBytes(decodedFrame[shortIndex + byteIndex]).CopyTo(decodedAudioData, byteIndex * 2); 
       byteIndex++; 
      } 

      // todo: do something with the decoded data 
      return decodedAudioData; 
     } 
     catch (Exception ex) 
     { 
      ShowMessageBox(ex.Message.ToString()); 
      return null; 
     } 

    } 

Chơi âm thanh:

void PlayWave(byte[] PCMBytes) 
    { 
     byte[] decodedBuffer = Decode(PCMBytes); 
     MemoryStream ms_PCM = new MemoryStream(decodedBuffer); 
     MemoryStream ms_Wave = new MemoryStream(); 

     _pcm.SavePcmToWav(ms_PCM, ms_Wave, 16, 8000, 1); 

     WaveMediaStreamSource WaveStream = new WaveMediaStreamSource(ms_Wave); 
     mediaElement1.SetSource(WaveStream); 
     mediaElement1.Play(); 
    } 
+0

Nó không phải là rõ ràng những gì câu hỏi là. Bạn có thể vui lòng chỉnh sửa câu hỏi của bạn không? – gioele

+0

Bạn có thể thêm nguồn cho phương thức 'SavePcmToWav' của mình không? Nếu tệp WAV kết quả bằng cách nào đó có tỷ lệ mẫu là 44.1kHz thay vì 8kHz, điều đó sẽ tạo ra âm thanh chipmunk (đó là dữ liệu âm thanh được phát lại quá nhanh). – MusiGenesis

Trả lời

0

Xin lỗi các bạn đã trả lời muộn nhưng tôi đã tìm ra vấn đề là gì.

Bên trong hàm giải mã của tôi Tôi lặp qua mảng được giải mã short nhưng tôi chỉ sao chép một nửa số byte vào mảng byte mới của tôi.

Nó cần phải tìm kiếm một cái gì đó như thế này:

decodedAudioData = new byte[decoderBytes * 2]; 
for (int shortIndex = 0, byteIndex = 0; shortIndex < decodedFrame.Length; shortIndex++, byteIndex += 2) 
{ 
    byte[] temp = BitConverter.GetBytes(decodedFrame[shortIndex]); 
    decodedAudioData[byteIndex] = temp[0]; 
    decodedAudioData[byteIndex + 1] = temp[1]; 
} 
Các vấn đề liên quan