How to play 2 waves at the same time? in "C"

Is it possible to play 2 waves t the same time in "C" programming ?
I'm asking this because I'm working on a game, and I have wave sounds for almost everything, like selecting items, alerts, etc.. but I want also to play music in the background. For now if I call multiple times 'PlaySound();' it will play the sounds one at time, and doesn't pass to next sound until the first one ends, and ofcourse the game freeze until the bran stops.
Thanks..
Last edited on
Use several threads, even parallel OpenMP and others
And with a problem like that your own research skills might have come across this within 10 seconds https://stackoverflow.com/questions/39503444/c-play-multiple-wave-resource-files-at-once
Last edited on
Does pthread.h can help me doing this..as you said Use several threads or do I have to download and use openAL, maybe FMOD or SFML or XAudio2? I think is a bit complicate to accomplish this, I just watch youtube examples, and they are pretty hard to understand.. dohhh :\
Is it even possible to use them in "C" ?
Last edited on
Unfortunately I don't know any more than what I posted which was intended as pointers to possible avenues I would take in trying out the idea of simultaneous processing.
As far as threads are concerned http://www.cplusplus.com/reference/multithreading/
and,
https://www.geeksforgeeks.org/multithreading-c-2/
and
https://www.openmp.org/

There are probably many ways to handle it but I'd start by writing a program to play a single wav file - not too hard I guess - and then start modifying that for multiple threads. Of course there is software that does all that for you channel by channel with volume knobs, meters and dials - but that is not programming :(
I got it finally.. I have 2 days of reading and download a bunch of files and l decided at last to play multiple audio files with openAL.lib, which is not that simple as it show on web pages. However for me remains one problem and I did try it but loading files from resources doesn't work with openAL, I mean I tried to use MAKEINTRESOURCE(name_ID) but the program crush so the only solution is to #define FILE "name.wav" and then pass it to alutCreateBufferFromFile(const char* fileName) function and of course to carry on files in same folder with the .exe Here is a sample code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
///try1///
#include <AL/al.h>
#include <AL/alc.h>
#include <alut.h>
#include <conio.h>
#include <windows.h>
#include "resource.h"

#define FILE1   "yourWav1.wav"
#define FILE2   "yourWav2.wav"
#define FILE3   "yourWav3.wav"
#define MAX_NUM 3

int main()
{
    ALuint uiBuffer;
    ALuint uiSources[MAX_NUM];

    alutInit(0, NULL);
    alGetError();

    uiBuffer = alutCreateBufferFromFile(FILE2);

    alGenSources(1, &uiSources[1]);
    alSourcei(uiSources[1], AL_BUFFER, uiBuffer);

    alSourcei(uiSources[1], AL_LOOPING, AL_TRUE);
    alSourcePlay(uiSources[1]);


    while(1)
    {

        if(_kbhit())
        {
            int ch = _getch();

            switch(ch)
            {
            case 'a':
                {
                    uiBuffer = alutCreateBufferFromFile(FILE1);

                    alGenSources(1, &uiSources[0]);

                    alSourcei(uiSources[0], AL_BUFFER, uiBuffer);
                    alSourcePlay(uiSources[0]);
                    break;
                }

            case 'c':
                {
                    uiBuffer = alutCreateBufferFromFile(FILE3);

                    alGenSources(1, &uiSources[2]);

                    alSourcei(uiSources[2], AL_BUFFER, uiBuffer);
                    alSourcePlay(uiSources[2]);
                    break;
                }
            }
        }
    }
    for(int i = 0; i < 3; i++)
        alDeleteSources(MAX_NUM, &uiSources[i]); // <-modified to delete all 3
    alDeleteBuffers(MAX_NUM, &uiBuffer);

    return 0;
}
Last edited on
Topic archived. No new replies allowed.