Skip to content

Commit

Permalink
Merge pull request #26 from risgk/develop
Browse files Browse the repository at this point in the history
v2.3.1
  • Loading branch information
risgk authored Apr 21, 2024
2 parents 2b57765 + 010fc54 commit 3dcf376
Show file tree
Hide file tree
Showing 17 changed files with 2,110 additions and 113 deletions.
141 changes: 105 additions & 36 deletions Digital-Synth-PRA32-U/Digital-Synth-PRA32-U.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Digital Synth PRA32-U
*/

#define PRA32_U_VERSION "v2.3.1 "

//#define PRA32_U_USE_DEBUG_PRINT // Serial1

#define PRA32_U_USE_USB_MIDI // Select USB Stack: "Adafruit TinyUSB" in the Arduino IDE "Tools" menu
Expand All @@ -16,13 +18,14 @@

#define PRA32_U_MIDI_CH (0) // 0-based

// for Pimoroni Pico Audio Pack [PIM544]
// for Pimoroni Pico Audio Pack (PIM544)
#define PRA32_U_I2S_DAC_MUTE_OFF_PIN (22)
#define PRA32_U_I2S_DATA_PIN (9)
//#define PRA32_U_I2S_MCLK_PIN (0)
//#define PRA32_U_I2S_MCLK_MULT (0)
#define PRA32_U_I2S_BCLK_PIN (10) // LRCLK Pin is PRA32_U_I2S_BCLK_PIN + 1
#define PRA32_U_I2S_SWAP_BCLK_AND_LRCLK_PINS (false)
#define PRA32_U_I2S_SWAP_LEFT_AND_RIGHT (false)

#define PRA32_U_I2S_BUFFERS (4)
#define PRA32_U_I2S_BUFFER_WORDS (64)
Expand All @@ -33,19 +36,50 @@
#define PRA32_U_PWM_AUDIO_L_PIN (28)
#define PRA32_U_PWM_AUDIO_R_PIN (27)

//#define PRA32_U_USE_PWM_AUDIO_DITHERING_INSTEAD_OF_ERROR_DIFFUSION

#define PRA32_U_USE_2_CORES_FOR_SIGNAL_PROCESSING

#define PRA32_U_USE_EMULATED_EEPROM

////////////////////////////////////////////////////////////////

//#define PRA32_U_USE_CONTROL_PANEL // Experimental

#define PRA32_U_USE_CONTROL_PANEL_KEY_INPUT // Use tactile switches
#define PRA32_U_KEY_INPUT_ACTIVE_LEVEL (HIGH)
#define PRA32_U_KEY_INPUT_PREV_KEY_PIN (16)
#define PRA32_U_KEY_INPUT_NEXT_KEY_PIN (18)
#define PRA32_U_KEY_INPUT_PLAY_KEY_PIN (20)
#define PRA32_U_KEY_ANTI_CHATTERING_WAIT (15)

#define PRA32_U_USE_CONTROL_PANEL_ANALOG_INPUT // Use ADC0, ADC1, and ADC2
#define PRA32_U_ANALOG_INPUT_REVERSED (true)
#define PRA32_U_ANALOG_INPUT_CORRECTION (-504)
#define PRA32_U_ANALOG_INPUT_THRESHOLD (504)
#define PRA32_U_ANALOG_INPUT_DENOMINATOR (504)

#define PRA32_U_USE_CONTROL_PANEL_OLED_DISPLAY // Use SSD1306 monochrome 128x64 OLED
#define PRA32_U_OLED_DISPLAY_I2C (i2c1)
#define PRA32_U_OLED_DISPLAY_I2C_SDA_PIN (6)
#define PRA32_U_OLED_DISPLAY_I2C_SCL_PIN (7)
#define PRA32_U_OLED_DISPLAY_I2C_ADDRESS (0x3C)
#define PRA32_U_OLED_DISPLAY_CONTRAST (0xFF)
#define PRA32_U_OLED_DISPLAY_ROTATION (true)

////////////////////////////////////////////////////////////////

#include "hardware/adc.h"

#if defined(PRA32_U_USE_CONTROL_PANEL)
extern void PRA32_U_ControlPanel_on_control_change(uint8_t control_number);
#endif // defined(PRA32_U_USE_CONTROL_PANEL_ANALOG_INPUT)

#include "pra32-u-common.h"
#include "pra32-u-synth.h"

PRA32_U_Synth g_synth;

#include "pra32-u-control-panel.h"

#include <MIDI.h>
#if defined(PRA32_U_USE_USB_MIDI)
#include <Adafruit_TinyUSB.h>
Expand All @@ -66,17 +100,65 @@ PWMAudio g_pwm_r(PRA32_U_PWM_AUDIO_R_PIN);
#include <I2S.h>
I2S g_i2s_output(OUTPUT);

static volatile uint32_t s_debug_measurement_elapsed0_us = 0;
static volatile uint32_t s_debug_measurement_max0_us = 0;
static volatile uint32_t s_debug_measurement_elapsed1_us = 0;
static volatile uint32_t s_debug_measurement_max1_us = 0;

void handleNoteOn(byte channel, byte pitch, byte velocity);
void handleNoteOff(byte channel, byte pitch, byte velocity);
void handleControlChange(byte channel, byte number, byte value);
void handleHandleProgramChange(byte channel, byte number);
void handleHandlePitchBend(byte channel, int bend);
void writeProgramsToFlashAndEndSketch();

void __not_in_flash_func(setup1)() {
PRA32_U_ControlPanel_setup();

#if defined(PRA32_U_USE_DEBUG_PRINT)
Serial1.setTX(0);
Serial1.setRX(1);
Serial1.begin(115200);
#endif // defined(PRA32_U_USE_DEBUG_PRINT)
}

void __not_in_flash_func(loop1)() {
g_synth.secondary_core_process();
boolean processed = g_synth.secondary_core_process();
if (processed) {
static uint32_t s_loop_counter = 0;
s_loop_counter++;
if (s_loop_counter >= 16 * 400) {
s_loop_counter = 0;
}

PRA32_U_ControlPanel_update_analog_inputs(s_loop_counter);
PRA32_U_ControlPanel_update_display_buffer(s_loop_counter);
PRA32_U_ControlPanel_update_display(s_loop_counter);

#if defined(PRA32_U_USE_DEBUG_PRINT)
switch (s_loop_counter) {
case 1 * 400:
Serial1.print("\e[1;1H\e[K");
Serial1.print(s_debug_measurement_elapsed1_us);
break;
case 2 * 400:
Serial1.print("\e[2;1H\e[K");
Serial1.print(s_debug_measurement_max1_us);
break;
case 3 * 400:
Serial1.print("\e[4;1H\e[K");
Serial1.print(s_debug_measurement_elapsed0_us);
break;
case 4 * 400:
Serial1.print("\e[5;1H\e[K");
Serial1.print(s_debug_measurement_max0_us);
break;
default:
PRA32_U_ControlPanel_debug_print(s_loop_counter);
break;
}
#endif // defined(PRA32_U_USE_DEBUG_PRINT)
}
}

void __not_in_flash_func(setup)() {
Expand Down Expand Up @@ -142,12 +224,6 @@ void __not_in_flash_func(setup)() {
Serial2.begin(PRA32_U_UART_MIDI_SPEED);
#endif // defined(PRA32_U_USE_UART_MIDI)

#if defined(PRA32_U_USE_DEBUG_PRINT)
Serial1.setTX(0);
Serial1.setRX(1);
Serial1.begin(115200);
#endif // defined(PRA32_U_USE_DEBUG_PRINT)

#if defined(ARDUINO_RASPBERRY_PI_PICO)
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
Expand All @@ -167,6 +243,10 @@ void __not_in_flash_func(setup)() {
#endif // defined(PRA32_U_USE_PWM_AUDIO_INSTEAD_OF_I2S)

g_synth.initialize();

PRA32_U_ControlPanel_initialize_parameters();

delay(100);
}

void __not_in_flash_func(loop)() {
Expand All @@ -185,6 +265,8 @@ void __not_in_flash_func(loop)() {
#endif
}

PRA32_U_ControlPanel_update_control();

#if defined(PRA32_U_USE_DEBUG_PRINT)
uint32_t debug_measurement_start1_us = micros();
#endif // defined(PRA32_U_USE_DEBUG_PRINT)
Expand Down Expand Up @@ -214,42 +296,29 @@ void __not_in_flash_func(loop)() {
}
#else // defined(PRA32_U_USE_PWM_AUDIO_INSTEAD_OF_I2S)
for (uint32_t i = 0; i < PRA32_U_I2S_BUFFER_WORDS; i++) {
g_i2s_output.write16(left_buffer[i], right_buffer[i]);
if (PRA32_U_I2S_SWAP_LEFT_AND_RIGHT) {
g_i2s_output.write16(right_buffer[i], left_buffer[i]);
} else {
g_i2s_output.write16(left_buffer[i], right_buffer[i]);
}
}
#endif // defined(PRA32_U_USE_PWM_AUDIO_INSTEAD_OF_I2S)

#if defined(PRA32_U_USE_DEBUG_PRINT)
static uint32_t s_debug_measurement_max0_us = 0;
uint32_t debug_measurement_elapsed0_us = debug_measurement_end_us - debug_measurement_start0_us;
s_debug_measurement_max0_us += (debug_measurement_elapsed0_us > s_debug_measurement_max0_us) *
(debug_measurement_elapsed0_us - s_debug_measurement_max0_us);

static uint32_t s_debug_measurement_max1_us = 0;
uint32_t debug_measurement_elapsed1_us = debug_measurement_end_us - debug_measurement_start1_us;
s_debug_measurement_max1_us += (debug_measurement_elapsed1_us > s_debug_measurement_max1_us) *
(debug_measurement_elapsed1_us - s_debug_measurement_max1_us);

static uint32_t s_debug_loop_counter = 0;
if (++s_debug_loop_counter == 4000) {
s_debug_loop_counter = 0;

Serial1.println(debug_measurement_elapsed1_us);
Serial1.println(s_debug_measurement_max1_us);
Serial1.println(debug_measurement_elapsed0_us);
Serial1.println(s_debug_measurement_max0_us);
Serial1.println();
}
s_debug_measurement_elapsed0_us = debug_measurement_end_us - debug_measurement_start0_us;
s_debug_measurement_max0_us += (s_debug_measurement_elapsed0_us > s_debug_measurement_max0_us) *
(s_debug_measurement_elapsed0_us - s_debug_measurement_max0_us);

s_debug_measurement_elapsed1_us = debug_measurement_end_us - debug_measurement_start1_us;
s_debug_measurement_max1_us += (s_debug_measurement_elapsed1_us > s_debug_measurement_max1_us) *
(s_debug_measurement_elapsed1_us - s_debug_measurement_max1_us);
#endif // defined(PRA32_U_USE_DEBUG_PRINT)
}

void __not_in_flash_func(handleNoteOn)(byte channel, byte pitch, byte velocity)
{
if ((channel - 1) == PRA32_U_MIDI_CH) {
if (velocity > 0) {
g_synth.note_on(pitch, velocity);
} else {
g_synth.note_off(pitch);
}
g_synth.note_on(pitch, velocity);
}
}

Expand Down
Loading

0 comments on commit 3dcf376

Please sign in to comment.