Waitrud Weber’s blog

things and reminders for memories

sounds: windows-make: The program is compiled well and does not open the device in source code files but does it in a header file.

*
The program is compiled well and does not open the device in source code files but does it in a header file.

 

wavewriter.cpp  Sat Sep 17 15:45:43 2022

  1 :#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers
  2 :// Windows Header Files
  3 :#include 
  4 :// C RunTime Header Files
  5 :#include 
  6 :#include 
  7 :#include 
  8 :#include 
  9 :
 10 :#include 
 11 :#include 
 12 :#include 
 13 :
 14 :#include "wavewriter.h"
 15 :
 16 :
 17 ://#define MAX_THREADS 1
 18 ://#define BUF_SIZE 255
 19 :
 20 :// Global variables.
 21 :
 22 :HANDLE hData  = NULL;  // handle of waveform data memory
 23 :HPSTR  lpData = NULL;  // pointer to waveform data memory
 24 :WAVEFORMAT waveformat;
 25 :
 26 :HWND hwndApp;
 27 :
 28 :void WriteWaveData(void);
 29 :
 30 :void WriteWaveData(void)
 31 :{
 32 :    HWAVEOUT    hWaveOut;
 33 :    HGLOBAL     hWaveHdr;
 34 :    LPWAVEHDR   lpWaveHdr;
 35 :    HMMIO       hmmio;
 36 :    UINT        wResult;
 37 :    HANDLE      hFormat;
 38 :    WAVEFORMAT  *pFormat;
 39 :    DWORD       dwDataSize;
 40 :
 41 :    // Open a waveform device for output using window callback.
 42 :
 43 :	pFormat = &waveformat;
 44 :
 45 :	// sounds-011.h is succeed
 46 :	// if (waveOutOpen(&m_waveOut, WAVE_MAPPER, &m_waveFormat, (DWORD) m_done, 0, CALLBACK_EVENT) != MMSYSERR_NOERROR) 
 47 :    if (waveOutOpen((LPHWAVEOUT)&hWaveOut, WAVE_MAPPER,
 48 ://                    (LPWAVEFORMAT)pFormat,
 49 :                    (LPWAVEFORMATEX)pFormat,
 50 :                    (LONG)hwndApp, 0L, CALLBACK_WINDOW))
 51 :    {
 52 :        MessageBox(hwndApp,
 53 :                   "Failed to open waveform output device.",
 54 :                   NULL, MB_OK | MB_ICONEXCLAMATION);
 55 :        LocalUnlock(hFormat);
 56 :        LocalFree(hFormat);
 57 :        mmioClose(hmmio, 0);
 58 :        return;
 59 :    }
 60 :
 61 :    // Allocate and lock memory for the waveform data.
 62 :
 63 :    hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, dwDataSize );
 64 :    if (!hData)
 65 :    {
 66 :        MessageBox(hwndApp, "Out of memory.",
 67 :                   NULL, MB_OK | MB_ICONEXCLAMATION);
 68 :        mmioClose(hmmio, 0);
 69 :        return;
 70 :    }
 71 ://    if ((lpData = GlobalLock(hData)) == NULL)
 72 :    if ((lpData = (HPSTR)GlobalLock(hData)) == NULL)
 73 :    {
 74 :        MessageBox(hwndApp, "Failed to lock memory for data chunk.",
 75 :                   NULL, MB_OK | MB_ICONEXCLAMATION);
 76 :        GlobalFree(hData);
 77 :        mmioClose(hmmio, 0);
 78 :        return;
 79 :    }
 80 :
 81 :    // Read the waveform data subchunk.
 82 :	if ((hmmio = mmioOpen("..\\sounds_resource\\thunder.wav", NULL, 
 83 :		MMIO_WRITE)) != NULL) {
 84 :		// File opened successfully. 
 85 :		MessageBox(hwndApp, "Failed to read data chunk.",
 86 :		NULL, MB_OK | MB_ICONEXCLAMATION);
 87 :	} else { 
 88 :		// File opened successfully. 
 89 :		MessageBox(hwndApp, "Failed to read data chunk.",
 90 :		NULL, MB_OK | MB_ICONEXCLAMATION);
 91 :	}
 92 :
 93 :    if(mmioRead(hmmio, (HPSTR) lpData, dwDataSize) != (LRESULT)dwDataSize)
 94 :    {
 95 :        MessageBox(hwndApp, "Failed to read data chunk.",
 96 :                   NULL, MB_OK | MB_ICONEXCLAMATION);
 97 :        GlobalUnlock(hData);
 98 :        GlobalFree(hData);
 99 :        mmioClose(hmmio, 0);
100 :        return;
101 :    }
102 :
103 :    // Allocate and lock memory for the header.
104 :
105 :    hWaveHdr = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE,
106 :        (DWORD) sizeof(WAVEHDR));
107 :    if (hWaveHdr == NULL)
108 :    {
109 :        GlobalUnlock(hData);
110 :        GlobalFree(hData);
111 :        MessageBox(hwndApp, "Not enough memory for header.",
112 :            NULL, MB_OK | MB_ICONEXCLAMATION);
113 :        return;
114 :    }
115 :
116 :    lpWaveHdr = (LPWAVEHDR) GlobalLock(hWaveHdr);
117 :    if (lpWaveHdr == NULL)
118 :    {
119 :        GlobalUnlock(hData);
120 :        GlobalFree(hData);
121 :        MessageBox(hwndApp,
122 :            "Failed to lock memory for header.",
123 :            NULL, MB_OK | MB_ICONEXCLAMATION);
124 :        return;
125 :    }
126 :
127 :    // After allocation, set up and prepare header.
128 :
129 :    lpWaveHdr->lpData = lpData;
130 :    lpWaveHdr->dwBufferLength = dwDataSize;
131 :    lpWaveHdr->dwFlags = 0L;
132 :    lpWaveHdr->dwLoops = 0L;
133 :    waveOutPrepareHeader(hWaveOut, lpWaveHdr, sizeof(WAVEHDR));
134 :
135 :	if ((hmmio = mmioOpen(".\\thunder-write-001.wav", NULL, 
136 :		MMIO_WRITE)) != NULL) {
137 :		// File opened successfully. 
138 :		MessageBox(hwndApp, "Failed to read data chunk.",
139 :		NULL, MB_OK | MB_ICONEXCLAMATION);
140 :	} else { 
141 :		// File opened successfully. 
142 :		MessageBox(hwndApp, "Failed to read data chunk.",
143 :		NULL, MB_OK | MB_ICONEXCLAMATION);
144 :	}
145 :
146 :
147 :    // Now the data block can be sent to the output device. The
148 :    // waveOutWrite function returns immediately and waveform
149 :    // data is sent to the output device in the background.
150 :
151 :    wResult = waveOutWrite(hWaveOut, lpWaveHdr, sizeof(WAVEHDR));
152 :    if (wResult != 0)
153 :    {
154 :        waveOutUnprepareHeader(hWaveOut, lpWaveHdr,
155 :                               sizeof(WAVEHDR));
156 :        GlobalUnlock( hData);
157 :        GlobalFree(hData);
158 :        MessageBox(hwndApp, "Failed to write block to device",
159 :                   NULL, MB_OK | MB_ICONEXCLAMATION);
160 :        return;
161 :    }
162 :}
163 :
164 :