forked from derselbst/lazyusf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.c
390 lines (332 loc) · 11.1 KB
/
audio.c
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <math.h>
#include <unistd.h>
#include <byteswap.h>
#ifdef FLAC_SUPPORT
#include <FLAC/stream_encoder.h>
#include <FLAC/metadata.h>
#endif
#ifdef PLAYBACK_SUPPORT
#include <ao/ao.h>
#endif
#include "usf.h"
#include "memory.h"
#include "main.h"
#include "cpu.h"
#include "registers.h"
// AU file header
typedef struct
{
uint32_t magic;
/* magic number */
uint32_t hdr_size;
/* size of this header */
uint32_t data_size;
/* length of data (optional) */
uint32_t encoding;
/* data encoding format */
uint32_t sample_rate;
/* samples per second */
uint32_t channels;
/* number of interleaved channels */
} audioFileHeader;
int fd; /* sound device file descriptor */
int8_t useFlac = 0;
#ifdef FLAC_SUPPORT
FLAC__int32 sampleBuf[16*1024]; // buffer for flac encoder
#define metadataBlocks 2
FLAC__StreamMetadata * metadata[metadataBlocks]; // metadata object for flac
FLAC__StreamMetadata_VorbisComment_Entry entry;
FLAC__StreamEncoder * flacEncoder=NULL; // pointer to flac encoder
#endif // FLAC_SUPPORT
uint32_t SampleRate = 0;
int8_t playingback = 0;
#ifdef PLAYBACK_SUPPORT
ao_device *device;
#endif // PLAYBACK_SUPPORT
void OpenSound(void)
{
// determine samplerate
if (round_frequency)
{
// 20000 < SampleRate < 22500
if (SampleRate<22500 && SampleRate>20000)
{
SampleRate=22050;
}
// 29000 < SampleRate < 35000
else if (SampleRate<35000&& SampleRate>29000)
{
SampleRate=32000;
}
else
{
// calculate a more even freq, algorithm suggested by Chris Moeller
SampleRate = SampleRate+25;
SampleRate -= SampleRate % 50;
}
}
if (!useFlac)
{
fd = open(filename, O_RDWR|O_CREAT,0644);
if (fd < 0)
{
char perrormsg[256];
strcat(perrormsg, "open of ");
strcat(perrormsg, filename);
strcat(perrormsg, " failed");
perror(perrormsg);
StopEmulation();
return;
}
audioFileHeader AUheader;
AUheader.magic=0x2e736e64;
// the au format uses big endian
AUheader.magic=bswap_32(AUheader.magic);
// offset where data starts
AUheader.hdr_size=24;
AUheader.hdr_size=bswap_32(AUheader.hdr_size);
// size of data is unknown
AUheader.data_size=0xffffffff;
// encoding is 16 bit linear pcm
AUheader.encoding=3;
AUheader.encoding=bswap_32(AUheader.encoding);
AUheader.sample_rate=SampleRate;
AUheader.sample_rate=bswap_32(AUheader.sample_rate);
// stereo
AUheader.channels=2;
AUheader.channels=bswap_32(AUheader.channels);
if (write(fd,&AUheader,sizeof(AUheader))<0)
{
DisplayError("An Error occurred when writing the Header for .au file! Exiting...\n");
StopEmulation();
Release_Memory();
exit(3);
}
}
#ifdef FLAC_SUPPORT
else
{
/* allocate the encoder */
if((flacEncoder = FLAC__stream_encoder_new()) == NULL)
{
fprintf(stderr, "ERROR: allocating Flac Encoder. Exiting...\n");
StopEmulation();
Release_Memory();
exit(3);
}
else
{
FLAC__stream_encoder_set_verify(flacEncoder,false);
FLAC__stream_encoder_set_channels(flacEncoder,2);
FLAC__stream_encoder_set_bits_per_sample(flacEncoder,16);
FLAC__stream_encoder_set_sample_rate(flacEncoder,SampleRate);
FLAC__stream_encoder_set_compression_level(flacEncoder,4);
/* set metadata */
if(
(metadata[0] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT)) == NULL ||
(metadata[1] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)) == NULL ||
/* there are many tag (vorbiscomment) functions but these are convenient for this particular use: */
!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "ALBUM", game) ||
!FLAC__metadata_object_vorbiscomment_append_comment(metadata[0], entry, /*copy=*/false) || /* copy=false: let metadata object take control of entry's allocated string */
!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "ALBUMARTIST", artist) ||
!FLAC__metadata_object_vorbiscomment_append_comment(metadata[0], entry, /*copy=*/false) ||
!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "ARTIST", artist) ||
!FLAC__metadata_object_vorbiscomment_append_comment(metadata[0], entry, /*copy=*/false) ||
!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "COPYRIGHT", copyright) ||
!FLAC__metadata_object_vorbiscomment_append_comment(metadata[0], entry, /*copy=*/false) ||
!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "DATE", year) ||
!FLAC__metadata_object_vorbiscomment_append_comment(metadata[0], entry, /*copy=*/false) ||
!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "GENRE", genre) ||
!FLAC__metadata_object_vorbiscomment_append_comment(metadata[0], entry, /*copy=*/false) ||
!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "TITLE", title) ||
!FLAC__metadata_object_vorbiscomment_append_comment(metadata[0], entry, /*copy=*/false)
)
{
fprintf(stderr, "ERROR: out of memory or tag error\n");
}
metadata[1]->length = 1234; /* set the padding length */
FLAC__stream_encoder_set_metadata(flacEncoder, metadata, metadataBlocks);
/* end set metadata */
FLAC__stream_encoder_init_file(flacEncoder,filename,NULL,NULL);
}
}
#endif // FLAC_SUPPORT
#ifdef PLAYBACK_SUPPORT
if(playingback)
{
/* -- Initialize libao -- */
ao_initialize();
/* -- Setup for default driver -- */
int default_driver;
default_driver = ao_default_driver_id();
ao_sample_format format;
memset(&format, 0, sizeof(format));
format.bits = 16;
format.channels = 2;
format.rate = SampleRate;
format.byte_format = AO_FMT_BIG;
/* -- Open driver -- */
device = ao_open_live(default_driver, &format, NULL /* no options */);
if (device == NULL)
{
fprintf(stderr, "Error opening device.\n");
StopEmulation();
Release_Memory();
exit(1);
}
}
#endif // PLAYBACK_SUPPORT
printf("Time [min]:\n");
}
void CloseSound(void)
{
if(!useFlac)
{
close(fd);
}
#ifdef FLAC_SUPPORT
else
{
FLAC__stream_encoder_finish(flacEncoder);
FLAC__metadata_object_delete(metadata[0]);
FLAC__metadata_object_delete(metadata[1]);
FLAC__stream_encoder_delete(flacEncoder);
}
#endif // FLAC_SUPPORT
#ifdef PLAYBACK_SUPPORT
if(playingback)
{
/* -- Close and shutdown libao -- */
ao_close(device);
ao_shutdown();
}
#endif // PLAYBACK_SUPPORT
}
void AddBuffer(unsigned char *buf, unsigned int length)
{
#ifdef FLAC_SUPPORT
int32_t out = 0;
#endif // FLAC_SUPPORT
double vol = 1.0;
if(!cpu_running)
{
return;
}
// fading
if(!(track_time >> (sizeof(uint32_t)*8 -1)) && play_time > track_time)
{
switch (fade_type)
{
case 1:
// linear
vol = 1.0f - (((double)play_time - (double)track_time) / (double)fade_time);
break;
case 2:
// logarithmic
vol = 1.0f - pow(0.1, (1 - (((double)play_time - (double)track_time) / (double)fade_time)) * 1);
break;
case 3:
// sine
vol = 1.0f - sin( (((double)play_time - (double)track_time) / (double)fade_time) * 3.14159265359f / 2 );
break;
default:
// none
// in this case no need to play the fade part
cpu_running = 0;
printf("\n");
return;
break;
}
}
uint32_t i = 0;
for(i = 0; i<length; i+=2)
{
uint8_t byte0 = buf[i];
uint8_t byte1 = buf[i+1];
//merge byte0 and byte1
int16_t n = 0;
n |= byte1 & 0xFF;
n <<= 8;
n |= byte0 & 0xFF;
n *= vol; // multiplier;
// the au format uses big endian words
n=bswap_16(n);
//split n into byte0 and byte1
byte1 = (n >> 8) & 0xFF;
byte0 = n & 0xFF;
//save the new values
buf[i] = byte0;
buf[i+1] = byte1;
}
#ifdef PLAYBACK_SUPPORT
if(playingback)
{
ao_play(device, (char*)buf, length);
}
#endif // PLAYBACK_SUPPORT
#ifdef FLAC_SUPPORT
if(useFlac)
{
for(i = 0; i < length; i=i+2)
{
/*for bigendian samples*/ sampleBuf[out++] = (FLAC__int32)(((FLAC__int16)(FLAC__int8)buf[i] << 8) | (FLAC__int16)buf[i+1]);
// little endian sampleBuf[out++] = (FLAC__int32)(((FLAC__int16)(FLAC__int8)buf[i+1] << 8) | (FLAC__int16)buf[i]);
}
FLAC__stream_encoder_process_interleaved(flacEncoder,sampleBuf,length/4);
// length/4 because: number_of_samples_of_orig_char_array/2 = samples_of_int16_arr, but flac wants number sample per each channel --> length_of_int16_arr / 2
}
else
#endif // FLAC_SUPPORT
{
// write raw pcm to au file
unsigned int status = write(fd, buf, length);
if (status != length)
{
perror("wrote wrong number of bytes\n");
}
}
play_time += (((double)(length >> 2) / (double)SampleRate) * 1000.0);
printf("\r%f",(play_time/60000));
if((!(track_time >> (sizeof(uint32_t)*8 -1))) && (play_time > (track_time + fade_time)))
{
cpu_running = 0;
printf("\n");
}
}
void AiLenChanged(void)
{
int32_t length = 0;
uint32_t address = (AI_DRAM_ADDR_REG & 0x00FFFFF8);
length = AI_LEN_REG & 0x3FFF8;
AddBuffer(RDRAM+address, length);
if(length && !(AI_STATUS_REG&0x80000000))
{
const float VSyncTiming = 789000.0f;
double BytesPerSecond = 48681812.0 / (AI_DACRATE_REG + 1) * 4;
double CountsPerSecond = (double)((((double)VSyncTiming) * (double)60.0)) * 2.0;
double CountsPerByte = (double)CountsPerSecond / (double)BytesPerSecond;
unsigned int IntScheduled = (unsigned int)((double)AI_LEN_REG * CountsPerByte);
ChangeTimer(AiTimer,IntScheduled);
}
if(enableFIFOfull)
{
if(AI_STATUS_REG&0x40000000)
{
AI_STATUS_REG|=0x80000000;
}
}
AI_STATUS_REG|=0x40000000;
}
unsigned int AiReadLength(void)
{
AI_LEN_REG = 0;
return AI_LEN_REG;
}
void AiDacrateChanged(unsigned int value)
{
AI_DACRATE_REG = value;
SampleRate = 48681812 / (AI_DACRATE_REG + 1);
}