|
|
1.1 root 1: /*
1.1.1.4 root 2: Hatari - wavFormat.c
3:
4: This file is distributed under the GNU Public License, version 2 or at
5: your option any later version. Read the file gpl.txt for details.
1.1 root 6:
7: WAV File output
8:
1.1.1.6 root 9: As well as YM file output we also have output in .WAV format. These .WAV
10: files can then be run through convertors to any other format, such as MP3.
11: We simply save out the WAVE format headers and then write the sample data
12: (at the current rate of playback) as we build it up each frame. When we stop
13: recording we complete the size information in the headers and close up.
1.1 root 14:
15:
16: RIFF Chunk (12 bytes in length total) Byte Number
17: 0 - 3 "RIFF" (ASCII Characters)
18: 4 - 7 Total Length Of Package To Follow (Binary, little endian)
19: 8 - 12 "WAVE" (ASCII Characters)
20:
21: FORMAT Chunk (24 bytes in length total) Byte Number
22: 0 - 3 "fmt_" (ASCII Characters)
23: 4 - 7 Length Of FORMAT Chunk (Binary, always 0x10)
24: 8 - 9 Always 0x01
25: 10 - 11 Channel Numbers (Always 0x01=Mono, 0x02=Stereo)
26: 12 - 15 Sample Rate (Binary, in Hz)
27: 16 - 19 Bytes Per Second
28: 20 - 21 Bytes Per Sample: 1=8 bit Mono, 2=8 bit Stereo or 16 bit Mono, 4=16 bit Stereo
29: 22 - 23 Bits Per Sample
30:
31: DATA Chunk Byte Number
32: 0 - 3 "data" (ASCII Characters)
33: 4 - 7 Length Of Data To Follow
34: 8 - end Data (Samples)
35: */
1.1.1.9 ! root 36: const char WAVFormat_rcsid[] = "Hatari $Id: wavFormat.c,v 1.14 2007/10/19 21:56:23 eerot Exp $";
1.1 root 37:
1.1.1.3 root 38: #include <SDL_endian.h>
39:
1.1 root 40: #include "main.h"
41: #include "audio.h"
1.1.1.6 root 42: #include "configuration.h"
1.1 root 43: #include "file.h"
1.1.1.6 root 44: #include "log.h"
1.1 root 45: #include "sound.h"
1.1.1.5 root 46: #include "wavFormat.h"
1.1.1.2 root 47:
1.1 root 48:
1.1.1.3 root 49: static FILE *WavFileHndl;
50: static int nWavOutputBytes; /* Number of samples bytes saved */
51: BOOL bRecordingWav = FALSE; /* Is a WAV file open and recording? */
52:
1.1 root 53:
1.1.1.3 root 54: /*-----------------------------------------------------------------------*/
1.1.1.9 ! root 55: /**
! 56: *
! 57: */
1.1.1.3 root 58: BOOL WAVFormat_OpenFile(char *pszWavFileName)
1.1 root 59: {
1.1.1.6 root 60: const Uint32 Blank = 0;
61: const Uint32 FmtLength = SDL_SwapLE32(0x10);
62: const Uint16 SOne = SDL_SwapLE16(0x01);
63: const Uint32 BitsPerSample = SDL_SwapLE32(8);
64: Uint32 SampleLength;
65:
66: /* Set frequency (11Khz, 22Khz or 44Khz) */
67: SampleLength = SDL_SwapLE32(SoundPlayBackFrequencies[ConfigureParams.Sound.nPlaybackQuality]);
68:
69: /* Create our file */
70: WavFileHndl = fopen(pszWavFileName, "wb");
71:
72: if (WavFileHndl != NULL)
73: {
74: /* Create 'RIFF' chunk */
75: fwrite("RIFF", 1, 4, WavFileHndl); /* "RIFF" (ASCII Characters) */
76: fwrite(&Blank, sizeof(Uint32), 1, WavFileHndl); /* Total Length Of Package To Follow (Binary, little endian) */
77: fwrite("WAVE", 1, 4, WavFileHndl); /* "WAVE" (ASCII Characters) */
78:
79: /* Create 'FORMAT' chunk */
80: fwrite("fmt ", 1, 4, WavFileHndl); /* "fmt_" (ASCII Characters) */
81: fwrite(&FmtLength, sizeof(Uint32), 1, WavFileHndl); /* Length Of FORMAT Chunk (Binary, always 0x10) */
82: fwrite(&SOne, sizeof(Uint16), 1, WavFileHndl); /* Always 0x01 */
83: fwrite(&SOne, sizeof(Uint16), 1, WavFileHndl); /* Channel Numbers (Always 0x01=Mono, 0x02=Stereo) */
84: fwrite(&SampleLength, sizeof(Uint32), 1, WavFileHndl); /* Sample Rate (Binary, in Hz) */
85: fwrite(&SampleLength, sizeof(Uint32), 1, WavFileHndl); /* Bytes Per Second */
86: fwrite(&SOne, sizeof(Uint16), 1, WavFileHndl); /* Bytes Per Sample: 1=8 bit Mono, 2=8 bit Stereo or 16 bit Mono, 4=16 bit Stereo */
87: fwrite(&BitsPerSample, sizeof(Uint16), 1, WavFileHndl); /* Bits Per Sample */
88:
89: /* Create 'DATA' chunk */
90: fwrite("data", 1, 4, WavFileHndl); /* "data" (ASCII Characters) */
91: fwrite(&Blank, sizeof(Uint32), 1, WavFileHndl); /* Length Of Data To Follow */
92:
93: nWavOutputBytes = 0;
94: bRecordingWav = TRUE;
95:
96: /* And inform user */
97: Log_AlertDlg(LOG_INFO, "WAV sound data recording has been started.");
98: }
99: else
100: bRecordingWav = FALSE;
1.1 root 101:
1.1.1.6 root 102: /* Ok, or failed? */
103: return bRecordingWav;
1.1 root 104: }
105:
1.1.1.3 root 106:
107: /*-----------------------------------------------------------------------*/
1.1.1.9 ! root 108: /**
! 109: *
! 110: */
! 111: void WAVFormat_CloseFile(void)
1.1 root 112: {
1.1.1.6 root 113: if (bRecordingWav)
114: {
115: Uint32 nWavFileBytes;
116: Uint32 nWavLEOutBytes;
117:
118: /* Update headers with sizes */
119: nWavFileBytes = SDL_SwapLE32((12+24+8+nWavOutputBytes)-8); /* File length, less 8 bytes for 'RIFF' and length */
120: fseek(WavFileHndl, 4, SEEK_SET); /* 'Total Length Of Package' element */
121: fwrite(&nWavFileBytes, sizeof(Uint32), 1, WavFileHndl); /* Total Length Of Package in 'RIFF' chunk */
122:
123: fseek(WavFileHndl, 12+24+4, SEEK_SET); /* 'Length' element */
124: nWavLEOutBytes = SDL_SwapLE32(nWavOutputBytes);
125: fwrite(&nWavLEOutBytes, sizeof(Uint32), 1, WavFileHndl); /* Length Of Data in 'DATA' chunk */
126:
127: /* Close file */
128: fclose(WavFileHndl);
129: WavFileHndl = NULL;
130: bRecordingWav = FALSE;
131:
132: /* And inform user */
133: Log_AlertDlg(LOG_INFO, "WAV Sound data recording has been stopped.");
134: }
1.1 root 135: }
136:
1.1.1.3 root 137:
138: /*-----------------------------------------------------------------------*/
1.1.1.9 ! root 139: /**
! 140: *
! 141: */
1.1.1.7 root 142: void WAVFormat_Update(Sint8 *pSamples, int Index, int Length)
1.1 root 143: {
1.1.1.6 root 144: Sint8 sample;
145: int i;
1.1.1.3 root 146:
1.1.1.6 root 147: if (bRecordingWav)
148: {
149: /* Output, better if did in two section if wrap */
150: for(i = 0; i < Length; i++)
151: {
152: /* Convert sample to 'signed' byte */
153: sample = pSamples[(Index+i)%MIXBUFFER_SIZE] ^ 128;
154: /* And store */
155: fwrite(&sample, sizeof(Sint8), 1, WavFileHndl);
156: }
157:
158: /* Add samples to wav file */
159: nWavOutputBytes += Length;
160: }
1.1 root 161: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.