Update main.cpp to use audio header and helpers

This commit is contained in:
Aadi Desai 2023-06-08 01:37:59 +01:00
parent f1dc882f93
commit 8be509cd90
No known key found for this signature in database
3 changed files with 80 additions and 81 deletions

View file

@ -4,9 +4,21 @@
#pragma once #pragma once
#ifdef CSR_AUDIO_BASE #ifdef CSR_AUDIO_BASE
// Function Definition
void reset_audio(void);
void set_wave(uint32_t osc, uint32_t wave);
void set_freq(uint32_t osc, uint32_t freq);
void audio(uint32_t osc, uint32_t wave, uint32_t freq);
void timed_freq(uint32_t osc, uint32_t freq, unsigned int duration_ms);
// Waves // Waves
enum wave_t { enum wave_t : uint32_t {
WAVE_SAWTOOTH, WAVE_SAWTOOTH,
WAVE_SQUARE, WAVE_SQUARE,
WAVE_TRIANGLE, WAVE_TRIANGLE,
@ -15,7 +27,7 @@ enum wave_t {
// Notes // Notes
enum note_t { enum note_t : uint32_t {
NONE = 0, NONE = 0,
C0 = 16, C0 = 16,
C0S = 17, C0S = 17,
@ -126,16 +138,4 @@ enum note_t {
A8S = 7459, A8S = 7459,
B8 = 7902, B8 = 7902,
}; };
// Function Definition
void reset_audio(void);
void set_wave(uint32_t osc, wave_t wave);
void set_freq(uint32_t osc, note_t freq);
void audio(uint32_t osc, wave_t wave, note_t freq);
void timed_freq(uint32_t osc, note_t freq, unsigned int duration_ms);
#endif #endif

View file

@ -13,26 +13,26 @@ void reset_audio(void) {
} }
// Set oscillator `osc` to waveform `wave` // Set oscillator `osc` to waveform `wave`
void set_wave(uint32_t osc, wave_t wave) { void set_wave(uint32_t osc, uint32_t wave) {
audio_osc_write(osc); audio_osc_write(osc);
audio_wav_write(wave); audio_wav_write(wave);
} }
// Set oscillator `osc` to frequency `freq`Hz // Set oscillator `osc` to frequency `freq`Hz
void set_freq(uint32_t osc, note_t freq) { void set_freq(uint32_t osc, uint32_t freq) {
audio_osc_write(osc); audio_osc_write(osc);
audio_tf_write(freq); audio_tf_write(freq);
} }
// Set oscillator `osc` to waveform `wave` at frequency `freq`Hz // Set oscillator `osc` to waveform `wave` at frequency `freq`Hz
void audio(uint32_t osc, wave_t wave, note_t freq) { void audio(uint32_t osc, uint32_t wave, uint32_t freq) {
audio_osc_write(osc); audio_osc_write(osc);
audio_wav_write(wave); audio_wav_write(wave);
audio_tf_write(freq); audio_tf_write(freq);
} }
// Set oscillator `osc` to waveform `wave` at frequency `freq`Hz for `ms` milliseconds // Set oscillator `osc` to waveform `wave` at frequency `freq`Hz for `ms` milliseconds
void timed_freq(uint32_t osc, note_t freq, unsigned int ms) { void timed_freq(uint32_t osc, uint32_t freq, unsigned int ms) {
audio_osc_write(osc); audio_osc_write(osc);
audio_tf_write(freq); audio_tf_write(freq);
busy_wait(ms); busy_wait(ms);

View file

@ -12,7 +12,6 @@
#include "audio" #include "audio"
#include "can" #include "can"
#include <cstdlib>
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
/* Uart */ /* Uart */
@ -136,89 +135,89 @@ static void leds_cmd(char **val) {
#ifdef CSR_AUDIO_BASE #ifdef CSR_AUDIO_BASE
static void saw_cmd(char **val) { static void saw_cmd(char **val) {
uint32_t osc = static_cast<uint32_t>(strtol(get_token(val), NULL, 10)); uint32_t osc = static_cast<uint32_t>(strtol(get_token(val), NULL, 10));
float freq = strtof(get_token(val), NULL); uint32_t freq = static_cast<uint32_t>(strtol(get_token(val), NULL, 10));
printf("Setting Oscillator %d to Sawtooth: %.2fHz\n", osc, freq); printf("Setting Oscillator %d to Sawtooth: %dHz\n", osc, freq);
audio(osc, WAVE_SAWTOOTH, freq); audio(osc, WAVE_SAWTOOTH, freq);
} }
static void square_cmd(char **val) { static void square_cmd(char **val) {
uint32_t osc = static_cast<uint32_t>(strtol(get_token(val), NULL, 10)); uint32_t osc = static_cast<uint32_t>(strtol(get_token(val), NULL, 10));
float freq = strtof(get_token(val), NULL); uint32_t freq = static_cast<uint32_t>(strtol(get_token(val), NULL, 10));
printf("Setting Oscillator %d to Square: %.2fHz\n", osc, freq); printf("Setting Oscillator %d to Square: %dHz\n", osc, freq);
audio(osc, WAVE_SQUARE, freq); audio(osc, WAVE_SQUARE, freq);
} }
static void triangle_cmd(char **val) { static void triangle_cmd(char **val) {
uint32_t osc = static_cast<uint32_t>(strtol(get_token(val), NULL, 10)); uint32_t osc = static_cast<uint32_t>(strtol(get_token(val), NULL, 10));
float freq = strtof(get_token(val), NULL); uint32_t freq = static_cast<uint32_t>(strtol(get_token(val), NULL, 10));
printf("Setting Oscillator %d to Triangle: %.2fHz\n", osc, freq); printf("Setting Oscillator %d to Triangle: %dHz\n", osc, freq);
audio(osc, WAVE_TRIANGLE, freq); audio(osc, WAVE_TRIANGLE, freq);
} }
static void sine_cmd(char **val) { static void sine_cmd(char **val) {
uint32_t osc = static_cast<uint32_t>(strtol(get_token(val), NULL, 10)); uint32_t osc = static_cast<uint32_t>(strtol(get_token(val), NULL, 10));
float freq = strtof(get_token(val), NULL); uint32_t freq = static_cast<uint32_t>(strtol(get_token(val), NULL, 10));
printf("Setting Oscillator %d to Sine: %.2fHz\n", osc, freq); printf("Setting Oscillator %d to Sine: %dHz\n", osc, freq);
audio(osc, WAVE_SINE, freq); audio(osc, WAVE_SINE, freq);
} }
static void imperial_cmd() { static void imperial_cmd() {
timed_freq(0, NOTE_G4, 400); timed_freq(0, G4, 400);
timed_freq(0, NOTE_NONE, 400); timed_freq(0, NONE, 400);
timed_freq(0, NOTE_G4, 400); timed_freq(0, G4, 400);
timed_freq(0, NOTE_NONE, 400); timed_freq(0, NONE, 400);
timed_freq(0, NOTE_G4, 600); timed_freq(0, G4, 600);
timed_freq(0, NOTE_NONE, 600); timed_freq(0, NONE, 600);
timed_freq(0, NOTE_D4S, 200); timed_freq(0, D4S, 200);
timed_freq(0, NOTE_A4S, 200); timed_freq(0, A4S, 200);
timed_freq(0, NOTE_G4, 600); timed_freq(0, G4, 600);
timed_freq(0, NOTE_NONE, 200); timed_freq(0, NONE, 200);
timed_freq(0, NOTE_D4S, 400); timed_freq(0, D4S, 400);
timed_freq(0, NOTE_NONE, 200); timed_freq(0, NONE, 200);
timed_freq(0, NOTE_A4S, 200); timed_freq(0, A4S, 200);
timed_freq(0, NOTE_G4, 1000); timed_freq(0, G4, 1000);
timed_freq(0, NOTE_D4S, 600); timed_freq(0, D4S, 600);
timed_freq(0, NOTE_NONE, 200); timed_freq(0, NONE, 200);
timed_freq(0, NOTE_D5, 600); timed_freq(0, D5, 600);
timed_freq(0, NOTE_NONE, 200); timed_freq(0, NONE, 200);
timed_freq(0, NOTE_D5, 600); timed_freq(0, D5, 600);
timed_freq(0, NOTE_NONE, 200); timed_freq(0, NONE, 200);
timed_freq(0, NOTE_D5, 600); timed_freq(0, D5, 600);
timed_freq(0, NOTE_NONE, 200); timed_freq(0, NONE, 200);
timed_freq(0, NOTE_D5S, 400); timed_freq(0, D5S, 400);
timed_freq(0, NOTE_NONE, 200); timed_freq(0, NONE, 200);
timed_freq(0, NOTE_A4S, 200); timed_freq(0, A4S, 200);
timed_freq(0, NOTE_F4S, 600); timed_freq(0, F4S, 600);
timed_freq(0, NOTE_NONE, 200); timed_freq(0, NONE, 200);
timed_freq(0, NOTE_D4S, 400); timed_freq(0, D4S, 400);
timed_freq(0, NOTE_NONE, 200); timed_freq(0, NONE, 200);
timed_freq(0, NOTE_A4S, 200); timed_freq(0, A4S, 200);
timed_freq(0, NOTE_G4, 800); timed_freq(0, G4, 800);
} }
static void roll_cmd() { static void roll_cmd() {
timed_freq(0, NOTE_C4S, 450); timed_freq(0, C4S, 450);
timed_freq(0, NOTE_D4S, 600); timed_freq(0, D4S, 600);
timed_freq(0, NOTE_G3S, 150); timed_freq(0, G3S, 150);
timed_freq(0, NOTE_D4S, 450); timed_freq(0, D4S, 450);
timed_freq(0, NOTE_F4, 450); timed_freq(0, F4, 450);
timed_freq(0, NOTE_G4S, 90); timed_freq(0, G4S, 90);
timed_freq(0, NOTE_F4S, 90); timed_freq(0, F4S, 90);
timed_freq(0, NOTE_F4, 90); timed_freq(0, F4, 90);
timed_freq(0, NOTE_C4S, 510); timed_freq(0, C4S, 510);
timed_freq(0, NOTE_D4S, 600); timed_freq(0, D4S, 600);
timed_freq(0, NOTE_G3S, 1500); timed_freq(0, G3S, 1500);
timed_freq(0, NOTE_C4S, 450); timed_freq(0, C4S, 450);
timed_freq(0, NOTE_D4S, 600); timed_freq(0, D4S, 600);
timed_freq(0, NOTE_G3S, 150); timed_freq(0, G3S, 150);
timed_freq(0, NOTE_D4S, 450); timed_freq(0, D4S, 450);
timed_freq(0, NOTE_F4, 450); timed_freq(0, F4, 450);
timed_freq(0, NOTE_G4S, 90); timed_freq(0, G4S, 90);
timed_freq(0, NOTE_F4S, 90); timed_freq(0, F4S, 90);
timed_freq(0, NOTE_F4, 90); timed_freq(0, F4, 90);
timed_freq(0, NOTE_C4S, 510); timed_freq(0, C4S, 510);
timed_freq(0, NOTE_D4S, 600); timed_freq(0, D4S, 600);
timed_freq(0, NOTE_G3S, 1500); timed_freq(0, G3S, 1500);
} }
#endif #endif
#ifdef CSR_DAC_VOL_BASE #ifdef CSR_DAC_VOL_BASE
@ -301,7 +300,7 @@ static void can_watch_cmd() {
#ifdef CSR_AUDIO_BASE #ifdef CSR_AUDIO_BASE
const char *notes[85] = {"None", "C1", "C1#", "D1", "D1#", "E1", "F1", "F1#", "G1", "G1#", "A1", "A1#", "B1", "C2", "C2#", "D2", "D2#", "E2", "F2", "F2#", "G2", "G2#", "A2", "A2#", "B2", "C3", "C3#", "D3", "D3#", "E3", "F3", "F3#", "G3", "G3#", "A3", "A3#", "B3", "C4", "C4#", "D4", "D4#", "E4", "F4", "F4#", "G4", "G4#", "A4", "A4#", "B4", "C5", "C5#", "D5", "D5#", "E5", "F5", "F5#", "G5", "G5#", "A5", "A5#", "B5", "C6", "C6#", "D6", "D6#", "E6", "F6", "F6#", "G6", "G6#", "A6", "A6#", "B6", "C7", "C7#", "D7", "D7#", "E7", "F7", "F7#", "G7", "G7#", "A7", "A7#", "B7"}; const char *notes[85] = {"None", "C1", "C1#", "D1", "D1#", "E1", "F1", "F1#", "G1", "G1#", "A1", "A1#", "B1", "C2", "C2#", "D2", "D2#", "E2", "F2", "F2#", "G2", "G2#", "A2", "A2#", "B2", "C3", "C3#", "D3", "D3#", "E3", "F3", "F3#", "G3", "G3#", "A3", "A3#", "B3", "C4", "C4#", "D4", "D4#", "E4", "F4", "F4#", "G4", "G4#", "A4", "A4#", "B4", "C5", "C5#", "D5", "D5#", "E5", "F5", "F5#", "G5", "G5#", "A5", "A5#", "B5", "C6", "C6#", "D6", "D6#", "E6", "F6", "F6#", "G6", "G6#", "A6", "A6#", "B6", "C7", "C7#", "D7", "D7#", "E7", "F7", "F7#", "G7", "G7#", "A7", "A7#", "B7"};
const float freqs[85] = {0.00f, 32.70f, 34.65f, 36.71f, 38.89f, 41.20f, 43.65f, 46.25f, 49.00f, 51.91f, 55.00f, 58.27f, 61.74f, 65.41f, 69.30f, 73.42f, 77.78f, 82.41f, 87.31f, 92.50f, 98.00f, 103.83f, 110.00f, 116.54f, 123.47f, 130.81f, 138.59f, 146.83f, 155.56f, 164.81f, 174.61f, 185.00f, 196.00f, 207.65f, 220.00f, 233.08f, 246.94f, 261.63f, 277.18f, 293.66f, 311.13f, 329.63f, 349.23f, 369.99f, 392.00f, 415.30f, 440.00f, 466.16f, 493.88f, 523.25f, 554.37f, 587.33f, 622.25f, 659.25f, 698.46f, 739.99f, 783.99f, 830.61f, 880.00f, 932.33f, 987.77f, 1046.50f, 1108.73f, 1174.66f, 1244.51f, 1318.51f, 1396.91f, 1479.98f, 1567.98f, 1661.22f, 1760.00f, 1864.66f, 1975.53f, 2093.00f, 2217.46f, 2349.32f, 2489.02f, 2637.02f, 2793.83f, 2959.96f, 3135.96f, 3322.44f, 3520.00f, 3729.31f, 3951.07f}; const uint32_t freqs[85] = {0, 33, 35, 37, 39, 41, 44, 46, 49, 52, 55, 58, 62, 65, 69, 73, 78, 82, 87, 93, 98, 104, 110, 117, 123, 131, 139, 147, 156, 165, 175, 185, 196, 208, 220, 233, 247, 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494, 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988, 1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1976, 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951};
static void can_listen_cmd() { static void can_listen_cmd() {
set_wave(0, WAVE_SINE); set_wave(0, WAVE_SINE);
int old_note = 0; int old_note = 0;