-
Notifications
You must be signed in to change notification settings - Fork 10
/
wave.cpp
131 lines (105 loc) · 2.55 KB
/
wave.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "types.h"
#include "fceu.h"
#include "driver.h"
#include "sound.h"
#include "wave.h"
#include <cstdio>
#include <cstdlib>
static FILE *soundlog=0;
static long wsize;
/* Checking whether the file exists before wiping it out is left up to the
reader..err...I mean, the driver code, if it feels so inclined(I don't feel
so).
*/
void FCEU_WriteWaveData(int32 *Buffer, int Count)
{
//mbg merge 7/17/06 changed to alloca
//int16 temp[Count]; /* Yay. Is this the first use of this "feature" of C in FCE Ultra? */
int16 *temp = (int16*)alloca(Count*2);
int16 *dest;
int x;
#ifndef __WIN_DRIVER__
if(!soundlog) return;
#else
if(!soundlog && !FCEUI_AviIsRecording()) return;
#endif
dest=temp;
x=Count;
//mbg 7/28/06 - we appear to be guaranteeing little endian
while(x--)
{
int16 tmp=*Buffer;
*(uint8 *)dest=(((uint16)tmp)&255);
*(((uint8 *)dest)+1)=(((uint16)tmp)>>8);
dest++;
Buffer++;
}
if(soundlog)
wsize+=fwrite(temp,1,Count*sizeof(int16),soundlog);
#ifdef __WIN_DRIVER__
if(FCEUI_AviIsRecording())
{
FCEUI_AviSoundUpdate((void*)temp, Count);
}
#endif
}
int FCEUI_EndWaveRecord()
{
long s;
if(!soundlog) return 0;
s=ftell(soundlog)-8;
fseek(soundlog,4,SEEK_SET);
fputc(s&0xFF,soundlog);
fputc((s>>8)&0xFF,soundlog);
fputc((s>>16)&0xFF,soundlog);
fputc((s>>24)&0xFF,soundlog);
fseek(soundlog,0x28,SEEK_SET);
s=wsize;
fputc(s&0xFF,soundlog);
fputc((s>>8)&0xFF,soundlog);
fputc((s>>16)&0xFF,soundlog);
fputc((s>>24)&0xFF,soundlog);
fclose(soundlog);
soundlog=0;
return 1;
}
bool FCEUI_BeginWaveRecord(const char *fn)
{
int r;
if(!(soundlog=FCEUD_UTF8fopen(fn,"wb")))
return false;
wsize=0;
/* Write the header. */
fputs("RIFF",soundlog);
fseek(soundlog,4,SEEK_CUR); // Skip size
fputs("WAVEfmt ",soundlog);
fputc(0x10,soundlog);
fputc(0,soundlog);
fputc(0,soundlog);
fputc(0,soundlog);
fputc(1,soundlog); // PCM
fputc(0,soundlog);
fputc(1,soundlog); // Monophonic
fputc(0,soundlog);
r=FSettings.SndRate;
fputc(r&0xFF,soundlog);
fputc((r>>8)&0xFF,soundlog);
fputc((r>>16)&0xFF,soundlog);
fputc((r>>24)&0xFF,soundlog);
r<<=1;
fputc(r&0xFF,soundlog);
fputc((r>>8)&0xFF,soundlog);
fputc((r>>16)&0xFF,soundlog);
fputc((r>>24)&0xFF,soundlog);
fputc(2,soundlog);
fputc(0,soundlog);
fputc(16,soundlog);
fputc(0,soundlog);
fputs("data",soundlog);
fseek(soundlog,4,SEEK_CUR);
return true;
}
bool FCEUI_WaveRecordRunning(void)
{
return (soundlog != NULL);
}