-
Notifications
You must be signed in to change notification settings - Fork 0
/
PSG.c
170 lines (153 loc) · 5.13 KB
/
PSG.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
#include <cx16.h>
#include "PSG.h"
#include "timer.h"
int tunedNotes[] = {
// C2 , Cs2 , d2 , ds2 , e2 , f2
175 , 186 , 197 , 208 , 221 , 234 ,
// fs2 , g2 , gs2 , a2 , as2 , b2
248 , 263 , 278 , 295 , 312 , 331 ,
// C3 , Cs3 , d3 , ds3 , e3 , f3
351 , 372 , 394 , 417 , 442 , 468 ,
// fs3 , g3 , gs3 , a3 , as3 , b3
496 , 526 , 557 , 590 , 625 , 662 ,
// c4 , cs4 , d4 , ds4 , e4 , f4
702 , 744 , 788 , 835 , 884 , 937 ,
// fs4 , g4 , gs4 , a4 , as4 , b4
993 , 1052 , 1114 , 1181 , 1251 , 1325,
// c5 , cs5 , d5 , ds5 , e5 , f5
1404 , 1488 , 1576 , 1670 , 1769 , 1875,
// fs5 , g5 , gs5 , a5 , as5 , b5
1986 , 2104 , 2229 , 2362 , 2502 , 2651,
// c6 , cs6 , d6 , ds6 , e6 , f6
2809 , 2976 , 3153 , 3340 , 3539 , 3750
};
int i;
int getTunedNote( unsigned index )
{
return tunedNotes[ index ];
}
void runVoice( unsigned voiceNumber, Voice* voice )
{
VERA.control = 0; // port 0
VERA.address = PSG_ADDRESS_VOICE(voiceNumber);
VERA.address_hi = VERA_INC_1 + 1; // from cx16.h
VERA.data0 = voice->frequency & 0xff;
VERA.data0 = voice->frequency >> 8;
VERA.data0 = (3<<6) + voice->volume; //voice->channel + voice->volume;
VERA.data0 = voice->waveform + voice->pulseWidth;
}
void setVolume( unsigned voiceNumber, Voice* voice )
{
VERA.control = 0;
VERA.address = PSG_ADDRESS_VOICE(voiceNumber) + 2; // channel + volume
VERA.address_hi = VERA_INC_1 + 1; // from cx16.h
VERA.data0 = (3<<6) + voice->volume; //voice->channel + voice->volume;
}
#define HI_RES(x) (x << 10)
#define HI_RES2(x) (x << 9)
#define LO_RES(x) (x >> 10)
void runVoiceWithEnvelope( unsigned voiceNumber, Voice* voice )
{
unsigned maxVol = HI_RES(voice->volume);
unsigned halfMaxVol = HI_RES2(voice->volume);
unsigned curVolPercent = 0;
unsigned deltaPercent;
voice->volume = 0;
runVoice(voiceNumber, voice);
//
// Attack rise is 0 to volume setting.
//
deltaPercent = maxVol / (1+ADSR_ENVELOPE(voiceNumber)->attack);
for(i=0; i<=ADSR_ENVELOPE(voiceNumber)->attack; ++i)
{
curVolPercent += deltaPercent;
// cprintf("att ----- %05u %05u %05u\r", deltaPercent, curVolPercent, voice->volume);
voice->volume = LO_RES(curVolPercent);
setVolume( voiceNumber, voice );
pause_jiffies(1);
}
//
// What is the decay rate?? half volume??
//
deltaPercent = halfMaxVol / (1+ADSR_ENVELOPE(voiceNumber)->decay);
for(i=0; i<=ADSR_ENVELOPE(voiceNumber)->decay; ++i)
{
curVolPercent -= deltaPercent;
// cprintf("dec ----- %05u %05u %05u\r", deltaPercent, curVolPercent, voice->volume);
voice->volume = LO_RES(curVolPercent);
setVolume( voiceNumber, voice );
pause_jiffies(1);
}
//
// Sustain is pure time.
//
// cprintf("sus ----- ----- ----- %05u\r", voice->volume);
pause_jiffies( ADSR_ENVELOPE(voiceNumber)->sustain );
//
// And release is curVolPercent down to zero.
//
deltaPercent = halfMaxVol / (1+ADSR_ENVELOPE(voiceNumber)->release);
for(i=0; i<ADSR_ENVELOPE(voiceNumber)->release; ++i)
{
curVolPercent -= deltaPercent;
// cprintf("rel ----- %05u %05u %05u\r", deltaPercent, curVolPercent, voice->volume);
voice->volume = LO_RES(curVolPercent);
setVolume( voiceNumber, voice );
pause_jiffies(1);
}
voice->volume = PSG_VOLUME_OFF;
setVolume( voiceNumber, voice );
}
void bang( unsigned frequency )
{
Voice voice;
voice.frequency = frequency;
voice.channel = PSG_CHANNEL_BOTH;
voice.volume = PSG_VOLUME_KNOB_10;
voice.waveform = PSG_WAVE_NOISE;
voice.pulseWidth = 0;
ADSR_ENVELOPE(1)->attack = 0;
ADSR_ENVELOPE(1)->decay = 10;
ADSR_ENVELOPE(1)->sustain = 0;
ADSR_ENVELOPE(1)->release = 10;
runVoiceWithEnvelope( 1, &voice );
}
void ping( unsigned frequency )
{
Voice voice;
voice.frequency = frequency;
voice.channel = PSG_CHANNEL_BOTH;
voice.volume = PSG_VOLUME_KNOB_10;
voice.waveform = PSG_WAVE_PULSE;
voice.pulseWidth = PSG_PULSE_SQUARE;
ADSR_ENVELOPE(1)->attack = 0;
ADSR_ENVELOPE(1)->decay = 10;
ADSR_ENVELOPE(1)->sustain = 0;
ADSR_ENVELOPE(1)->release = 10;
runVoiceWithEnvelope( 1, &voice );
}
void pluck( unsigned frequency )
{
Voice voice;
voice.frequency = frequency;
voice.channel = PSG_CHANNEL_BOTH;
voice.volume = PSG_VOLUME_KNOB_10;
voice.waveform = PSG_WAVE_SAWTOOTH;
voice.pulseWidth = PSG_PULSE_SQUARE;
ADSR_ENVELOPE(1)->attack = 0;
ADSR_ENVELOPE(1)->decay = 10;
ADSR_ENVELOPE(1)->sustain = 0;
ADSR_ENVELOPE(1)->release = 10;
runVoiceWithEnvelope( 1, &voice );
}
void waves(unsigned frequency)
{
Voice voice;
voice.frequency = frequency;
voice.channel = PSG_CHANNEL_BOTH;
voice.volume = PSG_VOLUME_KNOB_6;
voice.waveform = PSG_WAVE_NOISE;
runVoice( 1, &voice );
//voice.frequency = frequency + 8000;
//runVoice( 2, &voice );
}