mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-05-23 05:24:46 -07:00
Update music_beeper_worker.c
This commit is contained in:
@@ -47,47 +47,51 @@ static int32_t music_beeper_worker_thread_callback(void* context) {
|
|||||||
|
|
||||||
NoteBlockArray_it_t it;
|
NoteBlockArray_it_t it;
|
||||||
NoteBlockArray_it(it, instance->notes);
|
NoteBlockArray_it(it, instance->notes);
|
||||||
|
if(furi_hal_speaker_acquire(1000)) {
|
||||||
|
while(instance->should_work) {
|
||||||
|
if(NoteBlockArray_end_p(it)) {
|
||||||
|
NoteBlockArray_it(it, instance->notes);
|
||||||
|
furi_delay_ms(10);
|
||||||
|
} else {
|
||||||
|
NoteBlock* note_block = NoteBlockArray_ref(it);
|
||||||
|
|
||||||
while(instance->should_work) {
|
float note_from_a4 = (float)note_block->semitone - NOTE_C4_SEMITONE;
|
||||||
if(NoteBlockArray_end_p(it)) {
|
float frequency = NOTE_C4 * powf(TWO_POW_TWELTH_ROOT, note_from_a4);
|
||||||
NoteBlockArray_it(it, instance->notes);
|
float duration = 60.0 * furi_kernel_get_tick_frequency() * 4 / instance->bpm /
|
||||||
furi_delay_ms(10);
|
note_block->duration;
|
||||||
} else {
|
uint32_t dots = note_block->dots;
|
||||||
NoteBlock* note_block = NoteBlockArray_ref(it);
|
while(dots > 0) {
|
||||||
|
duration += duration / 2;
|
||||||
|
dots--;
|
||||||
|
}
|
||||||
|
uint32_t next_tick = furi_get_tick() + duration;
|
||||||
|
float volume = instance->volume;
|
||||||
|
|
||||||
float note_from_a4 = (float)note_block->semitone - NOTE_C4_SEMITONE;
|
if(instance->callback) {
|
||||||
float frequency = NOTE_C4 * powf(TWO_POW_TWELTH_ROOT, note_from_a4);
|
instance->callback(
|
||||||
float duration =
|
note_block->semitone,
|
||||||
60.0 * furi_kernel_get_tick_frequency() * 4 / instance->bpm / note_block->duration;
|
note_block->dots,
|
||||||
uint32_t dots = note_block->dots;
|
note_block->duration,
|
||||||
while(dots > 0) {
|
0.0,
|
||||||
duration += duration / 2;
|
instance->callback_context);
|
||||||
dots--;
|
}
|
||||||
|
|
||||||
|
furi_hal_speaker_stop();
|
||||||
|
furi_hal_speaker_start(frequency, volume);
|
||||||
|
while(instance->should_work && furi_get_tick() < next_tick) {
|
||||||
|
volume *= 0.9945679;
|
||||||
|
furi_hal_speaker_set_volume(volume);
|
||||||
|
furi_delay_ms(2);
|
||||||
|
}
|
||||||
|
NoteBlockArray_next(it);
|
||||||
}
|
}
|
||||||
uint32_t next_tick = furi_get_tick() + duration;
|
|
||||||
float volume = instance->volume;
|
|
||||||
|
|
||||||
if(instance->callback) {
|
|
||||||
instance->callback(
|
|
||||||
note_block->semitone,
|
|
||||||
note_block->dots,
|
|
||||||
note_block->duration,
|
|
||||||
0.0,
|
|
||||||
instance->callback_context);
|
|
||||||
}
|
|
||||||
|
|
||||||
furi_hal_speaker_stop();
|
|
||||||
furi_hal_speaker_start(frequency, volume);
|
|
||||||
while(instance->should_work && furi_get_tick() < next_tick) {
|
|
||||||
volume *= 1;
|
|
||||||
furi_hal_speaker_set_volume(volume);
|
|
||||||
furi_delay_ms(2);
|
|
||||||
}
|
|
||||||
NoteBlockArray_next(it);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
furi_hal_speaker_stop();
|
furi_hal_speaker_stop();
|
||||||
|
furi_hal_speaker_release();
|
||||||
|
} else {
|
||||||
|
FURI_LOG_E(TAG, "Speaker system is busy with another process.");
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -97,11 +101,8 @@ MusicBeeperWorker* music_beeper_worker_alloc() {
|
|||||||
|
|
||||||
NoteBlockArray_init(instance->notes);
|
NoteBlockArray_init(instance->notes);
|
||||||
|
|
||||||
instance->thread = furi_thread_alloc();
|
instance->thread = furi_thread_alloc_ex(
|
||||||
furi_thread_set_name(instance->thread, "MusicBeeperWorker");
|
"MusicBeeperWorker", 1024, music_beeper_worker_thread_callback, instance);
|
||||||
furi_thread_set_stack_size(instance->thread, 1024);
|
|
||||||
furi_thread_set_context(instance->thread, instance);
|
|
||||||
furi_thread_set_callback(instance->thread, music_beeper_worker_thread_callback);
|
|
||||||
|
|
||||||
instance->volume = 1.0f;
|
instance->volume = 1.0f;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user