This project has moved. For the latest updates, please go here.

Wave conversion from byte buffer

Jan 9, 2011 at 4:11 PM

Hi,

i want to use an acm driver to decode audio to pcm.

I have found the WaveFormatConversionStream, which can be used for this but is there any

possebility to use directly a byte buffer as source and as target for decoding?

Thanks

 

 

Coordinator
Jan 10, 2011 at 1:39 PM

make your own class inheriting from WaveStream. In its Read method, just return the next bytes from your byte array.

Mark

Jan 10, 2011 at 1:55 PM

Thanks Mark,

 

i already solved it but i want to know another thing:

 

I try to decompress truespeech to pcm but it does not work.

Here ist my code:

 

//Helper Class for the compressed stream

private class ACMCodecCompressedStream : WaveStream
    {
      private byte[] cAudioData;
      private WaveFormat SourceWaveFormat;

      public override WaveFormat WaveFormat
      {
        get
        {
          return SourceWaveFormat;
        }
      }

      public override long Length
      {
        get
        {
          return cAudioData.Length;
        }
      }

      public override long Position { get; set; }

      public ACMCodecCompressedStream(MyWaveFormat SourceWaveFormat, byte[] cSourceData)
      {
        cAudioData = cSourceData;
        this.SourceWaveFormat = SourceWaveFormat;
      }

      public override int Read(byte[] buffer, int offset, int count)
      {
        Array.Copy(cAudioData, 0, buffer, offset, cAudioData.Length);
        return cAudioData.Length;
      }
    }

// Helper class for the Truespeech-WaveFormat structure (it has 32 bytes extra)

// the Extra bytes are 0x00f00001

    [StructLayout(LayoutKind.Sequential)]
    public class MyWaveFormat : WaveFormat
    {
      protected byte[] cExtraData;

      public MyWaveFormat(WaveFormatEncoding WaveFormatTag, short BitsPerSample, short Channels, int AverageBytesPerSecond, short BlockAlign, int SampleRate, short ExtraSize, byte[] cExtraData)
      {
        base.extraSize = ExtraSize;
        base.averageBytesPerSecond = AverageBytesPerSecond;
        base.bitsPerSample = BitsPerSample;
        base.blockAlign = BlockAlign;
        base.channels = Channels;
        base.sampleRate = SampleRate;
        base.waveFormatTag = WaveFormatTag;
         if (ExtraSize > 0)
        {
          this.cExtraData = new byte[ExtraSize];
          Array.Copy(cExtraData, this.cExtraData, cExtraData.Length);
        }
      }
    }

// Decompression code...

          ACMCodecCompressedStream TrueSpeechStream = null;
          WaveFormatConversionStream OutputStream = null;

          TrueSpeechStream = new ACMCodecCompressedStream(new MyWaveFormat(WaveFormatEncoding.DspGroupTruespeech, 8000, 1, 1067, 32, 1, 32, cExtraData), cCompressedAudioBuffer);
          OutputStream = new WaveFormatConversionStream(new WaveFormat(8000,16,1),TrueSpeechStream);

When this line is called "OutputStream = new WaveFormatConversionStream(new WaveFormat(8000,16,1),TrueSpeechStream);"

i get this Exception: AcmNotPossible calling acmStreamOpen

 

The Truespeech codec is installed.

I don't know why it does not work.

Can you give me a hint?

 

Tobias

 

 

 

Coordinator
Jan 10, 2011 at 3:38 PM

AcmNotPossible means that the ACM codec reports that it can't convert the incoming WaveFormat to outgoing. You can use the NAudio demo app to query all the supported input formats of each codec. Then you will need to byte by byte compare those with the ones you have made to work out the difference.

Mark