CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2009
    Posts
    0

    making wave player

    hi all,
    i m new to both codeguru and vcpp...i ws tryin to build a wav player on a code that plays sine waves i found on net....the code is pasted below , the problem is that though the song is being played there is some discontinuity after every buffer is finished....pls tell me why....heres the code...my plan ws to use the time the sound card uses to play the sound to load the next buffer , which i think i hav not very well executed...please help....

    //------------------------CODE--------------------------
    //pls do not the comments in the code(exept this one) for they are from the guy's
    //code to play cosine waves




    /*

    ** Simple Windows Sound Program **

    This program demonstrates how to create and play a simple sound wave
    with your default sound card.

    Make sure to link into user32.lib and winmm.lib during compile.

    Todd Moore
    toddmoore@erols.com
    */


    #include <windows.h>
    #include <math.h>
    #include <stdio.h>
    FILE * pFile;
    long cursor;
    long lSize;
    int pingpong;
    //size_t end;



    //#define FREQUENCY 440 // 440hz = Musical A Note
    #define BUFFERSIZE 32768 // 32k sound buffer
    char Data [BUFFERSIZE];
    char Data1[BUFFERSIZE];


    #define PI 3.14159265358979

    void init ()
    {
    pFile = fopen ("stereo1.wav" , "r");
    fseek (pFile, 0, SEEK_END);
    lSize = ftell (pFile);
    cursor=0;
    for(int i=0;i<BUFFERSIZE;i++)
    Data[i]=Data1[i]=0;

    }
    void readr ()
    {

    fseek ( pFile , cursor , SEEK_SET );
    if(pingpong)
    fread (Data,1,BUFFERSIZE,pFile);
    else
    fread (Data1,1,BUFFERSIZE,pFile);
    cursor=cursor+BUFFERSIZE;
    }


    void Message(LPCSTR message)
    {
    MessageBox(NULL, message, "Sound Error", MB_OK | MB_ICONSTOP);
    }


    // Main entry point for Win32 Program
    int WINAPI WinMain(HINSTANCE hInstance, // handle to current instance
    HINSTANCE hPrevInstance, // handle to previous instance
    LPSTR lpCmdLine, // pointer to command line
    int nCmdShow) // show state of window

    {

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


    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


    // ** 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 = 16; // Bits per sample per channel
    WaveFormat.nSamplesPerSec = 44000; // 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)
    {
    Message("Sound card cannot be opened.");
    return TRUE;
    }


    // ** Create the wave header for our sound buffer **
    WaveHeader.dwBufferLength=BUFFERSIZE;
    WaveHeader.dwFlags=0;
    WaveHeader.dwLoops=0;


    // ** Make the sound buffer **
    init();
    while(cursor<=lSize)
    {
    if(pingpong)
    {
    WaveHeader.lpData=Data;
    pingpong=0;
    }
    else
    {
    WaveHeader.lpData=Data1;
    pingpong=1;
    }


    // ** Prepare the header for playback on sound card **
    if (waveOutPrepareHeader(hWaveOut,&WaveHeader,sizeof(WaveHeader)) != MMSYSERR_NOERROR)
    {
    Message("Error preparing Header!");
    return TRUE;
    }

    // ** Play the sound! **
    ResetEvent(Done); // Reset our Event so it is non-signaled, it will be signaled again with buffer finished

    if (waveOutWrite(hWaveOut,&WaveHeader,sizeof(WaveHeader)) != MMSYSERR_NOERROR)
    {
    Message("Error writing to sound card!");
    return TRUE;
    }

    readr();

    // ** Wait until sound finishes playing
    if (WaitForSingleObject(Done,INFINITE) != WAIT_OBJECT_0)
    {
    Message("Error waiting for sound to finish");
    return TRUE;
    }




    // ** Unprepare our wav header **
    /*if (waveOutUnprepareHeader(hWaveOut,&WaveHeader,sizeof(WaveHeader)) != MMSYSERR_NOERROR)
    {
    Message("Error unpreparing header!");
    return TRUE;
    }*/

    }

    // ** close the wav device **
    if (waveOutClose(hWaveOut) != MMSYSERR_NOERROR)
    {
    Message("Sound card cannot be closed!");
    return TRUE;
    }

    // ** Release our event handle **
    CloseHandle(Done);

    return FALSE;
    }





    //////pardon me if you find this heavily hard coded.....
    ///////////Thank You for the time

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: making wave player

    Welcome to codeguru. Using code tags makes your code more readable.

  3. #3
    Join Date
    Feb 2005
    Posts
    2,160

    Re: making wave player

    Just a wild guess (didn't look through the code--use tags as Skizmo mentioned), but it could be an issue with not enough buffering (2-3 buffers is usually sufficient to prevent underruns) or it may just be a discontinuity at your buffer edges (sine wave ends at say 1/2 pi then begins again at zero--that would cause a "click" in the playback).

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured