mirror of
https://github.com/DashyFox/StackSport.git
synced 2025-06-28 13:19:41 +00:00
music
This commit is contained in:
225
Core/Src/Sound.c
225
Core/Src/Sound.c
@ -9,11 +9,13 @@
|
||||
#include "stm32f103xb.h"
|
||||
#include "SimpleTimer.h"
|
||||
|
||||
static uint32_t bpm = 145; // Default BPM
|
||||
static uint32_t note_end_time_ch3 = 0; // End time for channel 3
|
||||
static uint32_t note_end_time_ch4 = 0; // End time for channel 4
|
||||
static Note_t current_note_ch3;
|
||||
static Note_t current_note_ch4;
|
||||
static uint32_t bpm = 120; // Default BPM
|
||||
static uint32_t note_end_time_ch[4] = {0, 0, 0, 0}; // End times for each channel (1-4)
|
||||
static Melody_t melodies[4]; // Array of melodies for each channel
|
||||
|
||||
// Initialize the sound system
|
||||
#define SOUND_TIMER TIM4
|
||||
#define SOUND_TIMER_RCC RCC_APB1ENR_TIM4EN
|
||||
|
||||
// Initialize the sound system
|
||||
void sound_init(void) {
|
||||
@ -23,54 +25,29 @@ void sound_init(void) {
|
||||
// Configure GPIO pins for timer output (assuming GPIOB pins for TIM4 CH3 and CH4)
|
||||
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;
|
||||
|
||||
if (SOUND_CHANNEL_1 == 1 || SOUND_CHANNEL_1 == 2) {
|
||||
GPIOB->CRL &= ~(0xF << ((SOUND_CHANNEL_1 - 1) * 4)); // Clear
|
||||
GPIOB->CRL |= (0xB << ((SOUND_CHANNEL_1 - 1) * 4)); // Alternate function push-pull
|
||||
} else if (SOUND_CHANNEL_1 == 3 || SOUND_CHANNEL_1 == 4) {
|
||||
GPIOB->CRH &= ~(0xF << ((SOUND_CHANNEL_1 - 8) * 4)); // Clear
|
||||
GPIOB->CRH |= (0xB << ((SOUND_CHANNEL_1 - 8) * 4)); // Alternate function push-pull
|
||||
}
|
||||
// Configure only channels 3 and 4
|
||||
// Channel 3
|
||||
GPIOB->CRH &= ~(0xF << (3 * 4)); // Clear configuration for PB10 (TIM4_CH3)
|
||||
GPIOB->CRH |= (0xB << (3 * 4)); // Set to alternate function push-pull
|
||||
|
||||
if (SOUND_CHANNEL_2 == 1 || SOUND_CHANNEL_2 == 2) {
|
||||
GPIOB->CRL &= ~(0xF << ((SOUND_CHANNEL_2 - 1) * 4)); // Clear
|
||||
GPIOB->CRL |= (0xB << ((SOUND_CHANNEL_2 - 1) * 4)); // Alternate function push-pull
|
||||
} else if (SOUND_CHANNEL_2 == 3 || SOUND_CHANNEL_2 == 4) {
|
||||
GPIOB->CRH &= ~(0xF << ((SOUND_CHANNEL_2 - 8) * 4)); // Clear
|
||||
GPIOB->CRH |= (0xB << ((SOUND_CHANNEL_2 - 8) * 4)); // Alternate function push-pull
|
||||
}
|
||||
// Channel 4
|
||||
GPIOB->CRH &= ~(0xF << (4 * 4)); // Clear configuration for PB11 (TIM4_CH4)
|
||||
GPIOB->CRH |= (0xB << (4 * 4)); // Set to alternate function push-pull
|
||||
|
||||
// Initialize the timer for PWM output
|
||||
SOUND_TIMER->PSC = 0; // Will be set in sound_play_note()
|
||||
SOUND_TIMER->ARR = 0xFFFF; // Will be set in sound_play_note()
|
||||
SOUND_TIMER->PSC = 0;
|
||||
SOUND_TIMER->ARR = 0xFFFF;
|
||||
|
||||
// Configure PWM mode on both channels
|
||||
if (SOUND_CHANNEL_1 == 3) {
|
||||
SOUND_TIMER->CCMR2 &= ~TIM_CCMR2_OC3M;
|
||||
SOUND_TIMER->CCMR2 |= (6 << TIM_CCMR2_OC3M_Pos); // PWM mode 1 for channel 3
|
||||
SOUND_TIMER->CCER |= TIM_CCER_CC3E; // Enable output on channel 3
|
||||
} else if (SOUND_CHANNEL_1 == 4) {
|
||||
SOUND_TIMER->CCMR2 &= ~TIM_CCMR2_OC4M;
|
||||
SOUND_TIMER->CCMR2 |= (6 << TIM_CCMR2_OC4M_Pos); // PWM mode 1 for channel 4
|
||||
SOUND_TIMER->CCER |= TIM_CCER_CC4E; // Enable output on channel 4
|
||||
}
|
||||
// Configure only channels 3 and 4 for PWM output
|
||||
SOUND_TIMER->CCMR2 &= ~(TIM_CCMR2_OC3M | TIM_CCMR2_OC4M); // Clear channel 3 and 4 mode
|
||||
SOUND_TIMER->CCMR2 |= (6 << TIM_CCMR2_OC3M_Pos); // Set channel 3 to PWM mode 1
|
||||
SOUND_TIMER->CCER |= (TIM_CCER_CC3E | TIM_CCER_CC4E); // Enable output for channels 3 and 4
|
||||
|
||||
if (SOUND_CHANNEL_2 == 3) {
|
||||
SOUND_TIMER->CCMR2 &= ~TIM_CCMR2_OC3M;
|
||||
SOUND_TIMER->CCMR2 |= (6 << TIM_CCMR2_OC3M_Pos); // PWM mode 1 for channel 3
|
||||
SOUND_TIMER->CCER |= TIM_CCER_CC3E; // Enable output on channel 3
|
||||
} else if (SOUND_CHANNEL_2 == 4) {
|
||||
SOUND_TIMER->CCMR2 &= ~TIM_CCMR2_OC4M;
|
||||
SOUND_TIMER->CCMR2 |= (6 << TIM_CCMR2_OC4M_Pos); // PWM mode 1 for channel 4
|
||||
SOUND_TIMER->CCER |= TIM_CCER_CC4E; // Enable output on channel 4
|
||||
}
|
||||
|
||||
// Generate an update event to reload the prescaler and ARR values
|
||||
SOUND_TIMER->EGR = TIM_EGR_UG;
|
||||
|
||||
// Enable the timer
|
||||
SOUND_TIMER->CR1 |= TIM_CR1_CEN;
|
||||
SOUND_TIMER->EGR = TIM_EGR_UG; // Update the timer
|
||||
SOUND_TIMER->CR1 |= TIM_CR1_CEN; // Enable the timer
|
||||
}
|
||||
|
||||
|
||||
// Set BPM
|
||||
void sound_set_bpm(uint32_t new_bpm) {
|
||||
bpm = new_bpm;
|
||||
@ -78,96 +55,88 @@ void sound_set_bpm(uint32_t new_bpm) {
|
||||
|
||||
// Play a note on a specific channel
|
||||
void sound_play_note(Note_t note, uint8_t channel) {
|
||||
if (channel == SOUND_CHANNEL_1) {
|
||||
current_note_ch3 = note;
|
||||
if (note.frequency == 0) {
|
||||
// Pause on channel 3
|
||||
SOUND_TIMER->CCER &= ~TIM_CCER_CC3E; // Disable output on channel 3
|
||||
note_end_time_ch3 = millis() + note.duration;
|
||||
if (channel < 1 || channel > 4) return;
|
||||
|
||||
uint32_t *ccr = &SOUND_TIMER->CCR1 + (channel - 1); // Select correct CCR for the channel
|
||||
uint32_t *ccmr = (channel <= 2) ? &SOUND_TIMER->CCMR1 : &SOUND_TIMER->CCMR2;
|
||||
uint32_t *ccer = &SOUND_TIMER->CCER;
|
||||
uint32_t enable_cc = TIM_CCER_CC1E << ((channel - 1) * 4);
|
||||
|
||||
if (note.frequency == 0) {
|
||||
*ccer &= ~enable_cc;
|
||||
note_end_time_ch[channel - 1] = millis() + note.duration;
|
||||
} else {
|
||||
uint32_t timer_clock = SystemCoreClock / 2;
|
||||
uint32_t prescaler = (timer_clock / (note.frequency * 65536)) + 1;
|
||||
uint32_t arr = (timer_clock / (prescaler * note.frequency)) - 1;
|
||||
uint32_t ccr_val = ((arr + 1) * note.duty_cycle) / 100 - 1;
|
||||
|
||||
SOUND_TIMER->PSC = prescaler - 1;
|
||||
SOUND_TIMER->ARR = arr;
|
||||
*ccr = ccr_val;
|
||||
|
||||
*ccmr &= ~(TIM_CCMR1_OC1M << ((channel - 1) * 8));
|
||||
*ccmr |= (6 << TIM_CCMR1_OC1M_Pos) << ((channel - 1) * 8);
|
||||
|
||||
*ccer |= enable_cc;
|
||||
SOUND_TIMER->EGR = TIM_EGR_UG;
|
||||
SOUND_TIMER->CR1 |= TIM_CR1_CEN;
|
||||
|
||||
note_end_time_ch[channel - 1] = (note.duration == 0) ? 0 : millis() + note.duration;
|
||||
}
|
||||
}
|
||||
|
||||
// Play a melody on a specific channel with repeat count
|
||||
void sound_play_melody(Note_t* melody, uint32_t size, uint8_t channel, uint32_t repeat_count) {
|
||||
if (channel < 1 || channel > 4 || melody == NULL || size == 0) return;
|
||||
|
||||
uint8_t ch_idx = channel - 1;
|
||||
melodies[ch_idx].notes = melody;
|
||||
melodies[ch_idx].size = size;
|
||||
melodies[ch_idx].current_note = 0;
|
||||
melodies[ch_idx].repeat_count = repeat_count;
|
||||
melodies[ch_idx].repeat_left = repeat_count;
|
||||
|
||||
sound_play_note(melody[0], channel); // Play the first note
|
||||
}
|
||||
|
||||
// Function to play the next note in the melody for a specific channel
|
||||
void play_next_note(uint8_t channel) {
|
||||
uint8_t ch_idx = channel - 1;
|
||||
Melody_t* melody = &melodies[ch_idx];
|
||||
|
||||
if (melody->current_note >= melody->size) {
|
||||
// If end of melody, check repeat
|
||||
if (melody->repeat_left > 0) {
|
||||
melody->repeat_left--;
|
||||
melody->current_note = 0; // Restart melody
|
||||
} else {
|
||||
// Configure frequency and duty cycle for channel 3
|
||||
uint32_t timer_clock = SystemCoreClock / 2; // Assuming APB1 prescaler is 2
|
||||
uint32_t prescaler = (timer_clock / (note.frequency * 65536)) + 1;
|
||||
uint32_t arr = (timer_clock / (prescaler * note.frequency)) - 1;
|
||||
uint32_t ccr = ((arr + 1) * note.duty_cycle) / 100 - 1;
|
||||
|
||||
SOUND_TIMER->PSC = prescaler - 1;
|
||||
SOUND_TIMER->ARR = arr;
|
||||
SOUND_TIMER->CCR3 = ccr;
|
||||
|
||||
// Configure PWM mode for channel 3
|
||||
SOUND_TIMER->CCMR2 &= ~(TIM_CCMR2_OC3M | TIM_CCMR2_OC3PE);
|
||||
SOUND_TIMER->CCMR2 |= (TIM_CCMR2_OC3M_1 | TIM_CCMR2_OC3M_2 | TIM_CCMR2_OC3PE); // PWM mode 1
|
||||
|
||||
SOUND_TIMER->CCER |= TIM_CCER_CC3E; // Enable output on channel 3
|
||||
SOUND_TIMER->EGR = TIM_EGR_UG; // Update registers
|
||||
SOUND_TIMER->CR1 |= TIM_CR1_CEN; // Enable timer
|
||||
|
||||
if (note.duration == 0) {
|
||||
note_end_time_ch3 = 0; // Infinite duration
|
||||
} else {
|
||||
note_end_time_ch3 = millis() + note.duration;
|
||||
}
|
||||
}
|
||||
} else if (channel == SOUND_CHANNEL_2) {
|
||||
current_note_ch4 = note;
|
||||
if (note.frequency == 0) {
|
||||
// Pause on channel 4
|
||||
SOUND_TIMER->CCER &= ~TIM_CCER_CC4E; // Disable output on channel 4
|
||||
note_end_time_ch4 = millis() + note.duration;
|
||||
} else {
|
||||
// Configure frequency and duty cycle for channel 4
|
||||
uint32_t timer_clock = SystemCoreClock / 2; // Assuming APB1 prescaler is 2
|
||||
uint32_t prescaler = (timer_clock / (note.frequency * 65536)) + 1;
|
||||
uint32_t arr = (timer_clock / (prescaler * note.frequency)) - 1;
|
||||
uint32_t ccr = ((arr + 1) * note.duty_cycle) / 100 - 1;
|
||||
|
||||
SOUND_TIMER->PSC = prescaler - 1;
|
||||
SOUND_TIMER->ARR = arr;
|
||||
SOUND_TIMER->CCR4 = ccr;
|
||||
|
||||
// Configure PWM mode for channel 4
|
||||
SOUND_TIMER->CCMR2 &= ~(TIM_CCMR2_OC4M | TIM_CCMR2_OC4PE);
|
||||
SOUND_TIMER->CCMR2 |= (TIM_CCMR2_OC4M_1 | TIM_CCMR2_OC4M_2 | TIM_CCMR2_OC4PE); // PWM mode 1
|
||||
|
||||
SOUND_TIMER->CCER |= TIM_CCER_CC4E; // Enable output on channel 4
|
||||
SOUND_TIMER->EGR = TIM_EGR_UG; // Update registers
|
||||
SOUND_TIMER->CR1 |= TIM_CR1_CEN; // Enable timer
|
||||
|
||||
if (note.duration == 0) {
|
||||
note_end_time_ch4 = 0; // Infinite duration
|
||||
} else {
|
||||
note_end_time_ch4 = millis() + note.duration;
|
||||
}
|
||||
// Melody finished, no more repeats
|
||||
melody->notes = NULL;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
sound_play_note(melody->notes[melody->current_note], channel);
|
||||
melody->current_note++;
|
||||
}
|
||||
|
||||
// Function to be called periodically to handle note duration
|
||||
void sound_tick(void) {
|
||||
if (note_end_time_ch3 != 0 && millis() >= note_end_time_ch3) {
|
||||
// Stop the note on channel 3
|
||||
SOUND_TIMER->CCER &= ~TIM_CCER_CC3E; // Disable output on channel 3
|
||||
note_end_time_ch3 = 0;
|
||||
}
|
||||
if (note_end_time_ch4 != 0 && millis() >= note_end_time_ch4) {
|
||||
// Stop the note on channel 4
|
||||
SOUND_TIMER->CCER &= ~TIM_CCER_CC4E; // Disable output on channel 4
|
||||
note_end_time_ch4 = 0;
|
||||
for (uint8_t ch = 0; ch < 4; ++ch) {
|
||||
if (note_end_time_ch[ch] != 0 && millis() >= note_end_time_ch[ch]) {
|
||||
SOUND_TIMER->CCER &= ~(TIM_CCER_CC1E << (ch * 4)); // Disable output
|
||||
note_end_time_ch[ch] = 0;
|
||||
|
||||
if (melodies[ch].notes != NULL) {
|
||||
play_next_note(ch + 1); // Play next note for channel
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create a Note_t from musical note and duration enums
|
||||
Note_t sound_create_note(MusicalNote_t note_enum, NoteDuration_t duration_enum, uint8_t duty_cycle) {
|
||||
Note_t result_note;
|
||||
result_note.frequency = note_enum;
|
||||
if (duration_enum == 0) {
|
||||
result_note.duration = 0; // Infinite duration
|
||||
} else {
|
||||
// Calculate duration in milliseconds
|
||||
uint32_t whole_note_duration = (60000 / bpm) * 4; // Duration of whole note in ms
|
||||
result_note.duration = whole_note_duration / duration_enum;
|
||||
}
|
||||
result_note.duty_cycle = duty_cycle;
|
||||
return result_note;
|
||||
// Create a note
|
||||
Note_t sound_create_note(MusicalNote_t note, NoteDuration_t duration, uint8_t duty_cycle) {
|
||||
uint32_t note_duration = (duration == T0) ? 0 : (60000 * 4 / (bpm * duration));
|
||||
return (Note_t){ .frequency = note, .duration = note_duration, .duty_cycle = duty_cycle };
|
||||
}
|
||||
|
Reference in New Issue
Block a user