Thread: Am I calling this in the wrong order?

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Am I calling this in the wrong order?

    Code:
    Sound::Sound(char* FileName)
    {
    	//Load our Wave File
    	
    	Buffer[4]='\0';
    
    	MusicFile=CreateFile(FileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
    	ReadFile(MusicFile,Buffer,4,&RiffRead,NULL);
    	ReadFile(MusicFile,&LengthOfSound,4,&LengthBuffer,NULL);
    	ReadFile(MusicFile,Buffer,4,&WaveRead,NULL);
    	ReadFile(MusicFile,Buffer,4,&FmtRead,NULL);
    	ReadFile(MusicFile,&LengthOfChunk,4,&LengthOfChunkBuffer,NULL);
    	TEMP=new char[LengthOfChunk];
    	ReadFile(MusicFile,TEMP,LengthOfChunk,&RiffRead,NULL);
    	
    	ReadFile(MusicFile,Buffer,4,&WaveRead,NULL);
    	ReadFile(MusicFile,&LengthOfSoundWave,4,&LengthBufferWave,NULL);
    
    	//allocate enough memory to copy of sound data
    
    	SoundData=new char[LengthOfSoundWave];
    	ReadFile(MusicFile,SoundData,LengthOfSoundWave,&LengthBuffer,NULL);
    
    
    	  WaveHeader=new WAVEHDR [sizeof (WAVEHDR)];
    
    
    	   waveformat.wFormatTag      = WAVE_FORMAT_PCM ;
           waveformat.nChannels       = 1 ;
           waveformat.nSamplesPerSec  = 11025;
           waveformat.nAvgBytesPerSec = 11025;
           waveformat.nBlockAlign     = 1 ;
           waveformat.wBitsPerSample  = 8 ;
           waveformat.cbSize          = 0 ;
    
    	  
    	   waveOutOpen ((HWAVEOUT*)WaveOut, WAVE_MAPPER, &waveformat, NULL, 0, CALLBACK_WINDOW);
    	   
    	  
    	   WaveHeader->lpData=SoundData;
    	   WaveHeader->dwBufferLength=LengthOfSoundWave;
    	   WaveHeader->dwBytesRecorded = 0 ;
           WaveHeader->dwUser          = 0 ;
           WaveHeader->dwFlags         = 0 ;
           WaveHeader->dwLoops         = 1 ;
           WaveHeader->lpNext          = NULL ;
           WaveHeader->reserved        = 0 ;
    
    	   waveOutPrepareHeader (WaveOut, WaveHeader,sizeof (WAVEHDR)) ;
    
    
    	
    	   waveOutWrite (WaveOut, WaveHeader, sizeof (WAVEHDR)) ;
    
    
    
    
    
    }

    It's not producing any sounds.......please help.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    hope some of this might help

    Have you tried
    PlaySound(sFileName,NULL,SND_ASYNC| SND_LOOP| SND_FILENAME);//play sound on loop, no msgs sent no device prep needed

    or

    filling in the flag element of the struct
    WaveHdr1.dwFlags=WHDR_BEGINLOOP|WHDR_ENDLOOP;

    this is how I did this.

    Make sure you use waveOutReset() in between sounds.
    Check for errors in the loading of sounds and devices.

    Code:
    //when you have you sound
    if(MMSYSERR_NOERROR == waveOutOpen(blah,blah,..) ) will generate a MM_WOM_ msg
    
    //In the callback handle these MM_WOM_ msgs
    
    static 	WAVEHDR 			WaveHdr1;//ensure is init to zero ie ZeroMemory()
    	MMRESULT			MMResult;
    
    //*******************************
                     case MM_WOM_OPEN:
    				if(!Sound.SoundBuffer)
        					break;//no sound ready
    
    				WaveHdr1.lpData=Sound.SoundBuffer;
    				WaveHdr1.dwBufferLength=Sound.dwDataSize;
    				WaveHdr1.dwBytesRecorded=0;
    				WaveHdr1.dwUser=0;
    				WaveHdr1.dwFlags=WHDR_BEGINLOOP|WHDR_ENDLOOP;
    				WaveHdr1.dwLoops=dRepeat;
    				WaveHdr1.lpNext=NULL;
    				WaveHdr1.reserved=0;
    				MMResult=waveOutPrepareHeader(hWaveOut,&WaveHdr1,sizeof(WAVEHDR));
    				if(MMResult!=MMSYSERR_NOERROR)
    				{
    					iError=GetLastError();
    					sprintf(sBuffer,"Failed to prepare sound file header with error#%d.",iError);
    					ThreadMessageBox(hWnd,sBuffer,Ex_ERR,MBE);//my messagebox running in its own thread
    					break;
    				}
    				MMResult=waveOutWrite(hWaveOut,&WaveHdr1,sizeof(WAVEHDR));
    				if(MMResult!=MMSYSERR_NOERROR)
    				{
    					iError=GetLastError();
    					sprintf(sBuffer,"Failed to open sound device with error#%d.",iError);
    					ThreadMessageBox(hWnd,sBuffer,Ex_ERR,MBE);
    					break;
    				}
    				return TRUE;
    				break;
    //*******************************
    //*******************************
    		case MM_WOM_DONE:
    				waveOutUnprepareHeader(hWaveOut,&WaveHdr1,sizeof(WAVEHDR));
    				waveOutClose(hWaveOut);
    			return TRUE;
    				break;
    //*******************************
    //*******************************
    		case MM_WOM_CLOSE:
    				return TRUE;
    				break;
    //*******************************
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Code:
    	ReadFile(MusicFile,&LengthOfSoundWave,4,&LengthBufferWave,NULL);
    
    	//allocate enough memory to copy of sound data
    
    	SoundData=new char[LengthOfSoundWave];
    	ReadFile(MusicFile,SoundData,LengthOfSoundWave,&LengthBuffer,NULL);
    Is this a valid memory allocation tecnique? Does it matter if Sound data is a char* or a PBYTE?



    FUNCTIONRETURN=waveOutOpen ((HWAVEOUT*)WaveOut, WAVE_MAPPER, &waveformat, NULL, 0, CALLBACK_WINDOW);

    returns "An attempt was made to load a program with an incorrect format."
    Last edited by incognito; 03-21-2004 at 12:37 PM.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  4. #4
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    When calling WaveOutOpen I get this error.....

    "The process cannot access the file because it is being used by another process. " what does this mean?
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  5. #5
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    Originally posted by incognito
    what does this mean?
    It means that he process cannot access the file because it is being used by another process.
    Another app is reading the file, and Windows won't let you read it. Check that there are no other apps using it, and that you haven't already opened it.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 11-14-2008, 03:52 PM
  2. FFT returning values in wrong order
    By DavidP in forum C# Programming
    Replies: 3
    Last Post: 10-25-2007, 01:15 PM
  3. Problem with ascending order Program
    By cashmerelc in forum C Programming
    Replies: 4
    Last Post: 09-21-2007, 05:36 PM
  4. Printing out in wrong order
    By DarkDot in forum C++ Programming
    Replies: 1
    Last Post: 05-05-2007, 02:56 PM
  5. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM