wave datei abspielen



  • #include <dsound.h>
    #include <cstdlib>
    #include <mmsystem.h>
    #include <windows.h>
    using namespace std;
    
    LPDIRECTSOUND lpDirectSound;
    LPDIRECTSOUNDBUFFER dsbSound;
    void ReleaseDSound ()
    {
       if (dsbSound != NULL)
       {
          dsbSound->Release();
          dsbSound = NULL;
       }
    
       if (lpDirectSound != NULL)
       {
          lpDirectSound->Release();
          lpDirectSound = NULL;
       }
    }
    
    bool LoadWave (char *filename)
    {
       HMMIO wavefile;
       wavefile = mmioOpen (filename, 0, MMIO_READ | MMIO_ALLOCBUF);
    
       if (wavefile == NULL)
       {
          return (false);
       }
       MMCKINFO parent;
       memset (&parent, 0, sizeof(MMCKINFO));
       parent.fccType = mmioFOURCC ('W', 'A', 'V', 'E');
       mmioDescend (wavefile, &parent, 0, MMIO_FINDRIFF);
       MMCKINFO child;
       memset (&child, 0, sizeof(MMCKINFO));
       child.fccType = mmioFOURCC ('f', 'm', 't', ' ');
       mmioDescend (wavefile, &child, &parent, 0);
       WAVEFORMATEX wavefmt;
       mmioRead (wavefile, (char*)&wavefmt, sizeof(wavefmt));
    
       if(wavefmt.wFormatTag != WAVE_FORMAT_PCM)
       {
          return (false);
       }
       mmioAscend (wavefile, &child, 0);
       child.ckid = mmioFOURCC ('d', 'a', 't', 'a');
       mmioDescend (wavefile, &child, &parent, MMIO_FINDCHUNK);
    //Dann wird ein DirectSoundBuffer erstellt (die Variable dsbSound). Er nimmt den Sound in einem DirectSound-Format auf:
       DSBUFFERDESC bufdesc;
       memset (&bufdesc, 0, sizeof(DSBUFFERDESC));
       bufdesc.dwSize = sizeof(DSBUFFERDESC);
       bufdesc.dwFlags = 0;
       bufdesc.dwBufferBytes = child.cksize;
       bufdesc.lpwfxFormat = &wavefmt;
    
       if((lpDirectSound->CreateSoundBuffer (&bufdesc, &dsbSound, NULL))    != DS_OK)
       {
          return (false);
       }
    //Zum Schluß wird der Buffer mit den Daten gefüllt:
    
       void *write1 = 0, *write2 = 0;
       unsigned long length1,length2;
       dsbSound->Lock (0, child.cksize, &write1, &length1, &write2, &length2, 0);
       if(write1 > 0)
       mmioRead (wavefile, (char*)write1, length1);
       if (write2 > 0)
       mmioRead (wavefile, (char*)write2, length2);
       dsbSound->Unlock (write1, length1, write2, length2);
       mmioClose (wavefile, 0);
       return (true);
    }
    void PlayWave ()
    {
       dsbSound->SetCurrentPosition (0);
       dsbSound->Play (0, 0, 0);
    }
    void StopWave ()
    {
       dsbSound->Stop ();
    }
    
    int main(int argc, char *argv[])
    {
      LoadWave("lollig.wav");
      PlayWave();
      system("PAUSE");	
      return 0;
    }
    

    Irgendwie stürzt das Programm danach ab. -> es wird auch nichts abgespielt..keine fehlermeldung..nichts..ich sehe nur kurz das Consolen Fenster ca 1/2 sekunde lang und das wars



  • Konsole und DirectX?
    Das kommt nicht gut...



  • Wieso?

    Bye, TGGC (Der Held bei Dir!)



  • // CPP FILE
    #include"gidxsound.h"
    
    GidxSound::GidxSound()
    {
    	lpDS  =	NULL;
    	dsrval=	NULL;
    }
    
    GidxSound::~GidxSound()
    {
    	if(lpDS!=NULL)
    	{
    		lpDS->Release();
    		lpDS=NULL;
    	}
    }
    
    bool GidxSound::InitSound(HWND hwnd) 
    {
    	dsrval = DirectSoundCreate(NULL, &lpDS, NULL);
    	if(FAILED(dsrval))
    	{
    		return false;
       	}
    	dsrval = lpDS->SetCooperativeLevel(hwnd, DSSCL_NORMAL);
    	if(FAILED(dsrval))
    	{
    		return false;
    	}
    
    	return TRUE;
    }//InitSound
    
    bool GidxSound::LoadSnd(const char* filename, LPDIRECTSOUNDBUFFER* lpDSB)
    {
       DSBUFFERDESC         dsbd;          
       BYTE                *pDSBuffData;   
       WAVEFORMATEX         waveFormat;    
       DWORD                dwDataLength;  
       PBYTE                pbyWaveDaten;  
       HRESULT              dsrval;        
    
       pbyWaveDaten = NULL;
    
       if(!GidxSound::WaveLaden(filename, &waveFormat, &dwDataLength,&pbyWaveDaten))return false;
    
       ZeroMemory(&dsbd,sizeof(DSBUFFERDESC));
       dsbd.dwSize = sizeof(DSBUFFERDESC);
       dsbd.dwFlags = DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME |
                      DSBCAPS_CTRLFREQUENCY | DSBCAPS_STATIC;
       dsbd.dwBufferBytes = dwDataLength;
       dsbd.lpwfxFormat = &waveFormat;
    
       dsrval = lpDS->CreateSoundBuffer(&dsbd, &(*lpDSB), NULL);
       if (FAILED(dsrval)) 
       {
    		return false;
       }
    
       dsrval = (*lpDSB)->Lock(0,dwDataLength,(LPVOID *)&pDSBuffData,
                               &dwDataLength,NULL,0,0);
       if (FAILED(dsrval))
       {
    		return false;
       }
    
       memcpy(pDSBuffData,pbyWaveDaten,dwDataLength);
       free(pbyWaveDaten);
    
       dsrval = (*lpDSB)->Unlock(pDSBuffData,dwDataLength,NULL,0);
       if (FAILED(dsrval))
       {
    		return false;
       }
    
       return TRUE;
    }//LoadSnd
    
    bool GidxSound::PlaySnd(LPDIRECTSOUNDBUFFER lpDSB, long pan, long vol) 
    {
    	if(pan<-10000 && pan> 10000) pan=0;		
    	if(lpDSB)
    	{							
    		lpDSB->SetPan(pan);
    		lpDSB->SetVolume(vol);
    		lpDSB->Play(0,0,0);
    	}
    	return true;
    } // PlaySnd
    
    void GidxSound::StopSnd(LPDIRECTSOUNDBUFFER lpDSB)
    {
    	lpDSB->Stop();
    }// StopSnd
    
    void GidxSound::Pan(LPDIRECTSOUNDBUFFER lpDSB, long pan)
    {
    	if(pan<-10000 && pan> 10000) pan=0;
    	lpDSB->SetPan(pan);
    }//Pan
    
    bool GidxSound::WaveDaten(HMMIO hmmio, DWORD *pdwLaenge, PBYTE* ppbyWDat)
    {
       MMCKINFO mmWave;
       MMCKINFO mmData;
       MMRESULT mmResult;
       ZeroMemory(&mmWave, sizeof(mmWave));
       ZeroMemory(&mmData, sizeof(mmData));
       mmResult = mmioSeek(hmmio, 0, SEEK_SET);
       mmWave.fccType = mmioStringToFOURCC("WAVE", 0);
       mmData.ckid    = mmioStringToFOURCC("data", 0);
       mmResult = mmioDescend(hmmio,&mmWave,NULL,MMIO_FINDRIFF);
       if(FAILED(mmResult))
       {
    		return FALSE;
       }
       mmResult = mmioDescend(hmmio,&mmData,&mmWave,MMIO_FINDCHUNK);
       if(FAILED(mmResult))
       {
    	   return FALSE;
       }
       *pdwLaenge = mmData.cksize;
       *ppbyWDat = (PBYTE)malloc(*pdwLaenge);
       if(!*ppbyWDat)
       {
    	   return FALSE;
       }
       ZeroMemory(*ppbyWDat,*pdwLaenge);
       mmResult = mmioRead(hmmio,(HPSTR)*ppbyWDat,*pdwLaenge);
       if(FAILED(mmResult))
       {
    	   return FALSE;
       }
       return TRUE;
    }
    
    bool GidxSound::WaveFormat(HMMIO hmmio, WAVEFORMATEX *pWf) 
    {
       MMCKINFO mmWave;
       MMCKINFO mmFmt;
       MMRESULT mmResult;
       ZeroMemory(&mmWave, sizeof(mmWave));
       ZeroMemory(&mmFmt, sizeof(mmFmt));
       mmResult = mmioSeek(hmmio, 0, SEEK_SET);
       mmWave.fccType = mmioStringToFOURCC("WAVE", 0);
       mmFmt.ckid = mmioStringToFOURCC("fmt", 0);
       mmResult = mmioDescend(hmmio,&mmWave,NULL,MMIO_FINDRIFF);
       if(FAILED(mmResult))
       {
    	   return FALSE;
       }
       mmResult = mmioDescend(hmmio,&mmFmt,&mmWave,MMIO_FINDCHUNK);
       if(FAILED(mmResult))
       {
    	   return FALSE;
       }
       mmResult = mmioRead(hmmio,(HPSTR)pWf,sizeof(WAVEFORMATEX));
       if(FAILED(mmResult))
       {
    	   return FALSE;
       }
       return TRUE;
    } 
    
    bool GidxSound::WaveLaden(const char* fileName, WAVEFORMATEX* pWf,DWORD* pdwLaenge,PBYTE* ppbyWDat) 
    {
       HMMIO hmmio;
       bool  result;
       hmmio = mmioOpen((char*)((LPCTSTR)fileName), NULL, MMIO_READ);
       if(!hmmio)
       {
    	   return false;
       }
       result=GidxSound::WaveFormat(hmmio, pWf);
       if(!result)
       {
    	   return false;
       }
       result=GidxSound::WaveDaten(hmmio, pdwLaenge, ppbyWDat);
       if(!result)
       {
    	   return false;
       }
       mmioClose(hmmio, 0);
       return TRUE;
    }
    
    // HEADER FILE
    #pragma comment(lib,"dsound.lib")
    #pragma comment(lib,"winmm.lib")
    
    #include <dsound.h>
    #include <windows.h>
    #include <stdio.h>
    #include <mmsystem.h>
    
    class GidxSound
    {
    private:
    	LPDIRECTSOUND       lpDS;
    	HRESULT				dsrval;
    	bool WaveLaden(const char*,WAVEFORMATEX*,DWORD*,PBYTE*);
    	bool WaveDaten(HMMIO, DWORD*, PBYTE*);
    	bool WaveFormat(HMMIO, WAVEFORMATEX*); 
    public:
    	GidxSound();
    	~GidxSound();
    	bool InitSound(HWND);
    	bool LoadSnd(const char*, LPDIRECTSOUNDBUFFER*);
    	bool PlaySnd(LPDIRECTSOUNDBUFFER,long,long);
    	void StopSnd(LPDIRECTSOUNDBUFFER);
    	void Pan(LPDIRECTSOUNDBUFFER,long);
    };
    

    Schau mal ob du damit was anfangen kannst.



  • hu

    g++.exe main.o -o "sound2.exe" -L"E:/Programme/Dev-Cpp/lib" ../lib/libwinmm.a ../lib/libdsound.a

    main.o(.text+0xa9):main.cpp: undefined reference to `_Z17DirectSoundCreatePK5_GUIDPP12IDirectSoundP8IUnknown@12'

    make.exe: *** [sound2.exe] Error 1

    Ausführung beendet

    cu



  • dxguid kann ja nicht fehlen, das hättest du lange per Boardsuche o.ä. rausgefunden...

    Bye, TGGC (Der Held bei Dir!)



  • hi

    Wie meinst das?

    cu



  • So, wie ich es schreibe.

    Bye, TGGC (Der Held bei Dir!)



  • hu

    dxguid , was ist das?

    cu



  • Bitte lesen. Danke.

    Bye, TGGC (Reden wie die Großen)



  • hu

    ??? machts Spass Leute zu verarschen?

    cu



  • Hast du alle nötigen Libs eingebunden?



  • hu

    die dsound
    und die winmm oder so ähnlich.

    cu





  • hu

    Aber komischerweise wird nix abgespielt.

    EDIT: doch geht nu, thx.

    thx



  • auch dxgiud?



  • hu

    Ne, das funxt leider nicht.

    cu


Log in to reply