Просмотр исходного кода

add different sample rates support

LTVA1 3 лет назад
Родитель
Сommit
928d438203
6 измененных файлов с 83 добавлено и 44 удалено
  1. 5 1
      wav_parser.c
  2. 39 1
      wav_parser.h
  3. 2 23
      wav_player.c
  4. 6 2
      wav_player_hal.c
  5. 0 17
      wav_player_view.c
  6. 31 0
      wav_player_view.h

+ 5 - 1
wav_parser.c

@@ -29,7 +29,7 @@ void wav_parser_free(WavParser* parser) {
     free(parser);
 }
 
-bool wav_parser_parse(WavParser* parser, Stream* stream) {
+bool wav_parser_parse(WavParser* parser, Stream* stream, WavPlayerApp* app) {
     stream_read(stream, (uint8_t*)&parser->header, sizeof(WavHeaderChunk));
     stream_read(stream, (uint8_t*)&parser->format, sizeof(WavFormatChunk));
     stream_read(stream, (uint8_t*)&parser->data, sizeof(WavDataChunk));
@@ -63,6 +63,10 @@ bool wav_parser_parse(WavParser* parser, Stream* stream) {
         parser->format.byte_per_sec,
         parser->format.bits_per_sample);
 
+    app->sample_rate = parser->format.sample_rate;
+    app->num_channels = parser->format.channels;
+    app->bits_per_sample = parser->format.bits_per_sample;
+
     parser->wav_data_start = stream_tell(stream);
     parser->wav_data_end = parser->wav_data_start + parser->data.size;
 

+ 39 - 1
wav_parser.h

@@ -1,6 +1,18 @@
 #pragma once
 #include <toolbox/stream/stream.h>
 
+#include <furi.h>
+#include <furi_hal.h>
+#include <cli/cli.h>
+#include <gui/gui.h>
+#include <stm32wbxx_ll_dma.h>
+#include <dialogs/dialogs.h>
+#include <notification/notification_messages.h>
+#include <gui/view_dispatcher.h>
+#include <toolbox/stream/file_stream.h>
+
+#include "wav_player_view.h"
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -34,11 +46,37 @@ typedef struct {
 
 typedef struct WavParser WavParser;
 
+typedef struct {
+    Storage* storage;
+    Stream* stream;
+    WavParser* parser;
+    uint16_t* sample_buffer;
+    uint8_t* tmp_buffer;
+
+    uint32_t sample_rate;
+
+    uint16_t num_channels;
+    uint16_t bits_per_sample;
+
+    size_t samples_count_half;
+    size_t samples_count;
+
+    FuriMessageQueue* queue;
+
+    float volume;
+    bool play;
+
+    WavPlayerView* view;
+    ViewDispatcher* view_dispatcher;
+    Gui* gui;
+    NotificationApp* notification;
+} WavPlayerApp;
+
 WavParser* wav_parser_alloc();
 
 void wav_parser_free(WavParser* parser);
 
-bool wav_parser_parse(WavParser* parser, Stream* stream);
+bool wav_parser_parse(WavParser* parser, Stream* stream, WavPlayerApp* app);
 
 size_t wav_parser_get_data_start(WavParser* parser);
 

+ 2 - 23
wav_player.c

@@ -80,27 +80,6 @@ static void wav_player_dma_isr(void* ctx) {
     }
 }
 
-typedef struct {
-    Storage* storage;
-    Stream* stream;
-    WavParser* parser;
-    uint16_t* sample_buffer;
-    uint8_t* tmp_buffer;
-
-    size_t samples_count_half;
-    size_t samples_count;
-
-    FuriMessageQueue* queue;
-
-    float volume;
-    bool play;
-
-    WavPlayerView* view;
-    ViewDispatcher* view_dispatcher;
-    Gui* gui;
-    NotificationApp* notification;
-} WavPlayerApp;
-
 static WavPlayerApp* app_alloc() {
     WavPlayerApp* app = malloc(sizeof(WavPlayerApp));
     app->samples_count_half = 1024 * 4;
@@ -219,7 +198,7 @@ static void ctrl_callback(WavPlayerCtrl ctrl, void* ctx) {
 
 static void app_run(WavPlayerApp* app) {
     if(!open_wav_stream(app->stream)) return;
-    if(!wav_parser_parse(app->parser, app->stream)) return;
+    if(!wav_parser_parse(app->parser, app->stream, app)) return;
 
     wav_player_view_set_volume(app->view, app->volume);
     wav_player_view_set_start(app->view, wav_parser_get_data_start(app->parser));
@@ -233,7 +212,7 @@ static void app_run(WavPlayerApp* app) {
     bool eof = fill_data(app, 0);
     eof = fill_data(app, app->samples_count_half);
 
-    wav_player_speaker_init();
+    wav_player_speaker_init(app->sample_rate);
     wav_player_dma_init((uint32_t)app->sample_buffer, app->samples_count);
 
     furi_hal_interrupt_set_isr(FuriHalInterruptIdDma1Ch1, wav_player_dma_isr, app->queue);

+ 6 - 2
wav_player_hal.c

@@ -11,7 +11,8 @@
 #define FURI_HAL_SPEAKER_CHANNEL LL_TIM_CHANNEL_CH1
 #define DMA_INSTANCE DMA1, LL_DMA_CHANNEL_1
 
-void wav_player_speaker_init() {
+void wav_player_speaker_init(uint32_t sample_rate) 
+{
     LL_TIM_InitTypeDef TIM_InitStruct = {0};
     //TIM_InitStruct.Prescaler = 4;
     TIM_InitStruct.Prescaler = 1;
@@ -26,7 +27,10 @@ void wav_player_speaker_init() {
     LL_TIM_OC_Init(FURI_HAL_SPEAKER_TIMER, FURI_HAL_SPEAKER_CHANNEL, &TIM_OC_InitStruct);
 
     TIM_InitStruct.Prescaler = 0;
-    TIM_InitStruct.Autoreload = 1451; //64 000 000 / 1451 ~= 44100 Hz
+    //TIM_InitStruct.Autoreload = 1451; //64 000 000 / 1451 ~= 44100 Hz
+
+    TIM_InitStruct.Autoreload = 64000000 / sample_rate; //to support various sample rates
+
     LL_TIM_Init(SAMPLE_RATE_TIMER, &TIM_InitStruct);
 
     //LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};

+ 0 - 17
wav_player_view.c

@@ -1,22 +1,5 @@
 #include "wav_player_view.h"
 
-#define DATA_COUNT 116
-
-struct WavPlayerView {
-    View* view;
-    WavPlayerCtrlCallback callback;
-    void* context;
-};
-
-typedef struct {
-    bool play;
-    float volume;
-    size_t start;
-    size_t end;
-    size_t current;
-    uint8_t data[DATA_COUNT];
-} WavPlayerViewModel;
-
 float map(float x, float in_min, float in_max, float out_min, float out_max) {
     return (x - in_min) * (out_max - out_min + 1) / (in_max - in_min + 1) + out_min;
 }

+ 31 - 0
wav_player_view.h

@@ -1,6 +1,16 @@
 #pragma once
 #include <gui/view.h>
 
+#include <furi.h>
+#include <furi_hal.h>
+#include <cli/cli.h>
+#include <gui/gui.h>
+#include <stm32wbxx_ll_dma.h>
+#include <dialogs/dialogs.h>
+#include <notification/notification_messages.h>
+#include <gui/view_dispatcher.h>
+#include <toolbox/stream/file_stream.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -18,6 +28,27 @@ typedef enum {
 
 typedef void (*WavPlayerCtrlCallback)(WavPlayerCtrl ctrl, void* context);
 
+#define DATA_COUNT 116
+
+struct WavPlayerView {
+    View* view;
+    WavPlayerCtrlCallback callback;
+    void* context;
+};
+
+typedef struct {
+    bool play;
+    float volume;
+    size_t start;
+    size_t end;
+    size_t current;
+    uint8_t data[DATA_COUNT];
+} WavPlayerViewModel;
+
+
+
+
+
 WavPlayerView* wav_player_view_alloc();
 
 void wav_player_view_free(WavPlayerView* wav_view);