I am working on an application where I use Naudio to play sound streamed to/from a modem (no luck with tapi on this particular modem, so AT-commands).
This works acceptably well when I used a linear waveformat (WaveFormat waveFormat = new WaveFormat(8000, 16, 1);) but now I want to try getting full duplex, which means that using a slightly more efficient sound format is probably a good idea.
Saw that Naudio has an ImaAdpcmWaveFormat, so tried to replace the WaveFormat object with that one (ImaAdpcmWaveFormat imaadpcm_waveformat = new ImaAdpcmWaveFormat(8000, 1, 4);).
However, now the waveout.init throws a System.DivideByZeroException :(.
I had a similar problem when I had managed to misread the specification for WaveFormat and put 2 instead of 16 for "bits", but 4 bits/sample should be an acceptable rate for IMA ADPCM? Anyhow, same exception for 8 or 16 bits/sample...
Exception data:
System.DivideByZeroException HResult=0x80020012 Message=Attempted to divide by zer0. Source=NAudio StackTrace:
at NAudio.Wave.WaveFormat.ConvertLatencyToByteSize(Int32 milliseconds)
at NAudio.Wave.WaveOut.Init(IWaveProvider waveProvider)
at USRModemTest.Form1..ctor() in C:\Users\c\Source\Repos\USRModemTest\USRModemTest\Form1.cs:line 56
at USRModemTest.Program.Main() in C:\Users\c\Source\Repos\USRModemTest\USRModemTest\Program.cs:line 19
From the constructor:
Note that this works acceptably well if I replace imaadpcm_waveformat with waveFormat
//waveFormat = new WaveFormat(8000, 16, 1);
imaadpcm_waveformat = new ImaAdpcmWaveFormat(8000, 1, 4);
//bwp_ModemToSpeakers = new BufferedWaveProvider(waveFormat);
bwp_ModemToSpeakers = new BufferedWaveProvider(imaadpcm_waveformat);
bwp_ModemToSpeakers.DiscardOnBufferOverflow = true;
//bwp_ModemToSpeakers.BufferDuration = System.TimeSpan.FromMilliseconds(200);
bwp_ModemToSpeakers.ReadFully = true;
bwp_ModemToSpeakers.BufferLength = 1000 ; // as small as possible it seems
waveout = new WaveOut();
waveout.DeviceNumber = -1; // default communications device
waveout.DesiredLatency = 200;
waveout.Init(bwp_ModemToSpeakers);
You cannot play audio encoded with IMA ADPCM directly. I recommend you decode the audio into PCM before placing it into BufferedWaveProvider
. Examples of this can be found in the NAudio demo project and you can make use of the AcmStream
class for the conversion.
User contributions licensed under CC BY-SA 3.0