summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libgme/nes_vrc7_apu.c
blob: 8d3c2e88a66adb2756a497322bd1d43ba66d1be9 (plain)
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

#include "nes_vrc7_apu.h"
#include "blargg_source.h"

int const period = 36; // NES CPU clocks per FM clock

void Vrc7_init( struct Nes_Vrc7_Apu* this )
{
	Synth_init( &this->synth );
	
	OPLL_new ( &this->opll, 3579545, 3579545 / 72 );
    OPLL_reset_patch( &this->opll, OPLL_VRC7_TONE );
	
	this->osc.output = 0;
	this->osc.last_amp = 0;
	this->mask = 0;

	Vrc7_volume( this, (int)FP_ONE_VOLUME );
	Vrc7_reset( this );
}

void Vrc7_reset( struct Nes_Vrc7_Apu* this )
{
	this->addr      = 0;
	this->next_time = 0;
	this->osc.last_amp = 0;

	OPLL_reset (&this->opll);	
	OPLL_setMask(&this->opll, this->mask);
}

void Vrc7_set_rate( struct Nes_Vrc7_Apu* this, int r )
{
	OPLL_set_quality( &this->opll, r < 44100 ? 0 : 1 );
}

void Vrc7_write_reg( struct Nes_Vrc7_Apu* this, int data )
{
	this->addr = data;
}

void Vrc7_run_until( struct Nes_Vrc7_Apu* this, blip_time_t end_time );
void Vrc7_write_data( struct Nes_Vrc7_Apu* this, blip_time_t time, int data )
{	
	if ( time > this->next_time )
		Vrc7_run_until( this, time );

	OPLL_writeIO( &this->opll, 0, this->addr );
	OPLL_writeIO( &this->opll, 1, data );
}

void Vrc7_end_frame( struct Nes_Vrc7_Apu* this, blip_time_t time )
{
	if ( time > this->next_time )
		Vrc7_run_until( this, time );
	
	this->next_time -= time;
	assert( this->next_time >= 0 );
	
	if ( this->osc.output )
		Blip_set_modified( this->osc.output );
}

void Vrc7_run_until( struct Nes_Vrc7_Apu* this, blip_time_t end_time )
{
	require( end_time > this->next_time );

	blip_time_t time = this->next_time;
	OPLL* opll = &this->opll; // cache
	struct Blip_Buffer* const output = this-> osc.output;
	if ( output )
	{
		do
		{
			int amp = OPLL_calc( opll ) << 1;
			int delta = amp - this->osc.last_amp;
			if ( delta )
			{
				this->osc.last_amp = amp;
				Synth_offset_inline( &this->synth, time, delta, output );
			}
			time += period;
		}
		while ( time < end_time );
	}

	this->next_time = time;
}