Skip to content

Commit c78feee

Browse files
committed
♻️ (Audio): Update lk_audio spike with AudioKit
1 parent 7f1bd06 commit c78feee

File tree

2 files changed

+46
-47
lines changed

2 files changed

+46
-47
lines changed

spikes/lk_audio/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ target_sources(spike_lk_audio
1515
)
1616

1717
target_link_libraries(spike_lk_audio
18-
FileManagerKit
18+
CoreSTM32Hal
19+
CoreDAC
20+
AudioKit
21+
BLEKit
1922
)
2023

2124
target_link_custom_leka_targets(spike_lk_audio)

spikes/lk_audio/main.cpp

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,45 @@
11
// Leka - LekaOS
2-
// Copyright 2022 APF France handicap
2+
// Copyright 2024 APF France handicap
33
// SPDX-License-Identifier: Apache-2.0
44

5-
#include <array>
6-
#include <filesystem>
7-
8-
#include "drivers/AnalogOut.h"
9-
#include "drivers/DigitalOut.h"
10-
#include "platform/mbed_wait_api.h"
115
#include "rtos/ThisThread.h"
12-
#include "rtos/Thread.h"
136

7+
#include "BLEKit.h"
8+
#include "BLEServiceConfig.h"
9+
10+
#include "AudioKit.h"
11+
#include "CoreDAC.h"
12+
#include "CoreSTM32Hal.h"
13+
#include "CoreSTM32HalBasicTimer.h"
1414
#include "FATFileSystem.h"
15-
#include "FileManagerKit.h"
1615
#include "LogKit.h"
1716
#include "SDBlockDevice.h"
1817

1918
using namespace leka;
2019
using namespace std::chrono_literals;
2120

22-
auto sd_bd = SDBlockDevice {SD_SPI_MOSI, SD_SPI_MISO, SD_SPI_SCK};
23-
auto fatfs = FATFileSystem {"fs"};
21+
auto sd_bd = SDBlockDevice {SD_SPI_MOSI, SD_SPI_MISO, SD_SPI_SCK};
22+
auto fatfs = FATFileSystem {"fs"};
23+
auto filename = std::filesystem::path {};
24+
25+
auto hal = CoreSTM32Hal {};
26+
27+
namespace audio {
28+
29+
namespace internal {
30+
31+
extern "C" auto hal_timer = CoreSTM32HalBasicTimer {hal};
32+
extern "C" auto coredac = CoreDAC {hal, hal_timer};
2433

25-
const auto sound_file_path = std::filesystem::path {"/fs/home/wav/fur-elise.wav"};
26-
auto file = FileManagerKit::File {sound_file_path};
34+
} // namespace internal
2735

28-
auto thread_audio = rtos::Thread {};
36+
auto kit = AudioKit {internal::hal_timer, internal::coredac};
2937

30-
auto audio_enable = mbed::DigitalOut {SOUND_ENABLE, 1};
31-
auto audio_output = mbed::AnalogOut {MCU_SOUND_OUT};
38+
} // namespace audio
39+
40+
auto service_config = BLEServiceConfig {};
41+
auto services = std::to_array<interface::BLEService *>({&service_config});
42+
auto blekit = BLEKit {};
3243

3344
void initializeSD()
3445
{
@@ -40,48 +51,33 @@ void initializeSD()
4051
fatfs.mount(&sd_bd);
4152
}
4253

43-
void playSound()
54+
void play(std::string filename)
4455
{
45-
static const auto _n_bytes_to_read = int {512}; // arbitrary
46-
auto _buffer = std::array<uint8_t, _n_bytes_to_read> {0};
47-
48-
auto _ns_sample_rate = uint32_t {22676}; // 1,000,000,000 / 44,100 (in ns)
49-
auto _ns_sample_rate_adapted = _ns_sample_rate * 1.7; // arbitrary, 1s in MCU is not exactly 1s in real life
50-
auto bytesread = uint32_t {_n_bytes_to_read};
51-
52-
/* START READ WAV */
53-
while (bytesread == _n_bytes_to_read) {
54-
// Read "_n_bytes_to_read" from file at each iteration. Real bytes read is given by "bytesread"
55-
if (bytesread = file.read(_buffer.data(), _n_bytes_to_read); bytesread != 0) {
56-
// Play every 2-bytes (sound encoded in 16 bits)
57-
for (uint32_t j = 0; j < bytesread; j += 4) { // Play one channel, data for stereo are alternate
58-
audio_output.write_u16((_buffer.at(j + 1) + 0x8000) >>
59-
1); // offset for int16 data (0x8000) and volume 50% (>>1)
60-
61-
wait_ns(_ns_sample_rate_adapted); // adjust play speed
62-
}
63-
}
64-
}
65-
/* END READ WAV*/
56+
log_info("Play file: %s", filename.c_str());
57+
audio::kit.play(filename);
6658
}
6759

6860
auto main() -> int
6961
{
7062
logger::init();
7163

7264
log_info("Hello, World!\n\n");
65+
rtos::ThisThread::sleep_for(1s);
7366

7467
initializeSD();
7568

76-
if (FileManagerKit::file_is_missing(sound_file_path)) {
77-
return 1;
78-
}
69+
audio::kit.initialize();
7970

80-
while (true) {
81-
file.open(sound_file_path);
82-
playSound();
83-
file.close();
71+
blekit.setServices(services);
72+
blekit.init();
73+
74+
service_config.onRobotNameUpdated([](const std::array<uint8_t, BLEServiceConfig::kMaxRobotNameSize> &robot_name) {
75+
const auto *end_index = std::find(robot_name.begin(), robot_name.end(), '\0');
76+
filename = std::string {robot_name.begin(), end_index};
77+
play(filename);
78+
});
8479

85-
rtos::ThisThread::sleep_for(1s);
80+
while (true) {
81+
rtos::ThisThread::sleep_for(1min);
8682
}
8783
}

0 commit comments

Comments
 (0)