Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
MCIWave.cpp
Go to the documentation of this file.
1 /* Project nGenEx
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: nGenEx.lib
6  FILE: MCIWave.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  MCI Wave Output stuff
13 */
14 
15 #include "MemDebug.h"
16 #include "Types.h"
17 
18 // +----------------------------------------------------------------------+
19 
20 void Print(const char* fmt, ...);
21 
22 // +----------------------------------------------------------------------+
23 
24 const int MCI_MAX_STR = 128;
25 static char ret_str[MCI_MAX_STR];
26 static char err_str[MCI_MAX_STR];
27 static MCIERROR mci_err;
28 static MMRESULT wav_err;
29 extern HWND hwndApp;
30 
31 static int mci_send_string(const char* cmd_str)
32 {
33  mci_err = mciSendString(cmd_str, ret_str, sizeof(ret_str), hwndApp);
34  if (mci_err) {
35  if (mciGetErrorString(mci_err, err_str, sizeof(err_str)))
36  Print("Error (%s): '%s'\n", cmd_str, err_str);
37  else
38  Print("Error (%s): %d - UNKNOWN\n", cmd_str, mci_err);
39  return 0;
40  }
41 
42  return 1;
43 }
44 
45 // +--------------------------------------------------------------------+
46 
47 static void print_wav_error()
48 {
49  waveOutGetErrorText(wav_err, err_str, MCI_MAX_STR);
50  Print(err_str);
51 }
52 
53 // +--------------------------------------------------------------------+
54 
55 int load_wave_file(const char* fname, LPWAVEHDR hdr, LPWAVEFORMATEX format)
56 {
57  HMMIO hmmio; /* file handle for open file */
58  MMCKINFO mmckinfoParent; /* parent chunk information structure */
59  MMCKINFO mmckinfoSubchunk; /* subchunk information structure */
60  DWORD dwFmtSize; /* size of "fmt" chunk */
61  DWORD dwDataSize; /* size of "data" chunk */
62 
63  /*
64  * Open the given file for reading with buffered I/O
65  * using the default internal buffer.
66  */
67  hmmio = mmioOpen((LPSTR) fname, NULL, MMIO_READ | MMIO_ALLOCBUF);
68 
69  if (hmmio == NULL) {
70  Print("load_wave_file(): '%s' - Failed to open file.\n", fname);
71  return 0;
72  }
73 
74  /*
75  * Locate a "RIFF" chunk with a "WAVE" form type
76  * to make sure the file is a WAVE file.
77  */
78  mmckinfoParent.fccType = mmioFOURCC('W', 'A', 'V', 'E');
79  if (mmioDescend(hmmio, (LPMMCKINFO) &mmckinfoParent, NULL, MMIO_FINDRIFF)) {
80  Print("load_wave_file(): '%s' - This is not a WAVE file.\n", fname);
81  mmioClose(hmmio, 0);
82  return 0;
83  }
84 
85  /*
86  * Find the "fmt " chunk (form type "fmt "); it must be
87  * a subchunk of the "RIFF" parent chunk.
88  */
89  mmckinfoSubchunk.ckid = mmioFOURCC('f', 'm', 't', ' ');
90  if (mmioDescend(hmmio, &mmckinfoSubchunk, &mmckinfoParent, MMIO_FINDCHUNK)) {
91  Print("load_wave_file(): '%s' - WAVE file has no \"fmt\" chunk\n", fname);
92  mmioClose(hmmio, 0);
93  return 0;
94  }
95 
96  /*
97  * Get the size of the "fmt " chunk--allocate and lock memory for it.
98  */
99  dwFmtSize = mmckinfoSubchunk.cksize;
100 
101  /* Read the "fmt " chunk. */
102  if (mmioRead(hmmio, (HPSTR) format, dwFmtSize) != (LRESULT)dwFmtSize) {
103  Print("load_wave_file(): '%s' - Failed to read format chunk.\n", fname);
104  mmioClose(hmmio, 0);
105  return 0;
106  }
107 
108  /* Ascend out of the "fmt " subchunk. */
109  mmioAscend(hmmio, &mmckinfoSubchunk, 0);
110 
111  /*
112  * Find the data subchunk. The current file position
113  * should be at the beginning of the data chunk.
114  */
115  mmckinfoSubchunk.ckid = mmioFOURCC('d', 'a', 't', 'a');
116  if (mmioDescend(hmmio, &mmckinfoSubchunk, &mmckinfoParent, MMIO_FINDCHUNK)) {
117  Print("load_wave_file(): '%s' - WAVE file has no data chunk.\n", fname);
118  mmioClose(hmmio, 0);
119  return 0;
120  }
121 
122  /* Get the size of the data subchunk. */
123  dwDataSize = mmckinfoSubchunk.cksize;
124  if (dwDataSize == 0L) {
125  Print("load_wave_file(): '%s' - The data chunk contains no data.\n", fname);
126  mmioClose(hmmio, 0);
127  return 0;
128  }
129 
130  // allocate the data block:
131  hdr->lpData = (LPSTR) new(__FILE__,__LINE__) BYTE[dwDataSize];
132  hdr->dwBufferLength = dwDataSize;
133 
134  /* Read the waveform data subchunk. */
135  if (mmioRead(hmmio, (HPSTR) hdr->lpData, dwDataSize) != (LRESULT)dwDataSize) {
136  Print("load_wave_file(): '%s' - Failed to read data chunk.\n", fname);
137  mmioClose(hmmio, 0);
138  return 0;
139  }
140 
141  /* Close the file. */
142  mmioClose(hmmio, 0);
143 
144  return 1;
145 }
146 
147 // +--------------------------------------------------------------------+
148 
149 void delete_wave_file(LPWAVEHDR hdr, LPWAVEFORMATEX format)
150 {
151  if (hdr) {
152  delete hdr->lpData;
153  hdr->lpData = 0;
154  }
155 }