You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
7.4KB

  1. /*
  2. * PortAudio Portable Real-Time Audio Library
  3. * Latest Version at: http://www.portaudio.com
  4. *
  5. * Copyright (c) 1999-2010 Phil Burk and Ross Bencina
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining
  8. * a copy of this software and associated documentation files
  9. * (the "Software"), to deal in the Software without restriction,
  10. * including without limitation the rights to use, copy, modify, merge,
  11. * publish, distribute, sublicense, and/or sell copies of the Software,
  12. * and to permit persons to whom the Software is furnished to do so,
  13. * subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  22. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  23. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. /*
  27. * The text above constitutes the entire PortAudio license; however,
  28. * the PortAudio community also makes the following non-binding requests:
  29. *
  30. * Any person wishing to distribute modifications to the Software is
  31. * requested to send the modifications to the original developer so that
  32. * they can be incorporated into the canonical version. It is also
  33. * requested that these non-binding requests be included along with the
  34. * license above.
  35. */
  36. /**
  37. * Very simple WAV file writer for saving captured audio.
  38. */
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include "write_wav.h"
  42. /* Write long word data to a little endian format byte array. */
  43. static void WriteLongLE( unsigned char **addrPtr, unsigned long data )
  44. {
  45. unsigned char *addr = *addrPtr;
  46. *addr++ = (unsigned char) data;
  47. *addr++ = (unsigned char) (data>>8);
  48. *addr++ = (unsigned char) (data>>16);
  49. *addr++ = (unsigned char) (data>>24);
  50. *addrPtr = addr;
  51. }
  52. /* Write short word data to a little endian format byte array. */
  53. static void WriteShortLE( unsigned char **addrPtr, unsigned short data )
  54. {
  55. unsigned char *addr = *addrPtr;
  56. *addr++ = (unsigned char) data;
  57. *addr++ = (unsigned char) (data>>8);
  58. *addrPtr = addr;
  59. }
  60. /* Write IFF ChunkType data to a byte array. */
  61. static void WriteChunkType( unsigned char **addrPtr, unsigned long cktyp )
  62. {
  63. unsigned char *addr = *addrPtr;
  64. *addr++ = (unsigned char) (cktyp>>24);
  65. *addr++ = (unsigned char) (cktyp>>16);
  66. *addr++ = (unsigned char) (cktyp>>8);
  67. *addr++ = (unsigned char) cktyp;
  68. *addrPtr = addr;
  69. }
  70. #define WAV_HEADER_SIZE (4 + 4 + 4 + /* RIFF+size+WAVE */ \
  71. 4 + 4 + 16 + /* fmt chunk */ \
  72. 4 + 4 ) /* data chunk */
  73. /*********************************************************************************
  74. * Open named file and write WAV header to the file.
  75. * The header includes the DATA chunk type and size.
  76. * Returns number of bytes written to file or negative error code.
  77. */
  78. long Audio_WAV_OpenWriter( WAV_Writer *writer, const char *fileName, int frameRate, int samplesPerFrame )
  79. {
  80. unsigned int bytesPerSecond;
  81. unsigned char header[ WAV_HEADER_SIZE ];
  82. unsigned char *addr = header;
  83. int numWritten;
  84. writer->dataSize = 0;
  85. writer->dataSizeOffset = 0;
  86. writer->fid = fopen( fileName, "wb" );
  87. if( writer->fid == NULL )
  88. {
  89. return -1;
  90. }
  91. /* Write RIFF header. */
  92. WriteChunkType( &addr, RIFF_ID );
  93. /* Write RIFF size as zero for now. Will patch later. */
  94. WriteLongLE( &addr, 0 );
  95. /* Write WAVE form ID. */
  96. WriteChunkType( &addr, WAVE_ID );
  97. /* Write format chunk based on AudioSample structure. */
  98. WriteChunkType( &addr, FMT_ID );
  99. WriteLongLE( &addr, 16 );
  100. WriteShortLE( &addr, WAVE_FORMAT_PCM );
  101. bytesPerSecond = frameRate * samplesPerFrame * sizeof( short);
  102. WriteShortLE( &addr, (short) samplesPerFrame );
  103. WriteLongLE( &addr, frameRate );
  104. WriteLongLE( &addr, bytesPerSecond );
  105. WriteShortLE( &addr, (short) (samplesPerFrame * sizeof( short)) ); /* bytesPerBlock */
  106. WriteShortLE( &addr, (short) 16 ); /* bits per sample */
  107. /* Write ID and size for 'data' chunk. */
  108. WriteChunkType( &addr, DATA_ID );
  109. /* Save offset so we can patch it later. */
  110. writer->dataSizeOffset = (int) (addr - header);
  111. WriteLongLE( &addr, 0 );
  112. numWritten = fwrite( header, 1, sizeof(header), writer->fid );
  113. if( numWritten != sizeof(header) ) return -1;
  114. return (int) numWritten;
  115. }
  116. /*********************************************************************************
  117. * Write to the data chunk portion of a WAV file.
  118. * Returns bytes written or negative error code.
  119. */
  120. long Audio_WAV_WriteShorts( WAV_Writer *writer,
  121. short *samples,
  122. int numSamples
  123. )
  124. {
  125. unsigned char buffer[2];
  126. unsigned char *bufferPtr;
  127. int i;
  128. short *p = samples;
  129. int numWritten;
  130. int bytesWritten;
  131. if( numSamples <= 0 )
  132. {
  133. return -1;
  134. }
  135. for( i=0; i<numSamples; i++ )
  136. {
  137. bufferPtr = buffer;
  138. WriteShortLE( &bufferPtr, *p++ );
  139. numWritten = fwrite( buffer, 1, sizeof( buffer), writer->fid );
  140. if( numWritten != sizeof(buffer) ) return -1;
  141. }
  142. bytesWritten = numSamples * sizeof(short);
  143. writer->dataSize += bytesWritten;
  144. return (int) bytesWritten;
  145. }
  146. /*********************************************************************************
  147. * Close WAV file.
  148. * Update chunk sizes so it can be read by audio applications.
  149. */
  150. long Audio_WAV_CloseWriter( WAV_Writer *writer )
  151. {
  152. unsigned char buffer[4];
  153. unsigned char *bufferPtr;
  154. int numWritten;
  155. int riffSize;
  156. /* Go back to beginning of file and update DATA size */
  157. int result = fseek( writer->fid, writer->dataSizeOffset, SEEK_SET );
  158. if( result < 0 ) return result;
  159. bufferPtr = buffer;
  160. WriteLongLE( &bufferPtr, writer->dataSize );
  161. numWritten = fwrite( buffer, 1, sizeof( buffer), writer->fid );
  162. if( numWritten != sizeof(buffer) ) return -1;
  163. /* Update RIFF size */
  164. result = fseek( writer->fid, 4, SEEK_SET );
  165. if( result < 0 ) return result;
  166. riffSize = writer->dataSize + (WAV_HEADER_SIZE - 8);
  167. bufferPtr = buffer;
  168. WriteLongLE( &bufferPtr, riffSize );
  169. numWritten = fwrite( buffer, 1, sizeof( buffer), writer->fid );
  170. if( numWritten != sizeof(buffer) ) return -1;
  171. fclose( writer->fid );
  172. writer->fid = NULL;
  173. return writer->dataSize;
  174. }
  175. /*********************************************************************************
  176. * Simple test that write a sawtooth waveform to a file.
  177. */
  178. #if 0
  179. int main( void )
  180. {
  181. int i;
  182. WAV_Writer writer;
  183. int result;
  184. #define NUM_SAMPLES (200)
  185. short data[NUM_SAMPLES];
  186. short saw = 0;
  187. for( i=0; i<NUM_SAMPLES; i++ )
  188. {
  189. data[i] = saw;
  190. saw += 293;
  191. }
  192. result = Audio_WAV_OpenWriter( &writer, "rendered_midi.wav", 44100, 1 );
  193. if( result < 0 ) goto error;
  194. for( i=0; i<15; i++ )
  195. {
  196. result = Audio_WAV_WriteShorts( &writer, data, NUM_SAMPLES );
  197. if( result < 0 ) goto error;
  198. }
  199. result = Audio_WAV_CloseWriter( &writer );
  200. if( result < 0 ) goto error;
  201. return 0;
  202. error:
  203. printf("ERROR: result = %d\n", result );
  204. return result;
  205. }
  206. #endif