Putting audio files into buffers, mixing them, and playing the result from the buffer.

(In my last question I asked with help to get rid of a linking error message. That was solved! Now I need help with getting the audio to be played.)

I use a library called Audiere.
See the documentation of the classes here: http://audiere.sourceforge.net/audiere-1.9.4-users-doxygen/classes.html

Here is what I want to do with the following code:
I want to mix two wav.files, one of them containing noise.
In order to do that I want the samples of both of the files placed in different buffers. After that I mix them together into one of the buffers. After that I want to play the content of the buffer.

(I also will implement so that the noise level will be increased/decreased depending on user input. I will possibly also increase/decrease the volume of the other sound file before I mix it.)

I have tested the below code but it doesn't play any sound.

The numbers that I use in the code are only temporary.
The number of frames, sample rate and number of channels should be retrieved from the files. Please implement that in your solution.

I would be thankful if anyone could help me in making it to work!

(It maybe very trivial to fix, but it is late here in Sweden, so I can't work more with it today; and a deadline for this product is approaching..What I need help with is a small part in a larger product. If your code works, then I will use it in the product.)

Code using other libraries (if I am allowed to use the code in a commercial product provided that I include for example a GPL-license) is also accepted.

The code will be implemented in a Windows Forms-applications programmed in  Virtual C++ 2008-Express.

Thanks in advance!

Anders Branderud
#include "audiere.h"
				 audiere::OutputStreamPtr* output_stream2;

                 //Alot of code that you don't need to see in order to help.
				 enum SampleFormat file_noise_format = SF_S16;
				 FILE* file_a = fopen ("ljudfiler/HF01.wav", "rb");
				 long len;
				 short int *buffer;
				 long len2;
				 short int *buffer2;
				 int nr_of_channels =1;
				 int sample_rate2 =44100;

				 if (! file_a)
					 //Error message
				   buffer = (short int*) malloc (len); //malloc buffer
				   fread (buffer, len, 1, file_a); //read into buffer

				 FILE* file_b = fopen ("ljudfiler/noise/cocktail_noise.wav", "rb");
				 if (!file_b )
					 //TODO:Error message
					 buffer2 = (short int*) malloc (len2); //malloc buffer
					 fread (buffer2,len2 ,1, file_b); //read into buffer

                 SampleBuffer* sample_buffer = CreateSampleBuffer (buffer,100000,1,44100,SF_S16);
				 SampleBuffer* sample_buffer2 = CreateSampleBuffer (buffer2,100000,1,44100,SF_S16);

				 //Open a seekable sample source using the samples contained in the buffer.
				 SampleSource* sample_source = sample_buffer->openStream ();
				 SampleSource* sample_source2 = sample_buffer2->openStream();

				 //The samples are placed in a buffer and we can manipulate with buffers.
				 for (int i =0; i < file_number_of_frames; i++ )
					 buffer[i] = buffer[i]+buffer2[i];

				 //In sample_buffer3 we will store what should be played.	
				 SampleBuffer* sample_buffer3 = CreateSampleBuffer (buffer,100000,1,44100,SF_S16);
				 SampleSource* sample_source3 = sample_buffer3->openStream ();
				 AudioDevice* dev;
				 dev = OpenDevice (0, "buffer=10000, rate=44100");
				 if (dev == 0)
					 //TODO: Error message:

				 SampleSourcePtr sample_device_ptr(sample_source3);
				 sample_buffer3->getFormat ((int&)nr_of_channels,(int&)sample_rate2,file_noise_format);
				 //true stands for that the audio file should be played.
				 output_stream2 = new OutputStreamPtr (OpenSound(dev,(const SampleSourcePtr&)sample_device_ptr,true));

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I recommend that you never defer error TODO's as they are often critical in finding bugs. Also, if there is an error, at the minimum, print out some message and exit the program.

If you are using Visual Studio, then perhaps the following will help. Instead of
     FILE* file_a = fopen ("ljudfiler/HF01.wav", "rb");
    FILE* file_a = fopen ("ljudfiler\\HF01.wav", "rb");

and change
    FILE* file_b = fopen ("ljudfiler/noise/cocktail_noise.wav", "rb");
    FILE* file_b = fopen ("ljudfiler\\noise\\cocktail_noise.wav", "rb");

But do put in the error TODOs and exit.

AndersBranderudAuthor Commented:
Hello and thanks for your reply!

I did have an error message, so I shouldn't have written "TODO". I wrote that instead of the Visual C++-code that printed out an error message on the GUI. I didn't mean to give an incorrect description, I just erred in my sleepyness.

I gave up using the Audiere-library. I instead chose another solution in which I create a wav file with the mixed sound and play that file; instead of playing the file from the buffer.

Anders Branderud


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Was the problem related to the double-backslash, single forward slash issue noted in the above comment?

In any case good for you to find a new approach that is working for you.

Interesting link you have there. I didn't understand the sources well. But the bottom line of the first section, well, it would appear that something like this would be in a best-selling book. (Someone could even make a movie with enough creativity.)
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

I would assume that you made sure that the wave files are indeed 16-bit mono PCM at 44KHz?

A few things with your code:

- Where is file_number_of_frames being set? Is it set correctly?
- Same for file_noise_format.
- Additive mixing may produce garbage, due to overflow. But at least you would hear something.
- I'm not sure, but are you supposed to read the entire wav file (including non-data chunks) and pass those to CreateSampleBuffer?

I havent used that lib therefore cant say besides these at the moment. Can you please also comment out the mixing part and see if the non-mixed samples (single file) plays well?

bad comment :(
AndersBranderudAuthor Commented:
“Was the problem related to the double-backslash, single forward slash issue noted in the above comment?”
No, it wasn’t.

“Interesting link you have there. I didn't understand the sources well. But the bottom line of the first section, well, it would appear that something like this would be in a best-selling book. (Someone could even make a movie with enough creativity.)”

If interested more information is found in the website of Netzarim (www.netzarim.co.il), especially the History Museum-pages
It is in a book, but it is not best-selling.

Anders Branderud
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Multimedia Programming

From novice to tech pro — start learning today.