Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. waveOutWrite - sound duration

waveOutWrite - sound duration

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialhtmlcomhelp
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Crazy Joe Devola
    wrote on last edited by
    #1

    Hi, I found some code to play sounds. It works well but I can't figure out how to change the duration of the sound, e.g. to play for 100ms, 1000ms, 5000ms etc. Here is the code. I added the DurationMillisecs parameter but I don't know how to utilize it.

    BOOL Test1(long FREQUENCY, long DurationMillisecs)
    {
    // taken from http://www.tmsoft.com/tutorial-sound.html

    #define BUFFERSIZE 4860 // 4k sound buffer

    #define PI 3.14159265358979

    HWAVEOUT hWaveOut; // Handle to sound card output
    WAVEFORMATEX WaveFormat; // The sound format
    WAVEHDR WaveHeader; // WAVE header for our sound data

    char         Data\[BUFFERSIZE\];  // Sound data buffer
    
    HANDLE       Done;              // Event Handle that tells us the sound has finished being played.
                                    // This is a real efficient way to put the program to sleep
                                    // while the sound card is processing the sound buffer
    double x;
    int i;
    
    // \*\* Initialize the sound format we will request from sound card \*\*    
    WaveFormat.wFormatTag = WAVE\_FORMAT\_PCM;     // Uncompressed sound format
    WaveFormat.nChannels = 2;                    // 1=Mono 2=Stereo
    WaveFormat.wBitsPerSample = 8;               // Bits per sample per channel
    WaveFormat.nSamplesPerSec = 11025; //1000\*(DurationMillisecs/1000);           // Sample Per Second
    WaveFormat.nBlockAlign = WaveFormat.nChannels \* WaveFormat.wBitsPerSample / 8;
    WaveFormat.nAvgBytesPerSec = (WaveFormat.nSamplesPerSec \* WaveFormat.nBlockAlign);    
    
    WaveFormat.cbSize = 0;
    
    // \*\* Create our "Sound is Done" event \*\*
    Done = CreateEvent (0, FALSE, FALSE, 0);
    
    // \*\* Open the audio device \*\*
    if (waveOutOpen(&hWaveOut,0,&WaveFormat,(DWORD) Done,0,CALLBACK\_EVENT) != MMSYSERR\_NOERROR) 
    {        
        MessageBox(0,"Sound card cannot be opened.", "error", 0);
        return TRUE;
    }
    
    // \*\* Make the sound buffer \*\*    
    for (i=0; i < BUFFERSIZE; i++)
    {        
        // \*\* Generate the sound wave based on FREQUENCY define
        // \*\* x will have a range of -1 to +1
        x = sin(i\*2.0\*PI\*(FREQUENCY)/(double)WaveFormat.nSamplesPerSec); 
        
        // \*\* scale x to a range of 0-255 (signed char) for 8 bit sound reproduction \*\*
        Data\[i\] = (char)(127\*x+128);
    }
    
    
    // \*\* Create the wave header for our sound buffer \*\*
    WaveHeader.lpDa
    
    enhzflepE 1 Reply Last reply
    0
    • C Crazy Joe Devola

      Hi, I found some code to play sounds. It works well but I can't figure out how to change the duration of the sound, e.g. to play for 100ms, 1000ms, 5000ms etc. Here is the code. I added the DurationMillisecs parameter but I don't know how to utilize it.

      BOOL Test1(long FREQUENCY, long DurationMillisecs)
      {
      // taken from http://www.tmsoft.com/tutorial-sound.html

      #define BUFFERSIZE 4860 // 4k sound buffer

      #define PI 3.14159265358979

      HWAVEOUT hWaveOut; // Handle to sound card output
      WAVEFORMATEX WaveFormat; // The sound format
      WAVEHDR WaveHeader; // WAVE header for our sound data

      char         Data\[BUFFERSIZE\];  // Sound data buffer
      
      HANDLE       Done;              // Event Handle that tells us the sound has finished being played.
                                      // This is a real efficient way to put the program to sleep
                                      // while the sound card is processing the sound buffer
      double x;
      int i;
      
      // \*\* Initialize the sound format we will request from sound card \*\*    
      WaveFormat.wFormatTag = WAVE\_FORMAT\_PCM;     // Uncompressed sound format
      WaveFormat.nChannels = 2;                    // 1=Mono 2=Stereo
      WaveFormat.wBitsPerSample = 8;               // Bits per sample per channel
      WaveFormat.nSamplesPerSec = 11025; //1000\*(DurationMillisecs/1000);           // Sample Per Second
      WaveFormat.nBlockAlign = WaveFormat.nChannels \* WaveFormat.wBitsPerSample / 8;
      WaveFormat.nAvgBytesPerSec = (WaveFormat.nSamplesPerSec \* WaveFormat.nBlockAlign);    
      
      WaveFormat.cbSize = 0;
      
      // \*\* Create our "Sound is Done" event \*\*
      Done = CreateEvent (0, FALSE, FALSE, 0);
      
      // \*\* Open the audio device \*\*
      if (waveOutOpen(&hWaveOut,0,&WaveFormat,(DWORD) Done,0,CALLBACK\_EVENT) != MMSYSERR\_NOERROR) 
      {        
          MessageBox(0,"Sound card cannot be opened.", "error", 0);
          return TRUE;
      }
      
      // \*\* Make the sound buffer \*\*    
      for (i=0; i < BUFFERSIZE; i++)
      {        
          // \*\* Generate the sound wave based on FREQUENCY define
          // \*\* x will have a range of -1 to +1
          x = sin(i\*2.0\*PI\*(FREQUENCY)/(double)WaveFormat.nSamplesPerSec); 
          
          // \*\* scale x to a range of 0-255 (signed char) for 8 bit sound reproduction \*\*
          Data\[i\] = (char)(127\*x+128);
      }
      
      
      // \*\* Create the wave header for our sound buffer \*\*
      WaveHeader.lpDa
      
      enhzflepE Offline
      enhzflepE Offline
      enhzflep
      wrote on last edited by
      #2

      Duration(msec) / 1000.0 = durationSec durationSec * samplesPerSecond = numOfSamplesRequired for (i=0; i<numOfSamplesRequired; i++) { // ** Generate the sound wave based on FREQUENCY define } WaveHeader.dwBufferLength = numOfSamplesRequired * bytesPerSample

      C 1 Reply Last reply
      0
      • enhzflepE enhzflep

        Duration(msec) / 1000.0 = durationSec durationSec * samplesPerSecond = numOfSamplesRequired for (i=0; i<numOfSamplesRequired; i++) { // ** Generate the sound wave based on FREQUENCY define } WaveHeader.dwBufferLength = numOfSamplesRequired * bytesPerSample

        C Offline
        C Offline
        Crazy Joe Devola
        wrote on last edited by
        #3

        Thanks. I didn't quite understand what the code does. It seems to me you mean that I should loop for the number of seconds I want to sound to last. But that creates a non-smooth sounds, like noise. But I understood from your code that by playing with the size of BUFFERSIZE I can make the sound shorter or longer, and that seems to work well. Thank you.

        enhzflepE 1 Reply Last reply
        0
        • C Crazy Joe Devola

          Thanks. I didn't quite understand what the code does. It seems to me you mean that I should loop for the number of seconds I want to sound to last. But that creates a non-smooth sounds, like noise. But I understood from your code that by playing with the size of BUFFERSIZE I can make the sound shorter or longer, and that seems to work well. Thank you.

          enhzflepE Offline
          enhzflepE Offline
          enhzflep
          wrote on last edited by
          #4

          That's okay. I hope you can forgive me for my poorly formatted (i.e - not at all) answer. I had hoped you would understand as much as I do from what I wrote, and it seems to have served its purpose. Have a nice weekend. :)

          C 1 Reply Last reply
          0
          • enhzflepE enhzflep

            That's okay. I hope you can forgive me for my poorly formatted (i.e - not at all) answer. I had hoped you would understand as much as I do from what I wrote, and it seems to have served its purpose. Have a nice weekend. :)

            C Offline
            C Offline
            Crazy Joe Devola
            wrote on last edited by
            #5

            It certainly did serve it's purpose. Thank you, it was helpful.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups