Now I'm using waveOut instead od DirectSoundOut. But PlaybackStopped event still doesn't fire, when playback actually ends itself.
New Post: When playback ends
New Post: When playback ends
it may be that the waveprovider you gave to waveOut never stops returning data from the Read function. this is quite possible as some streams pad with zeroes. you can simply test it by setting up a loop that reads from your waveprovider and see if it ever exits
New Post: Play position
Now, I'm using WaveOut. I noticed that it has DesiredLatency property default set to 300. So I tried higher values. Even 2000. In the beginning everything seems to hear good, although the indicator "started" somewhere in the half o the track and then it showed up somewhere at the end. Track length was about 4 seconds. In about half of the track, output "cut" some part.
To make myself more clear, let say that my record was: "One two three four five six seven eight nine ten" and lasts about 4, 5 seconds.
I can hear smething like "One two three four five eight ten"
The higher latency, the more weird output ;)
I didn't get rid off this tick, although they are not so frequent as they were. In latency about 2000 there is really smooth output(about 2 or 3 ticks), but there is this "cut". Now I'm using 400 latency. What can I do more?
New Post: Use of WaveViewer control with raw streams
I am trying to use the WaveViewer control to visualize a raw stream with 8bit, 8k data. The issue I am having is that the WaveViewer displays a nearly all black waveform (appears like extreme clipping) that looks normal in Audacity. How do I control the appearance so that it looks as expected. I would also like to change the color of the graph if possible, but when I change the color property nothing happens.
private void ShowActivePhrase(string path)
{ FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read); WaveFormat format = new WaveFormat(8000, 8, 1); WaveStream rawStream = new RawSourceWaveStream(file, format); waveViewer1.SamplesPerPixel = 24; waveViewer1.WaveStream = rawStream;
}
New Post: Use of WaveViewer control with raw streams
I found the source code to the control and saw what my problem is. The control is expecting 16 bit data and I was feeding it 8 bit data. I used WaveFormatConversionStream plotStream = new WaveFormatConversionStream(new WaveFormat(8000, 16, 1), rawStream) to do the conversion and now it plots correctly. I also saw the control is hard coded to use a black pen so I will probably modify the control to use the forecolor.
I am very pleased with NAudio, it does everything I need, just wish there was more documentation available for newbies like myself.
New Post: WaveIn to SoundPlayer issue.
NAudio has its own playback mechanisms so if you are using NAudio, you would not normally also use SoundPlayer. You could use WaveFileWriter to create a WAV file in a MemoryStream, but you can't append to that stream while you are playing, since the WAV header must know the file length in advance.
New Post: PlaybackState remains at Playing once finished
probably you have passed a never-ending stream in. Your code looks based on very old sample code. The new MP3FileReader doesn't need the use of BlockAlignReduction stream or an additional WaveFormatConversionStream.
New Post: Use of WaveViewer control with raw streams
glad you got it working. Sorry about lack of documentation. NAudio is just a spare time hobby project for me and for the first 7 years I was the only person to use it, so there was no point writing reams of documentation. I try to regularly blog about NAudio, and the NAudioDemo project is the main place to go to see examples of the classes in real situations.
Mark
New Post: Play position
If you are getting weird playback problems, use the WaveFileWriter to dump your output waveprovider to a WAV file and then check that the WAV file has the audio you expect. That will tell you whether the problem is generating the output or playing it back.
New Post: Server-side microphone capture in ASP.NET/ C#
first of all, you are playing audio on the server, not on the clients. NAudio cannot be used to play audio in the browser of the people visiting your page.
Secondly, you can't feed WaveOut from WaveIn directly. You need to use a BufferedWaveProvider.
Mark
Commented Issue: Destination array was not long anough when reading mp3 file whit NAudio [16345]
static void Main(string[] args)
{
try
{
Mp3FileReader reader = new Mp3FileReader(@"C:\testAudio.mp3");
long count = reader.Length;
if (count <= int.MaxValue)
{
byte[] info = new byte[count];
reader.Read(info, 0, (int)count);
Console.WriteLine("Succesfull read");
}
else
Console.WriteLine("Could not read");
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
}
that prints out the following exception message
System.ArgumentException: Destination array was not long enough. Check destIndex and length, and the array's lower bounds.
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length)
at NAudio.Wave.AcmMp3FrameDecompressor.DecompressFrame(Mp3Frame frame, Byte[] dest, Int32 destOffset) in C:\NAudio-Source\NAudio\FileFormats\Mp3\Mp3FrameDecompressor.cs:line 50
at NAudio.Wave.Mp3FileReader.Read(Byte[] sampleBuffer, Int32 offset, Int32 numBytes) in C:\NAudio-Source\NAudio\Wave\WaveStreams\Mp3FileReader.cs:line 338
at Tester.Program.Main(String[] args) in C:\Tester\Program.cs:line 30
I've dowloaded NAudio code and I've been debugging it but I can't find the cause of the error, although, as you can see on the stack trace is in *NAudio-Source\NAudio\FileFormats\Mp3\Mp3FrameDecompressor.cs:line 50*
¿Am I doing something wrong? By the way, it only happens with a few mp3 files, others are read just fine. I'm attaching one of the files that can't be read
Thanx
Comments: Normally you read from a stream in smaller chunks (e.g. 1kB at a time), so I am not surprised this doesn't work. But thanks for submitting an example file - I'll see if I can test this scenario and find out what the issue is when I get a chance.
New Post: Use of WaveViewer control with raw streams
I really appreciate your efforts to develop this and I think others who have a good handle on how to use the objects could help provide highly simplified code examples and class descriptions to distribute the documentation work. The YouTube videos by Giawa are a good example of this.
Thanks!
Commented Issue: Destination array was not long anough when reading mp3 file whit NAudio [16345]
static void Main(string[] args)
{
try
{
Mp3FileReader reader = new Mp3FileReader(@"C:\testAudio.mp3");
long count = reader.Length;
if (count <= int.MaxValue)
{
byte[] info = new byte[count];
reader.Read(info, 0, (int)count);
Console.WriteLine("Succesfull read");
}
else
Console.WriteLine("Could not read");
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
}
that prints out the following exception message
System.ArgumentException: Destination array was not long enough. Check destIndex and length, and the array's lower bounds.
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length)
at NAudio.Wave.AcmMp3FrameDecompressor.DecompressFrame(Mp3Frame frame, Byte[] dest, Int32 destOffset) in C:\NAudio-Source\NAudio\FileFormats\Mp3\Mp3FrameDecompressor.cs:line 50
at NAudio.Wave.Mp3FileReader.Read(Byte[] sampleBuffer, Int32 offset, Int32 numBytes) in C:\NAudio-Source\NAudio\Wave\WaveStreams\Mp3FileReader.cs:line 338
at Tester.Program.Main(String[] args) in C:\Tester\Program.cs:line 30
I've dowloaded NAudio code and I've been debugging it but I can't find the cause of the error, although, as you can see on the stack trace is in *NAudio-Source\NAudio\FileFormats\Mp3\Mp3FrameDecompressor.cs:line 50*
¿Am I doing something wrong? By the way, it only happens with a few mp3 files, others are read just fine. I'm attaching one of the files that can't be read
Thanx
Comments: Hello Mark, I've continue to debug the code, NAdio include, and the problem has nothing to do with trying to read the whole file at once. Te problem occurs when decompressing one specific frame. The frame read is of size 3072 (bytes), when decompressing with AcmInterop.acmStreamConvert, the resulting decompressed length is of 27648 bytes. But then the decomprresBuffer on Mp3FileReader, where the decompressed bytes are copied, is of length 9216 (in my case), thus the error when trying to copy 27648 bytes into an 9216 lengthed byte array on Mp3FrameDecompressor.DecompressFrame (line 50). First, initialization of decompressBuffer on Mp3FileReader is as follows this.decompressBuffer = new byte[1152 * bytesPerSample * 2]; (which in may case yields an array of lenght 9216) This initialization is acompanied by the following comment: // no MP3 frames have more than 1152 samples in them // some MP3s I seem to get double and this has gotten me thinking "¿Is it alright if I increase the size of decompressBuffer by some value or would that have some freak effect somewhere else on the code? ¿Could it be possible that this particular frame contains more bytes than usual when decompressed or is the some error of AcmInterop.acmStreamConvert, that is, the codecs?" Some comments on this would be great. Thanks a lot again, an love using NAudio. (By the way, all this was tested with the file i previously attached. The conflicted frame is the second frame read if readding after calling Seek(19888128, SeekOrigin.Begin)). I'm pasting some code: public static void NAudioTest() { try { using (Mp3FileReader reader = new Mp3FileReader(@"F:\Music\__PRUEBA\14-Hammer to fall.mp3")) { byte[] info = new byte[2048]; reader.Seek(19888128, SeekOrigin.Begin); reader.Read(info, 0, 2048); Console.WriteLine("Succesfull read"); } } catch (Exception ex) { Console.WriteLine(ex); } Console.ReadLine(); }
Commented Issue: Destination array was not long anough when reading mp3 file whit NAudio [16345]
static void Main(string[] args)
{
try
{
Mp3FileReader reader = new Mp3FileReader(@"C:\testAudio.mp3");
long count = reader.Length;
if (count <= int.MaxValue)
{
byte[] info = new byte[count];
reader.Read(info, 0, (int)count);
Console.WriteLine("Succesfull read");
}
else
Console.WriteLine("Could not read");
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
}
that prints out the following exception message
System.ArgumentException: Destination array was not long enough. Check destIndex and length, and the array's lower bounds.
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length)
at NAudio.Wave.AcmMp3FrameDecompressor.DecompressFrame(Mp3Frame frame, Byte[] dest, Int32 destOffset) in C:\NAudio-Source\NAudio\FileFormats\Mp3\Mp3FrameDecompressor.cs:line 50
at NAudio.Wave.Mp3FileReader.Read(Byte[] sampleBuffer, Int32 offset, Int32 numBytes) in C:\NAudio-Source\NAudio\Wave\WaveStreams\Mp3FileReader.cs:line 338
at Tester.Program.Main(String[] args) in C:\Tester\Program.cs:line 30
I've dowloaded NAudio code and I've been debugging it but I can't find the cause of the error, although, as you can see on the stack trace is in *NAudio-Source\NAudio\FileFormats\Mp3\Mp3FrameDecompressor.cs:line 50*
¿Am I doing something wrong? By the way, it only happens with a few mp3 files, others are read just fine. I'm attaching one of the files that can't be read
Thanx
Comments: that's interetsing, but I was able to successfully play your example file using NAudio demo, so I can't understand why it doesn't work for you. Can you play it with NAudioDemo?
Commented Issue: Destination array was not long anough when reading mp3 file whit NAudio [16345]
static void Main(string[] args)
{
try
{
Mp3FileReader reader = new Mp3FileReader(@"C:\testAudio.mp3");
long count = reader.Length;
if (count <= int.MaxValue)
{
byte[] info = new byte[count];
reader.Read(info, 0, (int)count);
Console.WriteLine("Succesfull read");
}
else
Console.WriteLine("Could not read");
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
}
that prints out the following exception message
System.ArgumentException: Destination array was not long enough. Check destIndex and length, and the array's lower bounds.
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length)
at NAudio.Wave.AcmMp3FrameDecompressor.DecompressFrame(Mp3Frame frame, Byte[] dest, Int32 destOffset) in C:\NAudio-Source\NAudio\FileFormats\Mp3\Mp3FrameDecompressor.cs:line 50
at NAudio.Wave.Mp3FileReader.Read(Byte[] sampleBuffer, Int32 offset, Int32 numBytes) in C:\NAudio-Source\NAudio\Wave\WaveStreams\Mp3FileReader.cs:line 338
at Tester.Program.Main(String[] args) in C:\Tester\Program.cs:line 30
I've dowloaded NAudio code and I've been debugging it but I can't find the cause of the error, although, as you can see on the stack trace is in *NAudio-Source\NAudio\FileFormats\Mp3\Mp3FrameDecompressor.cs:line 50*
¿Am I doing something wrong? By the way, it only happens with a few mp3 files, others are read just fine. I'm attaching one of the files that can't be read
Thanx
Comments: Hi Mark, I tried NAudioDemo, and playing the file also fails, at around 1:52 minutes from the start. From then on the file keeps playing, but "jumpy". I try it also on Windows Media Player, and, althought it keeps playing normally, around 1:52 theres like a glitch sound. I'm thinking maybe the file is corrupted? Or maybe my set of codecs is not the ideal one?? Well, if you find out anything else, please let me know.
New Post: NAudio Recording Performance
I'm using NAudio to record audio.
It seems to work well on my new desktop machine, which is a 4 core i7, but on my 3 year old laptop, the recorded audio has all sorts of "popping" noises. I suspect what's going on here is that because the app does lots of processing, and because the system overall is under 60% CPU load, perhaps the thread that is sampling the audio is getting interrupted, etc.
I was really hoping to release my app this weekend, and so to discover this at the last minute is really unfortunate. I'm hoping there is some way to set a thread priority somewhere so that the thread used by NAudio to do the recording is able to do its job without resulting in these popping noises.
If anyone know how to help, I'd be most grateful.
Thanks!
Daniel
New Post: PlaybackState remains at Playing once finished
Thanks for your answer. I stripped BlockAlignReduction and WaveFormatConversionStream from the code, but now I realize that you are saying they are not necessary for MP3FileReader whereas in the code I posted above they are applied to WAV streams, not MP3 streams. Should they still be used for WAV streams?
Also, I have cleaner code with the workaround I came up with on StackOverflow at http://stackoverflow.com/questions/10243031/why-does-playbackstate-remain-at-playing-after-playbackstopped-fires
Would you mind having a quick look?
Many thanks!
New Post: PlaybackState remains at Playing once finished
unless you are playing compressed WAV files (e.g. G.711 then no WaveFormatConversionStream step is necessary)
New Post: NAudio Recording Performance
The best way round popping is either to increase the latency (buffer sizes) or to find ways to optimise your playback code. Some of the NAudio playback models do create background threads, so you could copy the code and create one with a higher priority background thread, but I suspect the implact would be fairly limited.
New Post: Sending and playing MP3 frames
Hi
I'm trying to get a simple mp3 stream (server -> client) going. At the moment I have a server which
1. Opens the mp3 file
2. Reads one frame
3. Sends the frame to the client
a) More data? Go to 2
The client reads the frames into a memory stream. But here is my current problem, how do I play it?
Thread 1 feeds the memory stream, while thread 2 plays it was my plan. But currently I don't know how to play it.
So any input like help, suggestions, tips would be greatly appreciated.