Skip to content

Commit

Permalink
Improved audio quality
Browse files Browse the repository at this point in the history
  • Loading branch information
MCJack123 committed May 19, 2022
1 parent 9fa0ea7 commit 0e7b5ff
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 36 deletions.
28 changes: 18 additions & 10 deletions PSG.X/main.s
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//#include <xc.inc>

PROCESSOR 16LF1613

CONFIG FOSC = INTOSC
Expand Down Expand Up @@ -30,6 +28,7 @@ CONFIG WDTCCS = SWC
; - 0x073: increment low
; - 0x074: position high
; - 0x075: position low
; - 0x076: volume adjustment to center
; - 0x078: square duty cycle
; general purpose memory:
; - 0x020-0x025: temporary storage for frequency multiplication: product
Expand Down Expand Up @@ -93,6 +92,11 @@ setVolume:
lslf 0x71
movf 0x0E, 0
iorwf 0x71
; set adjustment
movlw 0x7F
movwf 0x76
lsrf 0x71, 0
subwf 0x76
goto done

high_bits:
Expand Down Expand Up @@ -226,7 +230,7 @@ _main:
movwf 0x26
movlw 0
movwf 0x27
movlw 145
movlw 147
movwf 0x28
; Set up I/O pins
movlb 3
Expand Down Expand Up @@ -258,10 +262,10 @@ _main:
; others so that they all take the same amount of time. This will allow us
; to use the instruction count as a stable clock for sample timings.
; Base clock time: 22 clocks
; Base clock time + scaling: 59 clocks
; Base clock time + scaling: 60 clocks
; Clock time for each type: 10 clocks
; Clock time for each type + scaling: 47 clocks
; Total clock time required: 69 clocks
; Clock time for each type + scaling: 48 clocks
; Total clock time required: 70 clocks
loop:
; Check volume + frequency for all 0s: 11 clocks
movf 0x71
Expand Down Expand Up @@ -296,17 +300,19 @@ ok2:

none:
; Since there's no output, we can ignore clock timings and just loop back.
clrf 0x19 ; reset DAC output
movlw 0x7f
movwf 0x19 ; reset DAC output
goto loop

square:
; Compare high position with duty cycle: 7 clocks
; Compare high position with duty cycle: 8 clocks
movf 0x78, 0
subwf 0x74, 0
movf 0x03, 0
andlw 0x01
addlw 0xFF ; 0 => 255, 1 => 0 + carry
andwf 0x71, 0 ; AND with volume
addwf 0x76, 0 ; add adjustment
movwf 0x19 ; write out
; Filler: 38 clocks
; Since the wait time is so high, use a loop
Expand Down Expand Up @@ -383,7 +389,7 @@ sine:
goto scale_output

noise:
; Generate random sample: 12 clocks
; Generate random sample: 13 clocks
; Uses NES noise algorithm
; 1. check if period has looped over (skip if not)
btfss 0x03, 1
Expand All @@ -401,6 +407,7 @@ noise:
movlw 0x00
btfss 0x24, 0
movf 0x71, 0
addwf 0x76, 0
movwf 0x19 ; write out
; Filler: 33 clocks
; Since the wait time is so high, use a loop
Expand All @@ -426,7 +433,7 @@ skip_noise:
goto next_loop

scale_output:
; Multiply unscaled output by volume + output: 37 clocks
; Multiply unscaled output by volume + output: 38 clocks
; from http://www.piclist.com/techref/microchip/math/mul/8x8.htm
mult MACRO
btfsc 0x03, 0
Expand All @@ -448,6 +455,7 @@ ENDM
mult ;* 4 cycles
mult ;* 4 cycles
movf 0x20, 0
addwf 0x76, 0
movwf 0x19

next_loop:
Expand Down
54 changes: 28 additions & 26 deletions pico-sound-driver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,38 +97,38 @@ template<typename T> static T max(T a, T b) {return a > b ? a : b;}
// arguments:
// 32 bits - floating-point time to fade for

int currentChannel = 0;

void interrupt(int channel) {
gpio_put(PICO_DEFAULT_LED_PIN, false);
gpio_put(PIN_DATA, true);
sleep_us(5);
sleep_us(1);
gpio_put(PIN_CLOCK, true);
sleep_us(5);
sleep_us(1);
gpio_put(PIN_CLOCK, false);
sleep_us(5);
sleep_us(1);
gpio_put(PIN_DATA, false);
sleep_us(5);
sleep_us(1);
for (int i = 0; i < channel; i++) {
gpio_put(PIN_CLOCK, true);
sleep_us(5);
sleep_us(1);
gpio_put(PIN_CLOCK, false);
sleep_us(5);
sleep_us(1);
}
gpio_put(PIN_STROBE, true);
sleep_us(5);
sleep_us(1);
gpio_put(PIN_STROBE, false);
sleep_us(5);
for (int i = channel; i < 32; i++) {
}

void release() {
for (int i = 0; i < 32; i++) {
gpio_put(PIN_CLOCK, true);
sleep_us(5);
sleep_us(1);
gpio_put(PIN_CLOCK, false);
sleep_us(5);
sleep_us(1);
}
gpio_put(PIN_STROBE, true);
sleep_us(5);
sleep_us(1);
gpio_put(PIN_STROBE, false);
sleep_us(5);
sleep_us(1);
gpio_put(PICO_DEFAULT_LED_PIN, true);
}

Expand All @@ -141,7 +141,7 @@ void write_data(uint8_t data) {
gpio_put(11, data & 0x04);
gpio_put(12, data & 0x02);
gpio_put(13, data & 0x01);
sleep_us(2);
sleep_us(1);
gpio_put(14, true);
sleep_us(1);
gpio_put(14, false);
Expand All @@ -163,21 +163,21 @@ int main() {
gpio_out(PIN_DATA);
gpio_out(PIN_CLOCK);
gpio_put(PIN_DATA, false);
sleep_us(5);
sleep_us(1);
for (int i = 0; i < 32; i++) {
gpio_put(PIN_CLOCK, true);
sleep_us(5);
sleep_us(1);
gpio_put(PIN_CLOCK, false);
sleep_us(5);
sleep_us(1);
}
gpio_put(PIN_STROBE, true);
sleep_us(5);
sleep_us(1);
gpio_put(PIN_CLOCK, true);
sleep_us(5);
sleep_us(1);
gpio_put(PIN_CLOCK, false);
sleep_us(5);
sleep_us(1);
gpio_put(PIN_STROBE, false);
sleep_us(5);
sleep_us(1);
for (int i = 0; i < NUM_CHANNELS; i++) {
channels[i].id = i;
channels[i].lastUpdateTime = time_us_64() / 1000000.0;
Expand Down Expand Up @@ -225,6 +225,7 @@ int main() {
interrupt(channel);
write_data(COMMAND_WAVE_TYPE | typeconv[type]);
if (type == 5) write_data(channels[channel].duty * 255.0);
release();
break;
} case 0x20: { // frequency
uint16_t freq = 0;
Expand All @@ -234,6 +235,7 @@ int main() {
interrupt(channel);
write_data(COMMAND_FREQUENCY | ((freq >> 8) & 0x3F));
write_data(freq & 0xFF);
release();
break;
} case 0x40: { // volume
uint8_t vol = getchar();
Expand All @@ -242,6 +244,7 @@ int main() {
interrupt(channel);
write_data(COMMAND_VOLUME);
write_data(vol);
release();
break;
} case 0x60: { // pan
int8_t pan = getchar();
Expand All @@ -264,6 +267,7 @@ int main() {
interrupt(channel);
write_data(COMMAND_VOLUME);
write_data(1);
release();
} else if (time < 0.000001) {
channels[channel].fadeInit = 0.0;
channels[channel].fade = channels[channel].fadeMax = 0;
Expand All @@ -274,11 +278,9 @@ int main() {
interrupt(channel);
write_data(COMMAND_VOLUME);
write_data(0);
release();
}
break;
} case 0xC0: { // set channel (debugging)
currentChannel = channel;
break;
}
}
}
Expand Down

0 comments on commit 0e7b5ff

Please sign in to comment.