Quantcast
Channel: naudio Work Item Rss Feed
Viewing all 738 articles
Browse latest View live

Commented Unassigned: WaveViewer and 24bit wavs [16438]

$
0
0
Maybe a stupid question but can the WaveViewer control display 24 bit wav files ?
I am doing this:

WaveViewer1.SamplesPerPixel = 400
Dim fr = New NAudio.Wave.WaveFileReader(filename)
WaveViewer1.WaveStream = fr

If filename is a file with 16bit depth (or lower), everything works fine. If the file is above 16bit, I think that the values becomes too big and verticaly the whole control is drawn.

Any help?
Comments: Thanks you again Mark. Just let me tell you (again) that I have managed to correct the WaveViewer and make it draw 24bit wavs. Perhaps it sounds easy for you, since you design these WaveViewers at will, but it wasn't easy for me !!! But I managed to understand how those 3 bytes contain the information... Anyway, it would be nice if you included sometime those other controls that you have made. Thank you again for everything. Your stuff have helped me a lot.

Created Unassigned: NAudio.Wave.MediaFoundationEncoder.PerformEncode throws MF_E_SINK_NO_SAMPLES_PROCESSED (0xC00D4A44) [16439]

$
0
0
OS: Windows Server 2012R2
NAudio Version: 1.7.1.17

----------------------------------------- Exception -------------------------------
Exception: "An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in NAudio.dll
Additional information: Exception from HRESULT: 0xC00D4A44'

writer.DoFinalize() - line 232, MediaFoundationEncoder.cs

I was attempting to convert several wav files to mp3 files when I came across this error. Sample code that should recreate the error:

----------------------------------------- My Source -------------------------------
NAudio.MediaFoundation.MediaFoundationApi.Startup();
NAudio.Wave.WaveFileReader readerStream = new NAudio.Wave.WaveFileReader(file);
NAudio.Wave.WaveStream waveStream = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(readerStream);

NAudio.Wave.MediaFoundationEncoder.EncodeToMp3(waveStream, file + ".mp3");
NAudio.MediaFoundation.MediaFoundationApi.Shutdown();

----------------------------------------- Cause -------------------------------
The writer is performing writes on a sample basis asynchronously to the thread making the writer.WriteSample() method. Therefore, sometimes the queued bytes to write out via the writer have not been written when writer.DoFinalize() is called inside PerformEncode() causing a COMException to be thrown since apparently DoFinalize() does not wait for the writer to become flush before disposal.

----------------------------------------- Solution -------------------------------
private void PerformEncode(IMFSinkWriter writer, int streamIndex, IWaveProvider inputProvider)
{
int maxLength = inputProvider.WaveFormat.AverageBytesPerSecond * 4;
var managedBuffer = new byte[maxLength];

writer.BeginWriting();

long position = 0;
long duration = 0;
do
{
duration = ConvertOneBuffer(writer, streamIndex, inputProvider, position, managedBuffer);
position += duration;
} while (duration > 0);

//We are going to check the stats of the writer and ensure no more bytes are queued before we call DoFinalize()
MF_SINK_WRITER_STATISTICS stats = new MF_SINK_WRITER_STATISTICS();
stats.cb = Marshal.SizeOf(stats);
bool finished = false;

while (!finished)
{
writer.GetStatistics(streamIndex, stats);

if (stats.dwByteCountQueued == 0)
finished = true;
else
System.Threading.Thread.Sleep(100); //sleeping could be optional, but don't want to hog the CPU
}
//End workaround

writer.DoFinalize();
}

Edited Unassigned: NAudio.Wave.MediaFoundationEncoder.PerformEncode throws MF_E_SINK_NO_SAMPLES_PROCESSED (0xC00D4A44) [16439]

$
0
0
OS: Windows Server 2012R2
NAudio Version: 1.7.1.17

----------------------------------------- Exception -------------------------------
Exception: "An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in NAudio.dll
Additional information: Exception from HRESULT: 0xC00D4A44'

writer.DoFinalize() - line 232, MediaFoundationEncoder.cs

I was attempting to convert several wav files to mp3 files when I came across this error. Sample code that should recreate the error:

----------------------------------------- My Source -------------------------------
```
NAudio.MediaFoundation.MediaFoundationApi.Startup();
using(NAudio.Wave.WaveFileReader readerStream = new NAudio.Wave.WaveFileReader(file))
{
using(NAudio.Wave.WaveStream waveStream = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(readerStream))
{
NAudio.Wave.MediaFoundationEncoder.EncodeToMp3(waveStream, file + ".mp3");
}
}
NAudio.MediaFoundation.MediaFoundationApi.Shutdown();
```

----------------------------------------- Cause -------------------------------
The writer is performing writes on a sample basis asynchronously to the thread making the writer.WriteSample() method. Therefore, sometimes the queued bytes to write out via the writer have not been written when writer.DoFinalize() is called inside PerformEncode() causing a COMException to be thrown since apparently DoFinalize() does not wait for the writer to become flush before disposal.

----------------------------------------- Solution -------------------------------
```
private void PerformEncode(IMFSinkWriter writer, int streamIndex, IWaveProvider inputProvider)
{
int maxLength = inputProvider.WaveFormat.AverageBytesPerSecond * 4;
var managedBuffer = new byte[maxLength];

writer.BeginWriting();

long position = 0;
long duration = 0;
do
{
duration = ConvertOneBuffer(writer, streamIndex, inputProvider, position, managedBuffer);
position += duration;
} while (duration > 0);

//We are going to check the stats of the writer and ensure no more bytes are queued before we call DoFinalize()
MF_SINK_WRITER_STATISTICS stats = new MF_SINK_WRITER_STATISTICS();
stats.cb = Marshal.SizeOf(stats);
bool finished = false;

while (!finished)
{
writer.GetStatistics(streamIndex, stats);

if (stats.dwByteCountQueued == 0)
finished = true;
else
System.Threading.Thread.Sleep(100); //sleeping could be optional, but don't want to hog the CPU
}
//End workaround

writer.DoFinalize();
}
```

Commented Issue: sound does not play because of double to integer truncation, caused by low numFramesAvailable value [16363]

$
0
0
When device format is using 48k-sampling and content format is using 44.1k-sampling, playback problems occur. We observed the following sequence of events.

[WasapiOut::PlayThread]
1. audio buffer is filled at first
2. enter while loop and sleep for a while
3. try to fill buffer again, sometimes 'numFramesAvailable' is only '1'

[WasapiOut::FillBuffer]
4. when argument 'frameCount'='1' and 'playbackProvider' is SRC Dmo (i.e. device format is 48k while content is 44.1k), 'playbackState' will be stopped
5. no sound gets played, or you only hear a click

Step 4 is caused since:
[ResamplerDmoStream::Read]
int inputBytesRequired = (int)OutputToInputPosition(count - outputBytesProvided);
=>
[ResamplerDmoStream::OutputToInputPosition]
long inputPosition = (long)(outputPosition / ratio);
=>
inputPosition is 0 since double to integer truncation

Thus, the idea is only use FillBuffer when available frame count is 'bigger' so that inputPosition does not result in double-to-integer truncation value of 0.

We propose an easy fix by changing WasapiOut.cs, line 138, so that (numFramesAvailable > 10), instead of (numFramesAvailable > 0).

138: if (numFramesAvailable > 10)
139: {
140: FillBuffer(playbackProvider, numFramesAvailable);
141: }

Ten frames are safe and will not cause data loss. Of course, modifying behavior with 'OutputToInputPosition' is another way.

Comments: I have a similar issue with audio content that is played at different speeds (via SoundTouch library). The proposed work-around fixes my issue where playbackState was randomly getting set to stopped. Thank you for documenting this!

Commented Issue: sound does not play because of double to integer truncation, caused by low numFramesAvailable value [16363]

$
0
0
When device format is using 48k-sampling and content format is using 44.1k-sampling, playback problems occur. We observed the following sequence of events.

[WasapiOut::PlayThread]
1. audio buffer is filled at first
2. enter while loop and sleep for a while
3. try to fill buffer again, sometimes 'numFramesAvailable' is only '1'

[WasapiOut::FillBuffer]
4. when argument 'frameCount'='1' and 'playbackProvider' is SRC Dmo (i.e. device format is 48k while content is 44.1k), 'playbackState' will be stopped
5. no sound gets played, or you only hear a click

Step 4 is caused since:
[ResamplerDmoStream::Read]
int inputBytesRequired = (int)OutputToInputPosition(count - outputBytesProvided);
=>
[ResamplerDmoStream::OutputToInputPosition]
long inputPosition = (long)(outputPosition / ratio);
=>
inputPosition is 0 since double to integer truncation

Thus, the idea is only use FillBuffer when available frame count is 'bigger' so that inputPosition does not result in double-to-integer truncation value of 0.

We propose an easy fix by changing WasapiOut.cs, line 138, so that (numFramesAvailable > 10), instead of (numFramesAvailable > 0).

138: if (numFramesAvailable > 10)
139: {
140: FillBuffer(playbackProvider, numFramesAvailable);
141: }

Ten frames are safe and will not cause data loss. Of course, modifying behavior with 'OutputToInputPosition' is another way.

Comments: thanks, have made this fix

Closed Issue: sound does not play because of double to integer truncation, caused by low numFramesAvailable value [16363]

$
0
0
When device format is using 48k-sampling and content format is using 44.1k-sampling, playback problems occur. We observed the following sequence of events.

[WasapiOut::PlayThread]
1. audio buffer is filled at first
2. enter while loop and sleep for a while
3. try to fill buffer again, sometimes 'numFramesAvailable' is only '1'

[WasapiOut::FillBuffer]
4. when argument 'frameCount'='1' and 'playbackProvider' is SRC Dmo (i.e. device format is 48k while content is 44.1k), 'playbackState' will be stopped
5. no sound gets played, or you only hear a click

Step 4 is caused since:
[ResamplerDmoStream::Read]
int inputBytesRequired = (int)OutputToInputPosition(count - outputBytesProvided);
=>
[ResamplerDmoStream::OutputToInputPosition]
long inputPosition = (long)(outputPosition / ratio);
=>
inputPosition is 0 since double to integer truncation

Thus, the idea is only use FillBuffer when available frame count is 'bigger' so that inputPosition does not result in double-to-integer truncation value of 0.

We propose an easy fix by changing WasapiOut.cs, line 138, so that (numFramesAvailable > 10), instead of (numFramesAvailable > 0).

138: if (numFramesAvailable > 10)
139: {
140: FillBuffer(playbackProvider, numFramesAvailable);
141: }

Ten frames are safe and will not cause data loss. Of course, modifying behavior with 'OutputToInputPosition' is another way.

Commented Unassigned: NAudio.Wave.MediaFoundationEncoder.PerformEncode throws MF_E_SINK_NO_SAMPLES_PROCESSED (0xC00D4A44) [16439]

$
0
0
OS: Windows Server 2012R2
NAudio Version: 1.7.1.17

----------------------------------------- Exception -------------------------------
Exception: "An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in NAudio.dll
Additional information: Exception from HRESULT: 0xC00D4A44'

writer.DoFinalize() - line 232, MediaFoundationEncoder.cs

I was attempting to convert several wav files to mp3 files when I came across this error. Sample code that should recreate the error:

----------------------------------------- My Source -------------------------------
```
NAudio.MediaFoundation.MediaFoundationApi.Startup();
using(NAudio.Wave.WaveFileReader readerStream = new NAudio.Wave.WaveFileReader(file))
{
using(NAudio.Wave.WaveStream waveStream = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(readerStream))
{
NAudio.Wave.MediaFoundationEncoder.EncodeToMp3(waveStream, file + ".mp3");
}
}
NAudio.MediaFoundation.MediaFoundationApi.Shutdown();
```

----------------------------------------- Cause -------------------------------
The writer is performing writes on a sample basis asynchronously to the thread making the writer.WriteSample() method. Therefore, sometimes the queued bytes to write out via the writer have not been written when writer.DoFinalize() is called inside PerformEncode() causing a COMException to be thrown since apparently DoFinalize() does not wait for the writer to become flush before disposal.

----------------------------------------- Solution -------------------------------
```
private void PerformEncode(IMFSinkWriter writer, int streamIndex, IWaveProvider inputProvider)
{
int maxLength = inputProvider.WaveFormat.AverageBytesPerSecond * 4;
var managedBuffer = new byte[maxLength];

writer.BeginWriting();

long position = 0;
long duration = 0;
do
{
duration = ConvertOneBuffer(writer, streamIndex, inputProvider, position, managedBuffer);
position += duration;
} while (duration > 0);

//We are going to check the stats of the writer and ensure no more bytes are queued before we call DoFinalize()
MF_SINK_WRITER_STATISTICS stats = new MF_SINK_WRITER_STATISTICS();
stats.cb = Marshal.SizeOf(stats);
bool finished = false;

while (!finished)
{
writer.GetStatistics(streamIndex, stats);

if (stats.dwByteCountQueued == 0)
finished = true;
else
System.Threading.Thread.Sleep(100); //sleeping could be optional, but don't want to hog the CPU
}
//End workaround

writer.DoFinalize();
}
```
Comments: thanks, I'll try to get this into a future NAudio

Commented Unassigned: initiating 4 different waveout for 4 difference output device [16437]

$
0
0
hi,

i got an issue on initiating 4 difference waveout for 4 different output devices.
The initiation of the waveout failed on the 4th call. Here is the code

Dictionary<int, WaveOut> _WaveOutCollections = null;


// Init the 4 output device, and assign to a difference output device
private void InitDevice()
{

_WaveOutCollections = new Dictionary<int, WaveOut>();

for (int i = 0; i< 4;i++)
{
WaveOut waveOut = new WaveOut();
waveOut.deviceNumber = i; // Assign to a different device number
waveOut.WaveFormat = new WaveFormat(8000, 16, 1);

_WaveOutCollection.Add(i, waveOut); // add the waveOut object into a container to be use later in playing sound
}

}


// This function will be use on playing the received voice buffer from 4 different input source to 4 different output devices

private void PlaySound(byte[] buffer, int deviceNumber)
{
IWaveProvider waveProvider = new BufferedWaveProvider();
waveProvider.AddSamples(buffer, 0, buffer.Length);

WaveOut waveOut = null;
if (_WaveOutCollections.TryGetValue(deviceNumber, out WaveOut))
{
if (waveOut!=null)
{
waveOut.Init(waveProvider); // The Error happens on the 4th initiating....

if (waveOut.PlayBackState != PlayBackState.Playing)
waveOut.Play();

}
}
}



I traced the error it happen on interop call on winmm.dll at "WaveInterop.cs", The Error result is "UnSpecified Error"

[DllImport("winmm.dll")]
public static extern MmResult waveOutOpen(out IntPtr hWaveOut, IntPtr uDeviceID, WaveFormat lpFormat, WaveCallback dwCallback, IntPtr dwInstance, WaveInOutOpenFlags dwFlags);
[DllImport("winmm.dll", EntryPoint = "waveOutOpen")]
public static extern MmResult waveOutOpenWindow(out IntPtr hWaveOut, IntPtr uDeviceID, WaveFormat lpFormat, IntPtr callbackWindowHandle, IntPtr dwInstance, WaveInOutOpenFlags dwFlags);



thanks,

Comments: Yup the sound card r the issue.. Thanks mark..

Created Unassigned: need pitch , frequency , noise , amplitude [16440]

$
0
0
can anyone help me to find out my .wav file to get value of pitch , frequency , noiseness and amplitude ?



thank you.

Commented Unassigned: need pitch , frequency , noise , amplitude [16440]

$
0
0
can anyone help me to find out my .wav file to get value of pitch , frequency , noiseness and amplitude ?



thank you.
Comments: NAudio will give you access to the samples. It's up to you to use them to get amplitude and frequency data out. For the purposes of getting frequency information, NAudio provides an FFT class you can use, although you'll need to learn how FFT's work in order to make use of it

Created Unassigned: PlaybackStopped event fires at start of playback [16441]

$
0
0
I'm trying to make an application which plays files on a playlist. Whenever the user clicks "next", it will start playing the next song, and immediately move to the next one before it ends. This seems to be caused by the PlaybackStopped event.
Here is the code: (I removed some irrelevant stuff)
```
public class BackgroundPlayer : IDisposable
{
private IWavePlayer myWaveOut;
private WaveStream myAudioFileReader;
public PlaybackStatus Status { get; private set; }
private int nowPlayingID;
public int NowPlayingID
{
get { return nowPlayingID; }
private set
{
nowPlayingID = value;
if (Status == PlaybackStatus.Playing)
Play();
else
{
Unload();
Load();
}
}
}

public void Next()
{
if (NowPlayingID < Playlist.Count - 1)
++NowPlayingID;
else
{
Status = PlaybackStatus.Stopped;
NowPlayingID = 0;
}
}

public void Previous()
{
if (NowPlayingID > 0)
--NowPlayingID;
else
{
Status = PlaybackStatus.Stopped;
NowPlayingID = Playlist.Count - 1;
}
}

public void Play()
{
Unload();
Load();
myWaveOut.Play();
Status = PlaybackStatus.Playing;
}

public void Stop()
{
if (myWaveOut != null)
myWaveOut.Stop();
Status = PlaybackStatus.Stopped;
}

public void Load()
{
myWaveOut = new DirectSoundOut();
myAudioFileReader = new AudioFileReader(NowPlaying.FilePath);
myWaveOut.PlaybackStopped += myWaveOut_PlaybackStopped;
myWaveOut.Init(myAudioFileReader);
}

public void Unload()
{
if (myWaveOut != null)
myWaveOut.Stop();
if (myAudioFileReader != null)
{
myAudioFileReader.Dispose();
myAudioFileReader = null;
}
if (myWaveOut != null)
{
myWaveOut.Dispose();
myWaveOut = null;
}
Status = PlaybackStatus.Stopped;
}

private void myWaveOut_PlaybackStopped(object sender, StoppedEventArgs e)
{
Next();
}
}
```

Could the problem be that this is in a seperate thread from the Form?
Should I be using something other than DirectSoundOut?
This code had worked, until I changed something with the Play, Load, and Unload functions.

Thanks in advance for the help!

Edited Unassigned: PlaybackStopped event fires at start of playback [16441]

$
0
0
I'm trying to make an application which plays files on a playlist. Whenever the user clicks "next", it will start playing the next song, and immediately move to the next one before it ends. This seems to be caused by the PlaybackStopped event.
Here is the code: (I removed some irrelevant stuff)
```
public class BackgroundPlayer
{
private IWavePlayer myWaveOut;
private WaveStream myAudioFileReader;
public PlaybackStatus Status { get; private set; }
private int nowPlayingID;
public int NowPlayingID
{
get { return nowPlayingID; }
private set
{
nowPlayingID = value;
if (Status == PlaybackStatus.Playing)
Play();
else
{
Unload();
Load();
}
}
}

public void Next()
{
if (NowPlayingID < Playlist.Count - 1)
++NowPlayingID;
else
{
Status = PlaybackStatus.Stopped;
NowPlayingID = 0;
}
}

public void Previous()
{
if (NowPlayingID > 0)
--NowPlayingID;
else
{
Status = PlaybackStatus.Stopped;
NowPlayingID = Playlist.Count - 1;
}
}

public void Play()
{
Unload();
Load();
myWaveOut.Play();
Status = PlaybackStatus.Playing;
}

public void Stop()
{
if (myWaveOut != null)
myWaveOut.Stop();
Status = PlaybackStatus.Stopped;
}

public void Load()
{
myWaveOut = new DirectSoundOut();
myAudioFileReader = new AudioFileReader(NowPlaying.FilePath);
myWaveOut.PlaybackStopped += myWaveOut_PlaybackStopped;
myWaveOut.Init(myAudioFileReader);
}

public void Unload()
{
if (myWaveOut != null)
myWaveOut.Stop();
if (myAudioFileReader != null)
{
myAudioFileReader.Dispose();
myAudioFileReader = null;
}
if (myWaveOut != null)
{
myWaveOut.Dispose();
myWaveOut = null;
}
Status = PlaybackStatus.Stopped;
}

private void myWaveOut_PlaybackStopped(object sender, StoppedEventArgs e)
{
Next();
}
}
```

Should I be using something other than DirectSoundOut?
This code had worked, until I changed something with the Play, Load, and Unload functions.

Thanks in advance for the help!

Edited Unassigned: PlaybackStopped event fires at start of playback with NullReferenceException [UPDATED] [16441]

$
0
0
UPDATE: I found the problem. In my Play() function, when playback is started, it causes a NullReferenceException which is sent in the PlaybackStopped event. However, the file continues playing fine.

I'm trying to make an application which plays files on a playlist. Whenever the user clicks "next", it will start playing the next song, and immediately move to the next one before it ends. This seems to be caused by the PlaybackStopped event.
Here is the code: (I removed some irrelevant stuff)
```
public class BackgroundPlayer
{
private IWavePlayer myWaveOut;
private WaveStream myAudioFileReader;
public PlaybackStatus Status { get; private set; }
private int nowPlayingID;
public int NowPlayingID
{
get { return nowPlayingID; }
private set
{
nowPlayingID = value;
if (Status == PlaybackStatus.Playing)
Play();
else
{
Unload();
Load();
}
}
}

public void Next()
{
if (NowPlayingID < Playlist.Count - 1)
++NowPlayingID;
else
{
Status = PlaybackStatus.Stopped;
NowPlayingID = 0;
}
}

public void Previous()
{
if (NowPlayingID > 0)
--NowPlayingID;
else
{
Status = PlaybackStatus.Stopped;
NowPlayingID = Playlist.Count - 1;
}
}

public void Play()
{
Unload();
Load();
myWaveOut.Play();
Status = PlaybackStatus.Playing;
}

public void Stop()
{
if (myWaveOut != null)
myWaveOut.Stop();
Status = PlaybackStatus.Stopped;
}

public void Load()
{
myWaveOut = new DirectSoundOut();
myAudioFileReader = new AudioFileReader(NowPlaying.FilePath);
myWaveOut.PlaybackStopped += myWaveOut_PlaybackStopped;
myWaveOut.Init(myAudioFileReader);
}

public void Unload()
{
if (myWaveOut != null)
myWaveOut.Stop();
if (myAudioFileReader != null)
{
myAudioFileReader.Dispose();
myAudioFileReader = null;
}
if (myWaveOut != null)
{
myWaveOut.Dispose();
myWaveOut = null;
}
Status = PlaybackStatus.Stopped;
}

private void myWaveOut_PlaybackStopped(object sender, StoppedEventArgs e)
{
Next();
}
}
```

Should I be using something other than DirectSoundOut?
This code had worked, until I changed something with the Play, Load, and Unload functions.

Thanks in advance for the help!

Edited Unassigned: PlaybackStopped event fires at start of playback with NullReferenceException [UPDATED] [16441]

$
0
0
__UPDATE: I found the problem. In my Play() function, when playback is started, it causes a NullReferenceException which is sent in the PlaybackStopped event. However, the file continues playing fine.__

I'm trying to make an application which plays files on a playlist. Whenever the user clicks "next", it will start playing the next song, and immediately move to the next one before it ends. This seems to be caused by the PlaybackStopped event.
Here is the code: (I removed some irrelevant stuff)
```
public class BackgroundPlayer
{
private IWavePlayer myWaveOut;
private WaveStream myAudioFileReader;
public PlaybackStatus Status { get; private set; }
private int nowPlayingID;
public int NowPlayingID
{
get { return nowPlayingID; }
private set
{
nowPlayingID = value;
if (Status == PlaybackStatus.Playing)
Play();
else
{
Unload();
Load();
}
}
}

public void Next()
{
if (NowPlayingID < Playlist.Count - 1)
++NowPlayingID;
else
{
Status = PlaybackStatus.Stopped;
NowPlayingID = 0;
}
}

public void Previous()
{
if (NowPlayingID > 0)
--NowPlayingID;
else
{
Status = PlaybackStatus.Stopped;
NowPlayingID = Playlist.Count - 1;
}
}

public void Play()
{
Unload();
Load();
myWaveOut.Play();
Status = PlaybackStatus.Playing;
}

public void Stop()
{
if (myWaveOut != null)
myWaveOut.Stop();
Status = PlaybackStatus.Stopped;
}

public void Load()
{
myWaveOut = new DirectSoundOut();
myAudioFileReader = new AudioFileReader(NowPlaying.FilePath);
myWaveOut.PlaybackStopped += myWaveOut_PlaybackStopped;
myWaveOut.Init(myAudioFileReader);
}

public void Unload()
{
if (myWaveOut != null)
myWaveOut.Stop();
if (myAudioFileReader != null)
{
myAudioFileReader.Dispose();
myAudioFileReader = null;
}
if (myWaveOut != null)
{
myWaveOut.Dispose();
myWaveOut = null;
}
Status = PlaybackStatus.Stopped;
}

private void myWaveOut_PlaybackStopped(object sender, StoppedEventArgs e)
{
Next();
}
}
```

Should I be using something other than DirectSoundOut?
This code had worked, until I changed something with the Play, Load, and Unload functions.

Thanks in advance for the help!

Created Unassigned: UnspecifiedError calling waveOutOpen when using Channel Phantoming or Speaker Fill [16442]

$
0
0
When calling WaveOutEvent.Init() with a device that points to a speaker that has Speaker Fill or Channel Phantoming(Options in Microsoft's basic sound driver) and a sample rate that is exactly equal to the device's sample rate the Init function throws this error:
NAudio.MmException
"UnspecifiedError calling waveOutOpen"

The following crashes when my speakers are set to 16 or 24 bit 44100 quality with either Channel Phantoming or Speaker Fill enabled:
```
//waveProvideris an instance of a class that inherits WaveProvider32
waveProvider.SetWaveFormat(44100, 2);

NAudio.Wave.WaveOutEvent waveOut = new WaveOutEvent();

waveOut.Init(waveProvider);

waveOut.Play();
```
But changing just the first line to Instance.SetWaveFormat(44099, 2); or Instance.SetWaveFormat(44101, 2); and it runs fine. Or ensuring Channel Phantoming and Speaker Fill are disabled and leaving it with sample rate 44100 will also make it run fine.

The same is true if my speakers are set to 48000 sample rate and have the first line as Instance.SetWaveFormat(48000, 2);

Created Unassigned: Incorrect handling of wave riff pad byte [16443]

$
0
0
Hi,

one of our customers sent to us a file (attached) which causes NAudio to crash (telling that no riff fmt chunk was found) we inspect the file and found it, but only after other riff chunks with odd length.

The problem lies in an incorrect handling of RIFF size if the size value is odd and not even.

Taken from Wikipedia:
All chunks have the following format:
* 4 bytes: an ASCII identifier for this chunk (examples are "fmt " and "data"; note the space in "fmt ").
* 4 bytes: an unsigned, little-endian 32-bit integer with the length of this chunk (except this field itself and the chunk identifier).
* variable-sized field: the chunk data itself, of the size given in the previous field.
* __a pad byte, if the chunk's length is not even.__

The bug explains also why sometime NAudio was unable to decode some riff chunks that other DAW software handles correctly.

The solution was to modify the WaveFileChunkReader.ReadWaveHeader(Stream stream) method, so when it encounters a odd length it will advance the stream by just one byte just after the stream.Position += chunkLength; line. Now it seems to work correctly.

Here's the modified method:
```
public void ReadWaveHeader(Stream stream)
{
this.dataChunkPosition = -1;
this.waveFormat = null;
this.riffChunks = new List<RiffChunk>();
this.dataChunkLength = 0;

BinaryReader br = new BinaryReader(stream);
ReadRiffHeader(br);
this.riffSize = br.ReadUInt32(); // read the file size (minus 8 bytes)

if (br.ReadInt32() != ChunkIdentifier.ChunkIdentifierToInt32("WAVE"))
{
throw new FormatException("Not a WAVE file - no WAVE header");
}

if (isRf64)
{
ReadDs64Chunk(br);
}

int dataChunkID = ChunkIdentifier.ChunkIdentifierToInt32("data");
int formatChunkId = ChunkIdentifier.ChunkIdentifierToInt32("fmt ");

// sometimes a file has more data than is specified after the RIFF header
long stopPosition = Math.Min(riffSize + 8, stream.Length);

// this -8 is so we can be sure that there are at least 8 bytes for a chunk id and length
while (stream.Position <= stopPosition - 8)
{
Int32 chunkIdentifier = br.ReadInt32();
Int32 chunkLength = br.ReadInt32();
if (chunkIdentifier == dataChunkID)
{
dataChunkPosition = stream.Position;
if (!isRf64) // we already know the dataChunkLength if this is an RF64 file
{
dataChunkLength = chunkLength;
}
stream.Position += chunkLength;
}
else if (chunkIdentifier == formatChunkId)
{
waveFormat = WaveFormat.FromFormatChunk(br, chunkLength);
}
else
{
// check for invalid chunk length
if (chunkLength < 0 || chunkLength > stream.Length - stream.Position)
{
if (strictMode)
{
Debug.Assert(false, String.Format("Invalid chunk length {0}, pos: {1}. length: {2}",
chunkLength, stream.Position, stream.Length));
}
// an exception will be thrown further down if we haven't got a format and data chunk yet,
// otherwise we will tolerate this file despite it having corrupt data at the end
break;
}
if (storeAllChunks)
{
riffChunks.Add(GetRiffChunk(stream, chunkIdentifier, chunkLength));
}
stream.Position += chunkLength;

// REVEL SOFTWARE - UG If the data length is odd we need to pad to word boundary. Data length does not count padding bytes.
// See https://forums.adobe.com/message/3798778 or http://www-mmsp.ece.mcgill.ca/documents/audioformats/wave/Docs/riffmci.pdf
if (chunkLength % 2 > 0)
stream.Position += 1;
}
}

if (waveFormat == null)
{
throw new FormatException("Invalid WAV file - No fmt chunk found");
}
if (dataChunkPosition == -1)
{
throw new FormatException("Invalid WAV file - No data chunk found");
}
}
```

Commented Unassigned: UnspecifiedError calling waveOutOpen when using Channel Phantoming or Speaker Fill [16442]

$
0
0
When calling WaveOutEvent.Init() with a device that points to a speaker that has Speaker Fill or Channel Phantoming(Options in Microsoft's basic sound driver) and a sample rate that is exactly equal to the device's sample rate the Init function throws this error:
NAudio.MmException
"UnspecifiedError calling waveOutOpen"

The following crashes when my speakers are set to 16 or 24 bit 44100 quality with either Channel Phantoming or Speaker Fill enabled:
```
//waveProvideris an instance of a class that inherits WaveProvider32
waveProvider.SetWaveFormat(44100, 2);

NAudio.Wave.WaveOutEvent waveOut = new WaveOutEvent();

waveOut.Init(waveProvider);

waveOut.Play();
```
But changing just the first line to Instance.SetWaveFormat(44099, 2); or Instance.SetWaveFormat(44101, 2); and it runs fine. Or ensuring Channel Phantoming and Speaker Fill are disabled and leaving it with sample rate 44100 will also make it run fine.

The same is true if my speakers are set to 48000 sample rate and have the first line as Instance.SetWaveFormat(48000, 2);
Comments: that's a strange issue. Could be a driver problem. might be worth reading the MSDN waveOutOpen documentation and seeing if there are any clues there

Commented Unassigned: PlaybackStopped event fires at start of playback with NullReferenceException [UPDATED] [16441]

$
0
0
__UPDATE: I found the problem. In my Play() function, when playback is started, it causes a NullReferenceException which is sent in the PlaybackStopped event. However, the file continues playing fine.__

I'm trying to make an application which plays files on a playlist. Whenever the user clicks "next", it will start playing the next song, and immediately move to the next one before it ends. This seems to be caused by the PlaybackStopped event.
Here is the code: (I removed some irrelevant stuff)
```
public class BackgroundPlayer
{
private IWavePlayer myWaveOut;
private WaveStream myAudioFileReader;
public PlaybackStatus Status { get; private set; }
private int nowPlayingID;
public int NowPlayingID
{
get { return nowPlayingID; }
private set
{
nowPlayingID = value;
if (Status == PlaybackStatus.Playing)
Play();
else
{
Unload();
Load();
}
}
}

public void Next()
{
if (NowPlayingID < Playlist.Count - 1)
++NowPlayingID;
else
{
Status = PlaybackStatus.Stopped;
NowPlayingID = 0;
}
}

public void Previous()
{
if (NowPlayingID > 0)
--NowPlayingID;
else
{
Status = PlaybackStatus.Stopped;
NowPlayingID = Playlist.Count - 1;
}
}

public void Play()
{
Unload();
Load();
myWaveOut.Play();
Status = PlaybackStatus.Playing;
}

public void Stop()
{
if (myWaveOut != null)
myWaveOut.Stop();
Status = PlaybackStatus.Stopped;
}

public void Load()
{
myWaveOut = new DirectSoundOut();
myAudioFileReader = new AudioFileReader(NowPlaying.FilePath);
myWaveOut.PlaybackStopped += myWaveOut_PlaybackStopped;
myWaveOut.Init(myAudioFileReader);
}

public void Unload()
{
if (myWaveOut != null)
myWaveOut.Stop();
if (myAudioFileReader != null)
{
myAudioFileReader.Dispose();
myAudioFileReader = null;
}
if (myWaveOut != null)
{
myWaveOut.Dispose();
myWaveOut = null;
}
Status = PlaybackStatus.Stopped;
}

private void myWaveOut_PlaybackStopped(object sender, StoppedEventArgs e)
{
Next();
}
}
```

Should I be using something other than DirectSoundOut?
This code had worked, until I changed something with the Play, Load, and Unload functions.

Thanks in advance for the help!
Comments: OronDF343, I had a similar issue with WaveOutEvent when running in an ASP.NET thread. Is your program running in a Windows Form? or something else? Jake

Commented Unassigned: PlaybackStopped event fires at start of playback with NullReferenceException [UPDATED] [16441]

$
0
0
__UPDATE: I found the problem. In my Play() function, when playback is started, it causes a NullReferenceException which is sent in the PlaybackStopped event. However, the file continues playing fine.__

I'm trying to make an application which plays files on a playlist. Whenever the user clicks "next", it will start playing the next song, and immediately move to the next one before it ends. This seems to be caused by the PlaybackStopped event.
Here is the code: (I removed some irrelevant stuff)
```
public class BackgroundPlayer
{
private IWavePlayer myWaveOut;
private WaveStream myAudioFileReader;
public PlaybackStatus Status { get; private set; }
private int nowPlayingID;
public int NowPlayingID
{
get { return nowPlayingID; }
private set
{
nowPlayingID = value;
if (Status == PlaybackStatus.Playing)
Play();
else
{
Unload();
Load();
}
}
}

public void Next()
{
if (NowPlayingID < Playlist.Count - 1)
++NowPlayingID;
else
{
Status = PlaybackStatus.Stopped;
NowPlayingID = 0;
}
}

public void Previous()
{
if (NowPlayingID > 0)
--NowPlayingID;
else
{
Status = PlaybackStatus.Stopped;
NowPlayingID = Playlist.Count - 1;
}
}

public void Play()
{
Unload();
Load();
myWaveOut.Play();
Status = PlaybackStatus.Playing;
}

public void Stop()
{
if (myWaveOut != null)
myWaveOut.Stop();
Status = PlaybackStatus.Stopped;
}

public void Load()
{
myWaveOut = new DirectSoundOut();
myAudioFileReader = new AudioFileReader(NowPlaying.FilePath);
myWaveOut.PlaybackStopped += myWaveOut_PlaybackStopped;
myWaveOut.Init(myAudioFileReader);
}

public void Unload()
{
if (myWaveOut != null)
myWaveOut.Stop();
if (myAudioFileReader != null)
{
myAudioFileReader.Dispose();
myAudioFileReader = null;
}
if (myWaveOut != null)
{
myWaveOut.Dispose();
myWaveOut = null;
}
Status = PlaybackStatus.Stopped;
}

private void myWaveOut_PlaybackStopped(object sender, StoppedEventArgs e)
{
Next();
}
}
```

Should I be using something other than DirectSoundOut?
This code had worked, until I changed something with the Play, Load, and Unload functions.

Thanks in advance for the help!
Comments: My program is a windows form, but it seems that the code above is in a separate thread.

Created Unassigned: Not available for Windows Phone 8 projects? [16444]

$
0
0
Trying to create a universal Windows 8/WP8 app, but I can't add Naudio package to WP8.1 project.
Is WP8 unsupported? Will it be supported anytime soon?
Viewing all 738 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>