| 1 |
/*============================================================ |
| 2 |
|
| 3 |
Test code for emu2413.c |
| 4 |
|
| 5 |
Write 2 seconds of the piano tone into temp.wav |
| 6 |
|
| 7 |
gcc -Wall -lm sample2413.c emu2413.c |
| 8 |
(The author had tried to compile on Solaris7 with gcc 2.8.1) |
| 9 |
|
| 10 |
=============================================================*/ |
| 11 |
#include "emu2413.h" |
| 12 |
#include <stdio.h> |
| 13 |
#include <time.h> |
| 14 |
|
| 15 |
/* |
| 16 |
* Standard clock = MSX clock |
| 17 |
*/ |
| 18 |
#define MSX_CLK 3579545 |
| 19 |
|
| 20 |
#define SAMPLERATE 44100 |
| 21 |
#define DATALENGTH (SAMPLERATE * 8) |
| 22 |
|
| 23 |
static void WORD(char *buf, uint32_t data) { |
| 24 |
|
| 25 |
buf[0] = data & 0xff; |
| 26 |
buf[1] = (data & 0xff00) >> 8; |
| 27 |
} |
| 28 |
|
| 29 |
static void DWORD(char *buf, uint32_t data) { |
| 30 |
|
| 31 |
buf[0] = data & 0xff; |
| 32 |
buf[1] = (data & 0xff00) >> 8; |
| 33 |
buf[2] = (data & 0xff0000) >> 16; |
| 34 |
buf[3] = (data & 0xff000000) >> 24; |
| 35 |
} |
| 36 |
|
| 37 |
static void chunkID(char *buf, char id[4]) { |
| 38 |
|
| 39 |
buf[0] = id[0]; |
| 40 |
buf[1] = id[1]; |
| 41 |
buf[2] = id[2]; |
| 42 |
buf[3] = id[3]; |
| 43 |
} |
| 44 |
|
| 45 |
int main(void) { |
| 46 |
|
| 47 |
static char wave[DATALENGTH * 2]; |
| 48 |
char filename[16] = "temp.wav"; |
| 49 |
char header[46]; |
| 50 |
int i; |
| 51 |
clock_t start, finish; |
| 52 |
|
| 53 |
FILE *fp; |
| 54 |
OPLL *opll; |
| 55 |
|
| 56 |
/* |
| 57 |
* Create WAVE header |
| 58 |
*/ |
| 59 |
chunkID(header, "RIFF"); |
| 60 |
DWORD(header + 4, DATALENGTH * 2 + 36); |
| 61 |
chunkID(header + 8, "WAVE"); |
| 62 |
chunkID(header + 12, "fmt "); |
| 63 |
DWORD(header + 16, 16); |
| 64 |
WORD(header + 20, 1); /* WAVE_FORMAT_PCM */ |
| 65 |
WORD(header + 22, 1); /* channel 1=mono,2=stereo */ |
| 66 |
DWORD(header + 24, SAMPLERATE); /* samplesPerSec */ |
| 67 |
DWORD(header + 28, 2 * SAMPLERATE); /* bytesPerSec */ |
| 68 |
WORD(header + 32, 2); /* blockSize */ |
| 69 |
WORD(header + 34, 16); /* bitsPerSample */ |
| 70 |
chunkID(header + 36, "data"); |
| 71 |
DWORD(header + 40, 2 * DATALENGTH); |
| 72 |
|
| 73 |
opll = OPLL_new(MSX_CLK, SAMPLERATE); |
| 74 |
OPLL_reset(opll); |
| 75 |
OPLL_writeReg(opll, 0x30, 0x30); /* select PIANO Voice to ch1. */ |
| 76 |
OPLL_writeReg(opll, 0x10, 0x80); /* set F-Number(L). */ |
| 77 |
OPLL_writeReg(opll, 0x20, 0x15); /* set BLK & F-Number(H) and |
| 78 |
* keyon. */ |
| 79 |
|
| 80 |
start = clock(); |
| 81 |
|
| 82 |
i = 0; |
| 83 |
|
| 84 |
for (i = 0; i < DATALENGTH; i++) { |
| 85 |
WORD(wave + i * 2, OPLL_calc(opll)); |
| 86 |
} |
| 87 |
|
| 88 |
finish = clock(); |
| 89 |
OPLL_delete(opll); |
| 90 |
|
| 91 |
printf("It has been %f sec to calc %d waves.\n", (double)(finish - start) / CLOCKS_PER_SEC, DATALENGTH); |
| 92 |
printf("%f times faster than real YM2413.\n", |
| 93 |
((double)DATALENGTH / SAMPLERATE) / ((double)(finish - start) / CLOCKS_PER_SEC)); |
| 94 |
|
| 95 |
fp = fopen(filename, "wb"); |
| 96 |
|
| 97 |
if (fp == NULL) |
| 98 |
return 1; |
| 99 |
|
| 100 |
fwrite(header, 46, 1, fp); |
| 101 |
fwrite(wave, DATALENGTH, 2, fp); |
| 102 |
|
| 103 |
fclose(fp); |
| 104 |
|
| 105 |
printf("Wrote : %s\n", filename); |
| 106 |
|
| 107 |
return 0; |
| 108 |
} |