Beep tones and i/o

Hello all,

I am working on a hangman program and i am almost complete.

on the victory screen i have the option to play again or quit, I have tried to add a song made up of beeps to the screen but it interrupts the restart option and will not allow input until the song is over.

I was wondering if there is a way to play the beeps and still enter input.

this is basically what i have for the victory function
1
2
3
4
5
6
7
void victory()
{
        cout << "1)play again\n";
        cout << "2)quit\n";
        song();
        cin >> restart;
}


I have the beeps in a function called song(); and I have tried putting it before the output and after and all over.

I only know so much c++, but any help or ideas would be greatly appreciated.

thanks,
R!NG0
You could make it a multithreaded program, or you could make another program that plays the beeps and call that program from your program, you could also use a 3rd party lib, SFML actually makes it pretty easy to get up and running with music (of course you can't use .mp3 but you can convert to .ogg and play it that way) I know it's not beeps but it would probably help you out to start on a 3rd party lib.
first, thank you for you reply!

second, I am what you might call a highschool level programmer meaning I only understood some of that.

if i made another program for the beeps how would i call it in the other program? does it need to be in the same folder?

should I just google this stuff? can i find tutorials on these methods?

thanks,(and sorry for being a n00b)
R!NG0

The easiest, and probably the worst way you could do it, is just use system("my other program here"); It's hacky and it will fail. If it's just for yourself it will work. Are you trying to make this cross-platform? Does it matter? If not are you on windows? (I'm assuming so because you're using Beep())
yes, I would like it work on different machines.

of all the options you listed what would you suggest that would work most efficiently?

this is a pet project no real goal or prize except of course for pride but I would still like it to be as good as it can be.

and yes I'm on windows.

could i play a midi file? is that even possible? and if so would it interrupt the input?
Efficiently... that doesn't matter. Truly it doesn't. Unless you're planning on some crazy computation you won't have to worry about efficiency.

Aparently winmm has some midi functionality. Midi support in c++ is sketchy at best. But you can do it if you want to go that direction.

If you just want audio, SFML would be your best approach, it allows easy loading easy playing and easy stopping. All within your main thread so you don't have to worry about multithreading.

if you're interested in said library, here's a link: http://www.sfml-dev.org/tutorials/1.6/
If you don't mind using a deprecated API, you could use the midi functionality ultifinitus alluded to:

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
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mmsystem.h> // link to winmm.lib

#include <iostream>
using namespace std;

void PlayMidiAndWait(const char* path)
{
    char cmd[1024] = "";
    sprintf(cmd, "play %s wait", path);
    mciSendString(cmd, NULL, 0, NULL);
}

void PlayMidi(const char* path)
{
    char cmd[1024] = "";
    sprintf(cmd, "open %s alias tune", path);
    mciSendString(cmd, NULL, 0, NULL);
    mciSendString("play tune", NULL, 0, NULL);
}

void StopMidi()
{
    const char cmd[] = "stop tune";
    mciSendString(cmd, NULL, 0, NULL);
}

int main()
{
    const char path[] = "C:\\WINDOWS\\Media\\town.mid";

    cout << "Play" << endl;
    PlayMidi(path);
    Sleep(3000);
    cout << "Stop" << endl;
    StopMidi();
    Sleep(3000);
    cout << "Play" << endl;
    PlayMidi(path);
    Sleep(3000);
    cout << "Stop" << endl;
    StopMidi();

    return 0;
}

Last edited on
Topic archived. No new replies allowed.