Thread: C++ sound and waveform functions

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    17

    C++ sound and waveform functions

    How do i change the frequency,waveshape,waveform,duration
    of the sound chip when programming in C++?

    Where do i find these functions and to call them ?

    Do i have to find the Datasheets to the sound card chips?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well you could start by saying which OS/Compiler you have.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    windows 98 or XP how do i do this in C++?

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    There is no sound in standard ANSI/ISO C++ !


    Look-up Direct Sound (part of Direct X) or MCI (Multimedia control Interface) on MSDN.

    There are lots of ways to make sound with the Windows API C++ library. But, Microsoft doesn't make it easy to do what you want. There isn't a simple MakeSound() function... You're probably going to need several lines of code to generate a simple tone of a given frequency. There are some simple MIDI functions, but you basically choose an instrument and a note.

    Windows tries to insulate you from the hardware. The hardware manufacturer writes a driver that interfaces with Windows in a standard way. But, the interface on the hardware-side of the driver is generally propriatary. If you can get the hardware details, you cannot read/write to the hardware directly with a user mode program. You need a kernel mode driver. Writing a Windows driver is not trivial!

    You might have to create a WAV file with the waveform you want to play. Or, if your soundcard/soundchip allows downloadable sound fonts, you can use them. You use MIDI functions to play downloaded sound fonts. In both cases, you will need to create or generate the sound as an array of samples.
    Last edited by DougDbug; 02-14-2006 at 03:47 PM.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    Thanks for the information

    How do i trigger the Midi functions in C++?

    What should i type in sound libraries for C++?

    I'm looking for the sound functions and waveform functions
    in C++ how do i find them in google please what am i looking
    what is the label for this type of programming?

    I thought game programmings triggered the soundchips
    to changes the waveforms,waveshapes,pitch,frequency , volume?


    Calling sound files ,wav files, mp3 files is not like the old atari sound chips programmers did

    The Atari consoles had sound chips on board and the programmer sound trigger the sounds waveforms,waveshapes,sound patches,pitch,frequency,volume
    How do i do this using C++ please?

    Which Soundcard is close to a Atari's soundcard soundchip please? its a 4bit sound chip i think

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    When programming Atari,commodore,Amiga computers they had sound and waveform fucntions to trigger the sound chips and the sound cards

    You could edit,with sound tooles like choose the sound waveform,waveshape,volume,pitch,frequency,envelope how do i do this in C++

    Calling and Linking the sound librarys , API, Direct X files to C++ applications
    is different than the old atari's ,commodore,Amiga programming languages had functions built in for sound and waveform functions

  7. #7
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    it is no different from what you did in the past except the libraries were then built into the language which was different for each platform.
    Now the language is identical on all platforms, only the libraries differ, making things easier overall.

    You could of course go back to doing it in ComodoreBASIC on your Vic20

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    How do i use external sound librarys in C++?

    Don't i just Call the external sound library by its header and link?

    In the External sound library do i set the editting,waveforms,waveshapes in the sound library not in C++?
    Where can i get Tutorial or even the names of external sound librarys i don't even know the names of them where do u find them and how do u use them please? i need lots ot tuturials on sound librarys please?

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    The Atari consoles had sound chips on board and the programmer sound trigger the sounds waveforms,waveshapes,sound patches,pitch,frequency,volume
    How do i do this using C++ please?
    Ah! Another old timer! I wrote an ocean wave sound program for my commodore 64! I was rather surprised that I couldn't do the same with my SoundBlaster card.

    I was able to write a little program that plays MIDI notes, using the information from this site.

    If I can find that program on my computer when I get home, I'll attatch it to this post.

    Don't i just Call the external sound library by its header and link?
    Yes. It's going to be just like using math.h so that you can use the sine / cosine functions in your program. Except, math.h ( or, more properly <cmath> ) is part of the C++ language standard, and included with every C++ compiler. Every platform / compiler has different sound functions, if any.

    ...editting,waveforms,waveshapes in the sound library not in C++?
    I think this is the hard part... I think you have to make an array. Each value in the array represents a "dot" on the graph that "draws" the waveform... i.e. When you read a WAV file, you generally read the values into an array (or into a C++ vector). I don't know what libraries are available to generate the "dots" that would make a particular frequency, overtones, ADSR, etc. I'm pretty sure the standard Windows compiler does NOT have them. Of course, you can use all of the regular C++ functions to do it, but you'd have to figure-out the algorithms yourself.

    Once you have an array that represents the waveform, there are various ways of sending it to your soundcard / soundchip. If you have a good soundcard/soundchip that can store your custom sound. Then, there are functions to "trigger" the "downloaded" sound whenever you want... This is how MIDI works. But, some soundcards/soundchips can only play their built-in MIDI sounds. All PC soundcards/soundchips can play a streaming array. i.e. a WAV file.

    There's a Windows header called mmsystem.h... I think, (MultiMedia System). It still exists in the Windows compiler, but that was before Direct X. I'll look that up when I get home, and I'll see what I have on DirectX. I have the Platform SDK CD at home, and it's got a ship-load of Direct X info. (That info is on MSDN too, if you can find it.)

    ----- UPDATE -----------------------------------------------
    Here's the Direct X SDK It's got lots & lots of good stuff to read. The Platform SDK may include everything, including the Direct X SDK.

    Here's another MSDN Multimedia link.

    I also have a book called Direct X 9 Audio Exposed. 'Couldn't find it on Amazon just now...

    I haven't done anything useful with this information yet... just read and tinkered a little.

    Here's my little MIDI program. 'Turns out it does NOT need mmsystem.h... just windows.h. You can look-up the special MIDI functions in your Visual C++ help files.
    Last edited by DougDbug; 02-16-2006 at 10:23 PM.

  10. #10
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    platform SDK doesn't include DX SDK, saves people not doing DX a few hundred megabytes in downloads.

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Ah! Another old timer! I wrote an ocean wave sound program for my commodore 64! I was rather surprised that I couldn't do the same with my SoundBlaster card.
    Yes you can, or could.

    Use the DSP write port and write the values to the buffer and play them through auto-init DMA. Won't work under XP, but you can still do this in DirectSound by accessing the sound buffers and manipulating them through the use of the write cursor and read cursor.

  12. #12
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    Thanks alot DougDbug for the information

    How did u write a ocean wave sound program ?
    What did the ocean wave sound program do?
    Did u use White/pink noise to make the ocean wave?
    Was the ocean waves programmable and how did u do that please?


    When Importing a .Wav file into C++ does it put the envelope,ADSR,waveform and waveshape values in the array for u ?

    Is there a editting list in C++ in the array?

  13. #13
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    Did u use the Random function to do polyphonic ocean waves?
    like 3 or 5 random functions at different time intervals and time offsets?

  14. #14
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    How did u write a ocean wave sound program ?
    What did the ocean wave sound program do?
    Did u use White/pink noise to make the ocean wave?
    Was the ocean waves programmable and how did u do that please?
    Did u use the Random function to do polyphonic ocean waves?
    Hmmm... As I remember....
    It was done in Commodore BASIC.
    I used the noise-generator (I think it was a white-noise generator), the ADSR, and probably a low-pass filter.
    It seems like there was a way to use the ADSR with a variable low-pass filter, so that the wave would start-out with lots of low-frequency energy, and then finish-off with more of a high-frequency "ssssss" sound.
    I did use the random function.... To vary the time-duration, volume, and the time between waves.
    As I recall there was only one noise generator, so the waves couldn't overlap. But, I could (randomly) start a new wave before the old one completely finished to make it seem like they overlapped.
    I think the Commodore sound was mono, so I may have used two Commodore 64s' to make "stereo" waves.
    Nothing was "programmable". You just started-it and it generated random wave sounds.

    When Importing a .Wav file into C++ does it put the envelope,ADSR,waveform and waveshape values in the array for u ?
    It just puts it into an array.... It's just a series of numbers, individual samples. It doesn't know anything about waveforms, waveshapes, or ADSR...

    A digital image is make-up of pixels, and a digital sound is made-up of samples. Either one can be stored in an array.

    Lets see... Imagine a sine wave... Imagine a sine wave drawn on paper....

    We can "digitize" the wave by putting one dot every degree.... 360 dots to represent one full cycle.

    If we write-down the height of each dot, we have a digital approximation of the wave. This process is called sampling, and each numerical value is called a sample. (This is different from what hip-hop DJs call "samples" & "sampling" )

    Put those numbers into an array, and you can save it as a WAV file! A CD quality wave file has 44,100 samples per second. So, you need an array that holds 44,100 values to store 1 second of sound.

    Here's something that's always bothered me - At a 44.1kHz sample rate, a 20kHz signal only gets two samples per cycle! Those samples won't always land on the positive & negative peaks. You cannot accurately digitize the high-frequency sound with two samples per cycle. Luckily we can't hear the details (if anything at all) at 20kHz. (You have to fairly low-frequencies to get 360 samples per cycle. The exercise to calculate that frequency is "left to the reader". )

    It's just a series of numbers that represent the height (or the position of the speaker) at any instant. It's very low-level information. You would have to analyze the data to find-out the frequency, or average level, or decay, or anything like that.

    And like I said before, I don't know what tools are available to take "higher-level" information (Frequency, ADSR, etc.) and generate the low-level information. (The Commodore sound chip had all that built in.)
    Last edited by DougDbug; 02-17-2006 at 03:13 PM.

  15. #15
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    Thanks alot DougDbug for your information

    random function
    To vary the time-duration

    To vary what time duration of what? there was only one noise generator to produce only one wave

    How did u use the random function to vary the time duration?

    one noise generator, so the waves couldn't overlap. But, I could (randomly) start a new wave before the old one completely finished to make it seem like they overlapped.

    How did u do that with only one noise generator to overlap?

    You have multiple Taps/outputs on the random generator?

    How did u set the multiple outputs on the random generator so u could start a new wave before the old one completed please?

    Or how did u get the random generator so start a new wave before the old one completed please?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What Math would be good to learn to making games??
    By hockey97 in forum Game Programming
    Replies: 17
    Last Post: 09-26-2008, 12:30 PM
  2. sounds?
    By BODYBUILDNERD in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2002, 03:34 PM