Skip to content

Commit 58823ff

Browse files
committed
ym3812 code
1 parent 5407a13 commit 58823ff

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

fmtoy_ym3812.c

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <math.h>
2+
#include <stdlib.h>
3+
4+
#include "fmtoy.h"
5+
#include "fmtoy_ym3812.h"
6+
#include "chips/fmopl.h"
7+
8+
static void fmwrite(void *ptr, uint8_t reg, uint8_t val) {
9+
ym3812_write(ptr, 0, reg);
10+
ym3812_write(ptr, 1, val);
11+
}
12+
13+
static int fmtoy_ym3812_init(struct fmtoy *fmtoy, int clock, int sample_rate, struct fmtoy_channel *channel) {
14+
channel->chip->clock = clock;
15+
channel->chip->data = ym3812_init(clock, sample_rate);
16+
fmwrite(channel->chip->data, 0x01, 0x20);
17+
18+
return 0;
19+
}
20+
21+
static int fmtoy_ym3812_destroy(struct fmtoy *fmtoy, struct fmtoy_channel *channel) {
22+
return 0;
23+
}
24+
25+
static void fmtoy_ym3812_program_change(struct fmtoy *fmtoy, uint8_t program, struct fmtoy_channel *channel) {
26+
struct fmtoy_opl_voice *v = &fmtoy->opl_voices[program];
27+
int chan_offsets[] = {
28+
0x00, 0x01, 0x02,
29+
0x08, 0x09, 0x0a,
30+
0x10, 0x11, 0x12,
31+
};
32+
for(int i = 0; i < 9; i++) {
33+
fmwrite(channel->chip->data, 0xc0 + i, v->fb_con);
34+
for(int j = 0; j < 2; j++) {
35+
struct fmtoy_opl_voice_operator *op = &v->operators[j];
36+
fmwrite(channel->chip->data, 0x20 + chan_offsets[i] + j * 3, op->am_vib_eg_ksr_mul);
37+
fmwrite(channel->chip->data, 0x40 + chan_offsets[i] + j * 3, op->ksl_tl);
38+
fmwrite(channel->chip->data, 0x60 + chan_offsets[i] + j * 3, op->ar_dr);
39+
fmwrite(channel->chip->data, 0x80 + chan_offsets[i] + j * 3, op->sl_rr);
40+
fmwrite(channel->chip->data, 0xe0 + chan_offsets[i] + j * 3, op->ws);
41+
}
42+
}
43+
}
44+
45+
static void fmtoy_ym3812_set_pitch(struct fmtoy *fmtoy, int chip_channel, float pitch, struct fmtoy_channel *channel) {
46+
uint8_t octave = (69 + 12 * log2(pitch / 440.0)) / 12 - 1;
47+
uint16_t fnum = (144 * pitch * (1 << 18) / channel->chip->clock) / (1 << (octave - 1));
48+
fmwrite(channel->chip->data, 0xb0 + chip_channel, (channel->chip->channels[chip_channel].on ? 0x20 : 0x00) | octave << 2 | (fnum >> 8 & 0x03));
49+
fmwrite(channel->chip->data, 0xa0 + chip_channel, fnum & 0xff);
50+
}
51+
52+
static void fmtoy_ym3812_pitch_bend(struct fmtoy *fmtoy, uint8_t chip_channel, float pitch, struct fmtoy_channel *channel) {
53+
fmtoy_ym3812_set_pitch(fmtoy, chip_channel, pitch, channel);
54+
}
55+
56+
static void fmtoy_ym3812_note_on(struct fmtoy *fmtoy, uint8_t chip_channel, float pitch, uint8_t velocity, struct fmtoy_channel *channel) {
57+
fmtoy_ym3812_set_pitch(fmtoy, chip_channel, pitch, channel);
58+
}
59+
60+
static void fmtoy_ym3812_note_off(struct fmtoy *fmtoy, uint8_t chip_channel, uint8_t velocity, struct fmtoy_channel *channel) {
61+
fmwrite(channel->chip->data, 0xb0 + chip_channel, 0x00);
62+
}
63+
64+
static void fmtoy_ym3812_render(struct fmtoy *fmtoy, stream_sample_t **buffers, int num_samples, struct fmtoy_channel *channel) {
65+
ym3812_update_one(channel->chip->data, buffers, num_samples);
66+
}
67+
68+
struct fmtoy_chip fmtoy_chip_ym3812 = {
69+
.name = "YM3812",
70+
.init = fmtoy_ym3812_init,
71+
.destroy = fmtoy_ym3812_destroy,
72+
.program_change = fmtoy_ym3812_program_change,
73+
.pitch_bend = fmtoy_ym3812_pitch_bend,
74+
.note_on = fmtoy_ym3812_note_on,
75+
.note_off = fmtoy_ym3812_note_off,
76+
.render = fmtoy_ym3812_render,
77+
.max_poliphony = 9,
78+
};

fmtoy_ym3812.h

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#pragma once
2+
3+
#include "fmtoy.h"
4+
5+
extern struct fmtoy_chip fmtoy_chip_ym3812;

0 commit comments

Comments
 (0)