Click here to Skip to main content
15,904,023 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: fstream read and write Pin
Saurabh.Garg29-Apr-10 19:00
Saurabh.Garg29-Apr-10 19:00 
GeneralRe: fstream read and write Pin
varsha_vm29-Apr-10 20:17
varsha_vm29-Apr-10 20:17 
QuestionRe: fstream read and write Pin
David Crow30-Apr-10 3:25
David Crow30-Apr-10 3:25 
AnswerRe: fstream read and write Pin
varsha_vm2-May-10 18:54
varsha_vm2-May-10 18:54 
Question'Generating code' and stop in building release mode (Visual Studio 2005 ) Pin
cchcc29-Apr-10 16:32
cchcc29-Apr-10 16:32 
AnswerRe: 'Generating code' and stop in building release mode (Visual Studio 2005 ) Pin
Sarath C29-Apr-10 17:51
Sarath C29-Apr-10 17:51 
GeneralRe: 'Generating code' and stop in building release mode (Visual Studio 2005 ) Pin
cchcc29-Apr-10 18:12
cchcc29-Apr-10 18:12 
QuestionwaveOutWrite - sound duration Pin
Crazy Joe Devola29-Apr-10 16:23
Crazy Joe Devola29-Apr-10 16:23 
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.lpData=Data;
    WaveHeader.dwBufferLength=BUFFERSIZE;
    WaveHeader.dwFlags=0;
    WaveHeader.dwLoops=0;
    
    // ** 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;
    }

    // ** 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;

}

void Message(LPCSTR msg)
{
	MessageBox(0, msg, " ",0);

}

AnswerRe: waveOutWrite - sound duration Pin
enhzflep29-Apr-10 17:07
enhzflep29-Apr-10 17:07 
AnswerRe: waveOutWrite - sound duration Pin
Crazy Joe Devola29-Apr-10 18:22
Crazy Joe Devola29-Apr-10 18:22 
GeneralRe: waveOutWrite - sound duration Pin
enhzflep29-Apr-10 21:01
enhzflep29-Apr-10 21:01 
GeneralRe: waveOutWrite - sound duration Pin
Crazy Joe Devola29-Apr-10 21:10
Crazy Joe Devola29-Apr-10 21:10 
Questionnew mfc controls (vs 2010) loose their visual style at ruining time for dialog based applications Pin
m_code29-Apr-10 9:30
m_code29-Apr-10 9:30 
AnswerRe: new mfc controls (vs 2010) loose their visual style at ruining time for dialog based applications Pin
Sarath C29-Apr-10 18:07
Sarath C29-Apr-10 18:07 
QuestionRe: new mfc controls (vs 2010) loose their visual style at ruining time for dialog based applications Pin
m_code29-Apr-10 18:54
m_code29-Apr-10 18:54 
AnswerRe: new mfc controls (vs 2010) loose their visual style at ruining time for dialog based applications Pin
asight19-Aug-11 4:02
asight19-Aug-11 4:02 
QuestionBest way to send mail through MFC Pin
mesajflaviu29-Apr-10 6:34
mesajflaviu29-Apr-10 6:34 
AnswerRe: Best way to send mail through MFC Pin
Joe Woodbury29-Apr-10 6:39
professionalJoe Woodbury29-Apr-10 6:39 
GeneralMessage Removed Pin
29-Apr-10 7:15
mesajflaviu29-Apr-10 7:15 
GeneralRe: Best way to send mail through MFC Pin
Joe Woodbury29-Apr-10 7:17
professionalJoe Woodbury29-Apr-10 7:17 
GeneralRe: Best way to send mail through MFC Pin
mesajflaviu29-Apr-10 7:22
mesajflaviu29-Apr-10 7:22 
AnswerRe: Best way to send mail through MFC Pin
Moak29-Apr-10 9:47
Moak29-Apr-10 9:47 
GeneralRe: Best way to send mail through MFC Pin
mesajflaviu29-Apr-10 20:55
mesajflaviu29-Apr-10 20:55 
GeneralRe: Best way to send mail through MFC Pin
Moak29-Apr-10 21:36
Moak29-Apr-10 21:36 
Questiontry catch exceptions Pin
gmallax29-Apr-10 2:16
gmallax29-Apr-10 2:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.