This project has moved and is read-only. For the latest updates, please go here.

How to play a wave-file with a custom format?

May 5, 2015 at 1:42 AM
Excuse me, could you help me please?
I need to play a wave with a custom format:
AverageBytesPerSecond   60638   int
BitsPerSample   4   int
BlockAlign  44  int
Channels    2   int
Encoding    Adpcm   NAudio.Wave.WaveFormatEncoding
ExtraSize   32  int
SampleRate  44101   int
How I can do that? When I tried, I got errors:
AcmNotPossible calling acmStreamOpen
or
"Unsupported source encoding"

Code:
WaveFormat format = ReadWaveFormat();

byte[] data = ReadData();
MemoryStream ms = new MemoryStream(data, 0, data.Length, false);
WaveStream waveStream = new RawSourceWaveStream(ms, format);

SampleChannel sampleChannel = new SampleChannel(_waveStream , true); // error
MeteringSampleProvider postVolumeMeter = new MeteringSampleProvider(sampleChannel);

IWavePlayer wavePlayer = GetPlayer();
wavePlayer.Init(sampleProvider);
wavePlayer.Play();
May 7, 2015 at 11:10 PM
Hi, you could try this instead of SampleChannel:
SampleProvider = RawSourceWaveStream.ToSampleProvider();
Although, this seems to be an ACM error which indicates that the Audio Compression Manager isn´t capable of your self-created waveformat and fails converting it.

Greetz
May 11, 2015 at 9:11 PM
Freefall wrote:
Hi, you could try this instead of SampleChannel:
SampleProvider = RawSourceWaveStream.ToSampleProvider();
Unfortunately, not working.
ToSampleProvider:
public static ISampleProvider ToSampleProvider(this IWaveProvider waveProvider)
{
    return SampleProviderConverters.ConvertWaveProviderIntoSampleProvider(waveProvider);
}
SampleChannel's constuctor:
public SampleChannel(IWaveProvider waveProvider, bool forceStereo)
{
    ISampleProvider source = SampleProviderConverters.ConvertWaveProviderIntoSampleProvider(waveProvider);
    ...
Although, this seems to be an ACM error which indicates that the Audio Compression Manager isn´t capable of your self-created waveformat and fails converting it.
Yes. And when I try to initialize WaveOut without SampleChannel I get error:
_waveOut.Init(_waveProvider);
// WaveOut: WaveBadFormat calling waveOutOpen
// DirectSound: (Localized message about non primitive data)
// WasAPI: Not a supported encoding Adpcm
May 13, 2015 at 2:34 PM
You will need to use WaveFormatConversionStream.CreatePcmStream to convert to linear PCM before playing. However, you do need to make sure your WaveFormat matches the one ACM is expecting. You can use the NAudio demo application to scrutinize the details of your ACM codecs, or maybe more simply by using AdpcmWaveFormat.
Marked as answer by Albeoris on 5/13/2015 at 10:56 AM
May 13, 2015 at 6:56 PM
markheath wrote:
You will need to use WaveFormatConversionStream.CreatePcmStream to convert to linear PCM before playing. However, you do need to make sure your WaveFormat matches the one ACM is expecting. You can use the NAudio demo application to scrutinize the details of your ACM codecs, or maybe more simply by using AdpcmWaveFormat.
Yeahoo!
WaveFormatConversionStream.CreatePcmStream(waveProvider) is working! Thank you!

P.S. AdpcmWaveFormat is unusable because it contains protected fields without public accessors. And some classes trying to marshal WaveFormat bypass Serialize method. =\ Instead, I use WaveFormat FromFormatChunk. It works fine for me. (: