Speaker: Introduce audio playback
Introduce the Speaker class to play audio from the SD card when detecting tags. The only audio supported is WAV. Audio playback is handled via interrupts, so it might try to read from the SD card at the same time as the LCD class is trying to read new frames. Update the LCD animation code to temporarily disable audio interrupts while reading from the SD card.
This commit is contained in:
parent
b0753ada9f
commit
224bc7a6d5
6 changed files with 124 additions and 6 deletions
49
src/Speaker.cpp
Normal file
49
src/Speaker.cpp
Normal file
|
@ -0,0 +1,49 @@
|
|||
//
|
||||
// Created by Teo-CD on 22/10/23.
|
||||
//
|
||||
|
||||
#include "Speaker.h"
|
||||
|
||||
DMAMEM audio_block_t Speaker::audioMemory[audioMemReservation];
|
||||
bool Speaker::memoryInitialized = false;
|
||||
|
||||
void Speaker::init() {
|
||||
if (memoryInitialized)
|
||||
return;
|
||||
AudioStream::initialize_memory(audioMemory, audioMemReservation);
|
||||
memoryInitialized = true;
|
||||
}
|
||||
|
||||
void Speaker::playNewSound(int8_t ID) {
|
||||
char filePath[20];
|
||||
uint8_t rawID = getRawId(ID);
|
||||
|
||||
if (!SD.mediaPresent()) {
|
||||
Com::sendComment("SD Card not present, cannot play sound");
|
||||
return;
|
||||
}
|
||||
if (!isValidId(ID)) {
|
||||
Com::sendComment("Unknown ID for sound : %d", rawID);
|
||||
return;
|
||||
}
|
||||
|
||||
if (sdWavSource.isPlaying())
|
||||
sdWavSource.stop();
|
||||
|
||||
snprintf(filePath, 20, "%s/ENTRY.WAV",
|
||||
(isIdCharacter(ID) ? charDirs : promoDirs)[rawID]);
|
||||
|
||||
if (!sd.exists(filePath)) {
|
||||
Com::sendComment("File %s not present, cannot play sound.", filePath);
|
||||
return;
|
||||
}
|
||||
digitalWrite(pin_Audio_Amp_enable, HIGH);
|
||||
sdWavSource.play(filePath);
|
||||
}
|
||||
|
||||
bool Speaker::checkPlayingAndDisable() {
|
||||
bool isPlaying = sdWavSource.isPlaying();
|
||||
if (!isPlaying)
|
||||
digitalWrite(pin_Audio_Amp_enable, LOW);
|
||||
return isPlaying;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue