Music player rework (#1189)
* Music player: cli tool and new worker * Music player cli: flush message * Music player: fix note calculation * MusicPlayer: fix # parsing and add magic * FuriHal: improve speaker volume handling. MusicPlayer: minor sustain improvements * MusicPlayer: fix buffer overseek * FuriHal: drop unused variables * MusicPlayer: LFO 4 magic * MusicPlayer: add RTTTL parser * MusicPlayer: refactoring and add file open dialog on start * MusicPlayer: fix memcpy issue and more * FuriHal: force disconnect USB on early init and then leave usb line alone for some time. * FuriHal: switch speaker to old volume. MusicPlayer: fix incorrect note history, and drop lfo from worker. Co-authored-by: DrZlo13 <who.just.the.doctor@gmail.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include <furi_hal_resources.h>
|
||||
#include <furi_hal_delay.h>
|
||||
#include <furi.h>
|
||||
|
||||
#include <stm32wbxx_ll_rcc.h>
|
||||
@@ -87,10 +88,19 @@ void furi_hal_resources_init_early() {
|
||||
SET_BIT(PWR->CR3, PWR_CR3_APC);
|
||||
|
||||
// Hard reset USB
|
||||
furi_hal_gpio_write(&gpio_usb_dm, 1);
|
||||
furi_hal_gpio_write(&gpio_usb_dp, 1);
|
||||
furi_hal_gpio_init_simple(&gpio_usb_dm, GpioModeOutputOpenDrain);
|
||||
furi_hal_gpio_init_simple(&gpio_usb_dp, GpioModeOutputOpenDrain);
|
||||
furi_hal_gpio_write(&gpio_usb_dm, 0);
|
||||
furi_hal_gpio_write(&gpio_usb_dp, 0);
|
||||
furi_hal_delay_us(5); // Device Driven disconnect: 2.5us + extra to compensate cables
|
||||
furi_hal_gpio_write(&gpio_usb_dm, 1);
|
||||
furi_hal_gpio_write(&gpio_usb_dp, 1);
|
||||
furi_hal_gpio_init_simple(&gpio_usb_dm, GpioModeAnalog);
|
||||
furi_hal_gpio_init_simple(&gpio_usb_dp, GpioModeAnalog);
|
||||
furi_hal_gpio_write(&gpio_usb_dm, 0);
|
||||
furi_hal_gpio_write(&gpio_usb_dp, 0);
|
||||
|
||||
// External header pins
|
||||
furi_hal_gpio_init(&gpio_ext_pc0, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
|
||||
|
@@ -20,15 +20,7 @@ void furi_hal_speaker_init() {
|
||||
&gpio_speaker, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedLow, GpioAltFn14TIM16);
|
||||
}
|
||||
|
||||
void furi_hal_speaker_start(float frequency, float volume) {
|
||||
if(volume == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(volume < 0) volume = 0;
|
||||
if(volume > 1) volume = 1;
|
||||
volume = volume * volume * volume;
|
||||
|
||||
static inline uint32_t furi_hal_speaker_calculate_autoreload(float frequency) {
|
||||
uint32_t autoreload = (SystemCoreClock / FURI_HAL_SPEAKER_PRESCALER / frequency) - 1;
|
||||
if(autoreload < 2) {
|
||||
autoreload = 2;
|
||||
@@ -36,35 +28,65 @@ void furi_hal_speaker_start(float frequency, float volume) {
|
||||
autoreload = UINT16_MAX;
|
||||
}
|
||||
|
||||
LL_TIM_InitTypeDef TIM_InitStruct = {0};
|
||||
TIM_InitStruct.Prescaler = FURI_HAL_SPEAKER_PRESCALER - 1;
|
||||
TIM_InitStruct.Autoreload = autoreload;
|
||||
LL_TIM_Init(FURI_HAL_SPEAKER_TIMER, &TIM_InitStruct);
|
||||
return autoreload;
|
||||
}
|
||||
|
||||
static inline uint32_t furi_hal_speaker_calculate_compare(float volume) {
|
||||
if(volume < 0) volume = 0;
|
||||
if(volume > 1) volume = 1;
|
||||
volume = volume * volume * volume;
|
||||
|
||||
#ifdef FURI_HAL_SPEAKER_NEW_VOLUME
|
||||
uint32_t compare_value = volume * FURI_HAL_SPEAKER_MAX_VOLUME;
|
||||
uint32_t clip_value = volume * TIM_InitStruct.Autoreload / 2;
|
||||
uint32_t clip_value = volume * LL_TIM_GetAutoReload(FURI_HAL_SPEAKER_TIMER) / 2;
|
||||
if(compare_value > clip_value) {
|
||||
compare_value = clip_value;
|
||||
}
|
||||
#else
|
||||
uint32_t compare_value = volume * autoreload / 2;
|
||||
uint32_t compare_value = volume * LL_TIM_GetAutoReload(FURI_HAL_SPEAKER_TIMER) / 2;
|
||||
#endif
|
||||
|
||||
if(compare_value == 0) {
|
||||
compare_value = 1;
|
||||
}
|
||||
|
||||
return compare_value;
|
||||
}
|
||||
|
||||
void furi_hal_speaker_start(float frequency, float volume) {
|
||||
if(volume <= 0) {
|
||||
furi_hal_speaker_stop();
|
||||
return;
|
||||
}
|
||||
|
||||
LL_TIM_InitTypeDef TIM_InitStruct = {0};
|
||||
TIM_InitStruct.Prescaler = FURI_HAL_SPEAKER_PRESCALER - 1;
|
||||
TIM_InitStruct.Autoreload = furi_hal_speaker_calculate_autoreload(frequency);
|
||||
LL_TIM_Init(FURI_HAL_SPEAKER_TIMER, &TIM_InitStruct);
|
||||
|
||||
LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
|
||||
TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1;
|
||||
TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_ENABLE;
|
||||
TIM_OC_InitStruct.CompareValue = compare_value;
|
||||
TIM_OC_InitStruct.CompareValue = furi_hal_speaker_calculate_compare(volume);
|
||||
LL_TIM_OC_Init(FURI_HAL_SPEAKER_TIMER, FURI_HAL_SPEAKER_CHANNEL, &TIM_OC_InitStruct);
|
||||
|
||||
LL_TIM_EnableAllOutputs(FURI_HAL_SPEAKER_TIMER);
|
||||
LL_TIM_EnableCounter(FURI_HAL_SPEAKER_TIMER);
|
||||
}
|
||||
|
||||
void furi_hal_speaker_set_volume(float volume) {
|
||||
if(volume <= 0) {
|
||||
furi_hal_speaker_stop();
|
||||
return;
|
||||
}
|
||||
|
||||
#if FURI_HAL_SPEAKER_CHANNEL == LL_TIM_CHANNEL_CH1
|
||||
LL_TIM_OC_SetCompareCH1(FURI_HAL_SPEAKER_TIMER, furi_hal_speaker_calculate_compare(volume));
|
||||
#else
|
||||
#error Invalid channel
|
||||
#endif
|
||||
}
|
||||
|
||||
void furi_hal_speaker_stop() {
|
||||
LL_TIM_DisableAllOutputs(FURI_HAL_SPEAKER_TIMER);
|
||||
LL_TIM_DisableCounter(FURI_HAL_SPEAKER_TIMER);
|
||||
|
@@ -12,6 +12,8 @@ void furi_hal_speaker_init();
|
||||
|
||||
void furi_hal_speaker_start(float frequency, float volume);
|
||||
|
||||
void furi_hal_speaker_set_volume(float volume);
|
||||
|
||||
void furi_hal_speaker_stop();
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
Reference in New Issue
Block a user