(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!
//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");
short int *buffer;
short int *buffer2;
int nr_of_channels =1;
int sample_rate2 =44100;
if (! file_a)
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 )
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 ();
dev = OpenDevice (0, "buffer=10000, rate=44100");
if (dev == 0)
//TODO: Error message:
//true stands for that the audio file should be played.
output_stream2 = new OutputStreamPtr (OpenSound(dev,(const SampleSourcePtr&)sample_device_ptr,true));