Thread: Making Sounds, Beeps and other pleasing things to the ear.

  1. #1
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079

    Making Sounds, Beeps and other pleasing things to the ear.

    This is another Newbie question. I know that "\a" does an alert beep after the given line, but is there a way to play different toned beeps to make a little tune at the beginning of a program?

    Also another unrealted question. Can you make Pointers point to specific lines of code? Like if I wanted to point to line 21 should it be something like "pointer=& line 21;"?
    Last edited by SlyMaelstrom; 05-10-2004 at 03:21 PM.

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    I beleive the function is 'beep()' and it lies somewhere in windows.h , Parameters are tone and duration. Also look into the directsound api, or other api's specific to your sound card.

    Pointers point to a specific spot in memory, not a 'line' in your code. You can't do this because that 'line' in your code is transformed in so many ways in the compilation process. Not only do you go into asm to binary, but the transition of the code is far to complex and variative in nature, that the compiler wouldn't be able to understand and translate that. Even if it could there are better ways to do it. Do you fully understand the concept of pointers? A 'line' consists of not just code reserving and takeing up memory, but it also consists of manipulation and evaluating, modifying memory, performing certain instructions on memory, etc. So not everything in a line would be considered 'pointable', if that makes any sense.

    Does that answer your question?
    Last edited by Tronic; 05-10-2004 at 04:26 PM.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    beep()

    There is no sound in ANSI standard C++. This means that it all depends on your operating system, hardware, and compiler.

    beep() is a bummer!
    If you're using MSVC++ on a Windows system, beep() will work... but it's a weird function. The way it works depends on your operating system and if you have a sound card. The function is beep(frequency, duration). If you have a sound card, the frequency and duration paramaters will be ignored, and it will play the Windows-Default sound thru the external speakers. If you don't have a sound card the sound will come-out of the PC's internal speaker. With Win98, the frequency and duration parameters will be ignored, and it will play a generic "beep". On WinNT, Win2K, or WinXP, (without a sound card) the frequency and duration paramaters will work as expected.

    You can play a wave sound with PlaySound(). There are other DirectSound functions for playing wave files too. Wave files are digitally recorded sounds.

    Or, you can play MIDI sounds. A MIDI file is not a recorded sound. The notes, timing, and "Instrument" characteristics are stored, and the sound is generated upon playback. MIDI is very efficient. Here's a link to some MIDI tutorials.

    [EDIT] - Pointers
    You can make a pointer to a function. Then, there's the EVIL goto statement. Perhaps that's what you are looking for. With goto, you can jump to any point in your code, but in C++, goto is considered bad practice. Conditional program flow in C++ is done by conditionally calling different functions with if-statements & switch-statements, NOT by jumping-around with goto.
    Last edited by DougDbug; 05-10-2004 at 08:05 PM.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    As has been said there is no way to determine where in memory your line will be. Also one line of C++ code might result in several lines of asm code so the translation process is not as straightforward as lines.

    For instance:


    for (unsigned char i=0;i<255;i++)

    might be:

    ...
    mov cx,0xFF
    @START:
    loop @START
    ...

    or any number of other possibilities.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    First off, I managed to fix my pointer problem.
    I did it with looping a switch statement and having it exit the loop in all cases except one. Rather than just having that one jump back to the beginning.

    Secondly... I know you can play Wav and Midi files, but if I were to send this program to someone, I'd have to include the Wav or Midi file with the package, right? It isn't compiled with the program, right?

Popular pages Recent additions subscribe to a feed