Click here to Skip to main content
15,913,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have played .wav file, using SDL_mixer library. But i don't know how to mix multiply audio stream. Please suggest for me!
Posted
Comments
Amir Mahfoozi 10-Jan-12 0:44am    
When I was younger I sent voice with ALSA over network in Linux :). If their two buffers are the same size try to compute each corresponding bytes average and send the result, I think it will mix two sounds together. BTW you can add weight to them to amplify one channel more than the other one.
mot sach 10-Jan-12 1:12am    
thank you, i will try to do that, and another question, how to get raw data from .wav file? (I have gotten header by reading from fist byte (byte 0) to 43th byte, but i'm not sure raw data beginning from 44th byte.)
Amir Mahfoozi 10-Jan-12 1:21am    
When you speak about WAV you speak about vast variety of formats! There are some of them which are compressed and to some extent are mp3! So do not get confused with them and just search for libraries that work with old and uncomplicated ones. There are a lot of loaders which can be found by Google.
mot sach 10-Jan-12 1:59am    
oh yeah, i have a little confusion, i think .wav file only contains raw data. Can load raw data from local file by using SDL_mixer lib, but i don't know how to mix multiply stream
Amir Mahfoozi 10-Jan-12 2:47am    
I speculate that it can be done by computing average of each corresponding two bytes. If the sample rates and buffer lengths for two streams are the same.

for examples of establishing UDP and TCP connections read here :
http://www.tenouk.com/Module40c.html[^]

And just for UDP read here :
http://www.abc.se/~m6695/udp.html[^]

And here is a sample wave player with SDL :
http://content.gpwiki.org/index.php/SDL_mixer:Tutorials:Playing_a_WAV_Sound_File[^]

Just move its reader part to the server and player part to the client (or vice versa). It's important to make sure that each module is working properly and then mix them together.

Hope it helps.
 
Share this answer
 
v2
Comments
RaisKazi 11-Jan-12 6:12am    
My 5.
Amir Mahfoozi 11-Jan-12 6:17am    
Thanks Rais
mot sach 12-Jan-12 4:27am    
so sorry, but i wanna ask you another question. If i use SDL_mixer lib to play sound, i need to allocate a large buffer of Mix_Chunk.
<pre lang="c++">
void play (uint8_t* stream, uint32_t len)
{
int channel;
int latency = len; // size of buffer
//init chunk
Mix_Chunk* chunk = (Mix_Chunk*) malloc (sizeof(Mix_Chunk));
chunk->abuf = (uint8_t*) malloc(latency); // len is very big
chunk->alen = len;
chunk->alloceted = true;
//copy data from stream to chunk
memcpy(chunk->abuf, stream, len);
//play audio
channel = Mix_PlayChannel(-1, chunk, 0);
while (Mix_Playing(channel); //while finished playing
}
</pre>

How to reduce "latency"?
Amir Mahfoozi 12-Jan-12 4:41am    
If I were you I was finishing the job with a small file and then would focus on read the file in small chunks. So please do not allow this problem stop you from doing the whole job. However I will do a try to find out how a file could be read in chunks in SDL. If there was no solution SDL could be replaced by another library. Just reach to a small working client server program and then solve those problems.
Amir Mahfoozi 12-Jan-12 4:46am    
Have a look at this page :
http://sdl.beuc.net/sdl.wiki/Audio_Examples
It have mixed two buffers together and used callbacks to avoid to load the whole file.
But firstly try to solve the problem with simplified assumptions, for example you have just one stream and small file ...
so sorry, but i wanna ask you another question. If i use SDL_mixer lib to play sound, i need to allocate a large buffer of Mix_Chunk.
C++
void play (uint8_t* stream, uint32_t len)
{
    int channel;
    int latency = len;          // size of buffer
    //init chunk
    Mix_Chunk* chunk = (Mix_Chunk*) malloc (sizeof(Mix_Chunk));
    chunk-&gt;abuf = (uint8_t*) malloc(latency);    // len is very big
    chunk-&gt;alen = len;
    chunk-&gt;alloceted = true;
    //copy data from stream to chunk
    memcpy(chunk-&gt;abuf, stream, len);
    //play audio
    channel = Mix_PlayChannel(-1, chunk, 0);
    while (Mix_Playing(channel); //while finished playing
}

How to reduce "latency"?
 
Share this answer
 
yes, thank so much, this is my solution:
C#
unsigned int    period_size = 4096;     // size of buffer
//init mix chunk
Mix_Chunk*  sound = (Mix_Chunk*) malloc(sizeof(Mix_Chunk);

sound-&gt;alen = 4096;
sound-&gt;abuf = (uint8_t*) malloc(period_size);
sound-&gt;allocated = true;

void play(data, len)
{
    while (len) {
        //copy data to chunk
        if (len &gt; period_size) {
            memcpy(sound-&gt;abuf, data + index, sound-&gt;alen);
            len -= sound-&gt;alen;
                index += sound-&gt;alen;
        }
        else {
            memcpy(sound-&gt;abuf, data + index, len);
            len -= len;
            index += len;
        }
        //Play our sound file, and capture the channel on which it is played
        channel2 = Mix_PlayChannel(-1, sound2, 0);
        if (channel2 == -1) {
            printf("Unable to play WAV file: %s\n", Mix_GetError());
            exit(-1);
        }
        //Wait until the sound has stopped playing
        while(Mix_Playing(channel2) != 0);
    }
}

Sequentially, I has copied data from stream by block (4096 bytes), then waiting finished play it . It work ok. But i think this is not good ideal. Do you think so
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900