Skip to content

Commit

Permalink
Audio tweaks (#103)
Browse files Browse the repository at this point in the history
keira: liltracker: fix calculated buffer size, fix int_t sizes for pattern/event indexes
sdk: add LILKA_NO_AUDIO_HELLO & LILKA_NO_BUZZER_HELLO
doc: add info about audio flags
  • Loading branch information
and3rson authored Apr 15, 2024
1 parent aff663f commit 13c2eff
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 30 deletions.
16 changes: 13 additions & 3 deletions docs/library/build_flags.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
Бібліотека ``lilka`` постачається з рядом вбудованих можливостей, які за замовчуванням увімкнені.

Можливо, вам не подобаються деякі налаштування, які вибрані за замовчуванням.
Наприклад, ви збираєте Лілку на макетній платі і ваш дисплей не здатний працювати на швидкості 80 МГц через високий рівень шумів та ємкостей, викликаних макетною платою та перемичками.

Наприклад, вас дратує звук, який видає Лілка при увімкненні.
Або ж ви збираєте Лілку на макетній платі і ваш дисплей не здатний працювати на швидкості 80 МГц через високий рівень шумів та ємкостей, викликаних макетною платою та перемичками.

Завдяки :term:`PlatformIO` ви можете змінити налаштування, відредагувавши файл `platformio.ini`. Для цього є опція ``build_flags``.

Для того, щоб зменшити швидкість SPI-шини, ви можете відредагувати файл ``firmware/main/platformio.ini`` та додати наступний рядок:
Наприклад, для того, щоб вимкнути всі звуки, ви можете відредагувати файл ``firmware/main/platformio.ini`` та додати наступний рядок:

.. code-block:: ini
:linenos:
Expand All @@ -20,7 +22,7 @@
framework = arduino
lib_deps =
lilka
build_flags = -D LILKA_BREADBOARD
build_flags = -D LILKA_NO_BUZZER_HELLO -D LILKA_NO_AUDIO_HELLO
Після цього ви повинні перекомпілювати вашу прошивку.

Expand All @@ -32,3 +34,11 @@
Використовуйте цю опцію, якщо ви збираєте Лілку на макетній платі або на іншій платі, яка має високий рівень шумів та ємкостей.

Вона зменшує швидкість SPI-шини з 80 МГц до 40 МГц.

.. c:macro:: LILKA_NO_BUZZER_HELLO
Встановіть цю опцію, щоб вимкнути звук, який Лілка відтворює через п'єзоелектричний динамік при увімкненні.

.. c:macro:: LILKA_NO_AUDIO_HELLO
Встановіть цю опцію, щоб вимкнути звук, який Лілка відтворює через I2S-модуль при увімкненні.
6 changes: 3 additions & 3 deletions firmware/keira/src/apps/liltracker/effects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void effect_arpeggio(float time, float relTime, float* frequency, float* amplitu
(void)amplitude;
(void)param;

constexpr int8_t count = 3; // 3 notes
constexpr uint8_t count = 3; // 3 notes

uint8_t note2offset = (param & 0xF0) >> 4;
uint8_t note3offset = (param & 0x0F);
Expand All @@ -30,14 +30,14 @@ void effect_arpeggio(float time, float relTime, float* frequency, float* amplitu
float stepDurationMs = 1000.0f / 60.0f;

// Calculate current arpeggio step
int8_t step = ((int64_t)(time / (stepDurationMs / 1000.0f))) % count;
uint8_t step = ((int64_t)(time / (stepDurationMs / 1000.0f))) % count;

if (step == 0) {
// No change
} else if (step == 1) {
// Use the second note
*frequency = modulate_frequency(*frequency, note2offset);
} else if (step == 2) {
} else {
// Use the third note
*frequency = modulate_frequency(*frequency, note3offset);
}
Expand Down
2 changes: 1 addition & 1 deletion firmware/keira/src/apps/liltracker/liltracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ int LilTrackerApp::drawElement(
}

void LilTrackerApp::startPreview(
Track* track, page_t* page, int32_t requestedChannelIndex, int32_t requestedEventIndex
Track* track, page_t* page, int8_t requestedChannelIndex, uint16_t requestedEventIndex
) {
for (int channelIndex = 0; channelIndex < CHANNEL_COUNT; channelIndex++) {
Pattern* pattern = track->getPattern(page->patternIndices[channelIndex]);
Expand Down
2 changes: 1 addition & 1 deletion firmware/keira/src/apps/liltracker/liltracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class LilTrackerApp : public App {
const char* text, int16_t x, int16_t y, lilka::Alignment hAlign, lilka::Alignment vAlign, bool editing,
bool focused, uint16_t color
);
void startPreview(Track* track, page_t* page, int32_t requestedChannelIndex, int32_t requestedEventIndex);
void startPreview(Track* track, page_t* page, int8_t requestedChannelIndex, uint16_t requestedEventIndex);
void alert(String title, String message);
bool confirm(String title, String message);
String filePicker(bool isSave);
Expand Down
2 changes: 1 addition & 1 deletion firmware/keira/src/apps/liltracker/note.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void noteinfo_t::fromFrequency(float frequency) {
// Function to convert musical note to frequency
float noteinfo_t::toFrequency() {
// C0 is the lowest note, which is index 0 (as opposed to MIDI's 12)
int32_t absIndex = octave * NOTES_PER_OCTAVE + index;
int16_t absIndex = octave * NOTES_PER_OCTAVE + index;
if (absIndex < 0) {
absIndex = 0;
}
Expand Down
5 changes: 1 addition & 4 deletions firmware/keira/src/apps/liltracker/pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ int Pattern::calculateWriteBufferSize() {
Acquire acquire(xMutex);
int32_t bufferSize = 0;

// Count 32 reserved bytes
bufferSize += 32;

// Calculate all channels
for (uint8_t channelIndex = 0; channelIndex < CHANNEL_COUNT; channelIndex++) {
bufferSize += sizeof(uint8_t); // Volume
Expand All @@ -81,7 +78,7 @@ int Pattern::writeToBuffer(uint8_t* buffer) {
int32_t offset = 0;

// Write all channels
for (int8_t channelIndex = 0; channelIndex < CHANNEL_COUNT; channelIndex++) {
for (uint8_t channelIndex = 0; channelIndex < CHANNEL_COUNT; channelIndex++) {
// Write channel settings
WRITE_TO_BUFFER(buffer, channels[channelIndex].volume);

Expand Down
2 changes: 1 addition & 1 deletion firmware/keira/src/apps/liltracker/sequencer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void Sequencer::sequencerTask() {
{
Acquire acquire(xMutex);
const page_t* page = playstate.track->getPage(playstate.pageIndex);
for (int32_t channelIndex = 0; channelIndex < CHANNEL_COUNT; channelIndex++) {
for (uint8_t channelIndex = 0; channelIndex < CHANNEL_COUNT; channelIndex++) {
Pattern* pattern = playstate.track->getPattern(page->patternIndices[channelIndex]);
event_t event = pattern->getChannelEvent(channelIndex, playstate.eventIndex);
// waveform_t waveform = pattern->getChannelWaveform(channelIndex);
Expand Down
11 changes: 5 additions & 6 deletions firmware/keira/src/apps/liltracker/track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ int16_t Track::getUsedPatternCount() {
Acquire acquire(xMutex, true);
int16_t count = 1;
for (int16_t i = 0; i < getPageCount(); i++) {
for (int8_t j = 0; j < CHANNEL_COUNT; j++) {
for (uint8_t j = 0; j < CHANNEL_COUNT; j++) {
count = MAX(count, getPage(i)->patternIndices[j] + 1);
}
}
Expand Down Expand Up @@ -91,7 +91,7 @@ void Track::setPageCount(int16_t count) {
while (getPageCount() < count) {
page_t* lastPage = getPageCount() > 0 ? pages.back() : NULL;
pages.push_back(new page_t());
for (int8_t i = 0; i < CHANNEL_COUNT; i++) {
for (uint8_t i = 0; i < CHANNEL_COUNT; i++) {
if (lastPage != NULL) {
// Copy pattern indices from last page
pages.back()->patternIndices[i] = lastPage->patternIndices[i];
Expand Down Expand Up @@ -151,8 +151,7 @@ void Track::reset() {
int32_t Track::calculateWriteBufferSize() {
Acquire acquire(xMutex, true);
int32_t size = 0;
size += 4; // Signature
size += 64; // Reserved
size += 64; // Signature, version, reserved bytes
size += sizeof(bpm); // BPM
size += sizeof(int16_t); // Pattern count
// Patterns
Expand Down Expand Up @@ -196,7 +195,7 @@ int32_t Track::writeToBuffer(uint8_t* data) {
WRITE_TO_BUFFER(data, pageCount);
// Write pages
for (int16_t i = 0; i < pageCount; i++) {
for (int8_t j = 0; j < CHANNEL_COUNT; j++) {
for (uint8_t j = 0; j < CHANNEL_COUNT; j++) {
WRITE_TO_BUFFER(data, getPage(i)->patternIndices[j]);
}
}
Expand Down Expand Up @@ -235,7 +234,7 @@ int32_t Track::readFromBuffer(const uint8_t* data) {
setPageCount(pageCount);
// Read pages
for (int16_t i = 0; i < pageCount; i++) {
for (int8_t j = 0; j < CHANNEL_COUNT; j++) {
for (uint8_t j = 0; j < CHANNEL_COUNT; j++) {
READ_FROM_BUFFER(getPage(i)->patternIndices[j], data);
}
}
Expand Down
23 changes: 13 additions & 10 deletions sdk/lib/lilka/src/lilka/audio.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "audio.h"
#include "config.h"
#include "serial.h"
#include "ping.h"

namespace lilka {
Expand Down Expand Up @@ -39,16 +38,20 @@ void ping_task(void* arg) {
#if LILKA_VERSION == 1
serial_err("This part of code should never be called. Audio not supported for this version of lilka");
#elif LILKA_VERSION == 2
# ifndef LILKA_NO_AUDIO_HELLO
// Signed 16-bit PCM
// const int16_t* ping = reinterpret_cast<const int16_t*>(ping_raw);

// I2S.begin(I2S_PHILIPS_MODE, 22050, 16);
// for (int i = 0; i < ping_raw_size / 2; i++) {
// // TODO: Should use i2s_write & DMA
// I2S.write(ping[i] >> 2);
// I2S.write(ping[i] >> 2);
// }
// I2S.end();
const int16_t* ping = reinterpret_cast<const int16_t*>(ping_raw);

vTaskDelay(400 / portTICK_PERIOD_MS);

I2S.begin(I2S_PHILIPS_MODE, 22050, 16);
for (int i = 0; i < ping_raw_size / 2; i++) {
// TODO: Should use i2s_write & DMA
I2S.write(ping[i] >> 2);
I2S.write(ping[i] >> 2);
}
I2S.end();
# endif

vTaskDelete(NULL);
#endif
Expand Down
2 changes: 2 additions & 0 deletions sdk/lib/lilka/src/lilka/buzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ void Buzzer::begin() {
#else
_stop();
pinMode(LILKA_BUZZER, OUTPUT);
# ifndef LILKA_NO_BUZZER_HELLO
const Tone helloTune[] = {{NOTE_C3, 8}, {NOTE_C4, 8}, {NOTE_C5, 8}, {NOTE_C7, 4}, {0, 8}, {NOTE_C6, 4}};
playMelody(helloTune, sizeof(helloTune) / sizeof(Tone), 160);
# endif
#endif
}

Expand Down

0 comments on commit 13c2eff

Please sign in to comment.