Skip to content

Commit 137976f

Browse files
committed
Performance Optimizations
1 parent 43d32de commit 137976f

File tree

16 files changed

+39752
-33
lines changed

16 files changed

+39752
-33
lines changed

examples/sandbox/mp3-mini/BabyElephantWalk60_mp3.h

Lines changed: 39630 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
3+
# set the project name
4+
project(mp3_mini)
5+
set (CMAKE_CXX_STANDARD 11)
6+
set (DCMAKE_CXX_FLAGS "-Werror")
7+
8+
include(FetchContent)
9+
option(BUILD_SHARED_LIBS "Build using shared libraries" OFF)
10+
11+
# provide minimp3
12+
FetchContent_Declare(arduino_minimp3 GIT_REPOSITORY "https://github.com/pschatzmann/arduino-minimp3.git" )
13+
FetchContent_GetProperties(arduino_minimp3)
14+
if(NOT arduino_minimp3_POPULATED)
15+
FetchContent_Populate(arduino_minimp3)
16+
add_subdirectory(${arduino_minimp3_SOURCE_DIR})
17+
endif()
18+
19+
# provide audio-tools
20+
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
21+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../.. ${CMAKE_CURRENT_BINARY_DIR}/arduino-audio-tools )
22+
endif()
23+
24+
# build sketch as executable
25+
set_source_files_properties(mp3-mini.ino PROPERTIES LANGUAGE CXX)
26+
add_executable (mp3_mini mp3-mini.ino)
27+
28+
# set preprocessor defines
29+
target_compile_definitions(arduino_emulator PUBLIC -DDEFINE_MAIN)
30+
target_compile_definitions(arduino_minimp3 INTERFACE -DARDUINO -DMINIMP3_NO_SIMD)
31+
target_compile_definitions(mp3_mini PUBLIC -DARDUINO -DIS_DESKTOP -DEXIT_ON_STOP)
32+
33+
# set compile optioins
34+
target_compile_options(portaudio PRIVATE -Wno-deprecated -Wno-inconsistent-missing-override)
35+
target_compile_options(arduino_minimp3 INTERFACE -Wdouble-promotion)
36+
target_compile_options(arduino-audio-tools INTERFACE -Wno-inconsistent-missing-override)
37+
38+
# specify libraries
39+
target_link_libraries(arduino-audio-tools INTERFACE portaudio arduino_emulator arduino_minimp3 )
40+
#target_link_libraries(mp3_mini PRIVATE arduino_minimp3 portaudio arduino_emulator arduino-audio-tools )
41+

examples/sandbox/mp3-mini/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Playing Sound on your Desktop
2+
3+
We provide some generic output which will also work on Linux, Windows and OS/X
4+
The cmake is downloading all dependencies and builds an executable from the sketch.
5+
6+
We use the more comprehensive new EncodedAudioStream class.
7+
8+
You just need to provide an Arduino Sketch as cpp file. To build the example execute:
9+
10+
```
11+
mkdir build
12+
cd build
13+
cmake ..
14+
make
15+
```
16+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Simple wrapper for Arduino sketch to compilable with cpp in cmake
2+
#include "Arduino.h"
3+
#include "AudioTools.h"
4+
#include "BabyElephantWalk60_mp3.h"
5+
#include "AudioLibs/PortAudioStream.h"
6+
#include "AudioCodecs/CodecMP3Mini.h"
7+
8+
9+
MemoryStream mp3(BabyElephantWalk60_mp3, BabyElephantWalk60_mp3_len);
10+
PortAudioStream out; // Output of sound on desktop
11+
EncodedAudioStream dec(&out, new MP3DecoderMini()); // MP3 data source
12+
StreamCopy copier(dec, mp3); // copy in to out
13+
14+
void setup(){
15+
Serial.begin(115200);
16+
AudioLogger::instance().begin(Serial, AudioLogger::Info);
17+
18+
out.begin();
19+
mp3.begin();
20+
dec.begin();
21+
}
22+
23+
void loop(){
24+
if (mp3) {
25+
copier.copy();
26+
} else {
27+
auto info = dec.decoder().audioInfo();
28+
LOGI("The audio rate from the mp3 file is %d", info.sample_rate);
29+
LOGI("The channels from the mp3 file is %d", info.channels);
30+
exit(0);
31+
}
32+
}

src/AudioCodecs/CodecMP3Mini.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ class MP3DecoderMini : public AudioDecoder {
4343
/// Starts the processing
4444
void begin() {
4545
LOGD(LOG_METHOD);
46-
esp_task_wdt_delete(nullptr);
47-
mp3dec_init(&mp3d);
46+
//esp_task_wdt_delete(nullptr);
47+
::mp3dec_init(&mp3d);
4848
buffer.resize(buffer_size);
4949
pcm.resize(MINIMP3_MAX_SAMPLES_PER_FRAME);
5050
buffer_pos = 0;
@@ -110,13 +110,13 @@ class MP3DecoderMini : public AudioDecoder {
110110

111111
/// Process single bytes so that we can decode a full frame when it is available
112112
void decode(int write_len) {
113-
LOGD("decode: %d ", buffer_pos);
113+
LOGD("decode: %zd ", buffer_pos);
114114
int open = buffer_pos;
115115
int processed = 0;
116116
int samples;
117117
do {
118118
// decode data
119-
samples = mp3dec_decode_frame(&mp3d, buffer.data()+processed, open,
119+
samples = ::mp3dec_decode_frame(&mp3d, buffer.data()+processed, open,
120120
pcm.data(), &mp3dec_info);
121121
LOGD("frame_offset: %d - frame_bytes: %d -> samples %d", mp3dec_info.frame_offset, mp3dec_info.frame_bytes, samples);
122122
open -= mp3dec_info.frame_bytes;
@@ -165,9 +165,9 @@ class MP3DecoderMini : public AudioDecoder {
165165
int i = 0;
166166
for(; i < num_samples; i++){
167167
float sample = in[i] * 32768.0f;
168-
if (sample >= 32766.5)
168+
if (sample >= 32766.5f)
169169
out[i] = (int16_t) 32767;
170-
else if (sample <= -32767.5)
170+
else if (sample <= -32767.5f)
171171
out[i] = (int16_t)-32768;
172172
else {
173173
int16_t s = (int16_t)(sample + .5f);

src/AudioEffects/AudioEffect.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ class Delay : public AudioEffect {
339339
// add input and delay to the buffer
340340
p_history->write(input+delayValue);
341341
// mix input with result
342-
return (delayValue * delayLine->getDepth()) + (input * (1.0 - delayLine->getDepth()));
342+
return (delayValue * delayLine->getDepth()) + (input * (1.0f - delayLine->getDepth()));
343343
}
344344

345345
Delay *clone() {

src/AudioEffects/AudioParameters.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class ADSR : public AbstractParameter {
9797
void keyOn(float tgt=0){
9898
LOGI("%s: %f", LOG_METHOD, tgt);
9999
state = Attack;
100-
this->target = tgt>0 && tgt<=1.0 ? tgt : sustain;
100+
this->target = tgt>0.0f && tgt<=1.0f ? tgt : sustain;
101101
this->act_value = 0;
102102
}
103103

@@ -152,7 +152,7 @@ class ADSR : public AbstractParameter {
152152

153153
case Release:
154154
act_value -= release;
155-
if ( act_value <= 0.0 ) {
155+
if ( act_value <= 0.0f ) {
156156
act_value = 0.0;
157157
state = Idle;
158158
}

src/AudioEffects/SoundGenerator.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ class SineFromTable : public SoundGenerator<T> {
630630

631631
float interpolate(float angle){
632632
bool positive = (angle<=180);
633-
float angle_positive = positive ? angle : angle - 180.0;
633+
float angle_positive = positive ? angle : angle - 180.0f;
634634
int angle_int1 = angle_positive;
635635
int angle_int2 = angle_int1+1;
636636
float v1 = values[angle_int1];
@@ -649,7 +649,7 @@ class SineFromTable : public SoundGenerator<T> {
649649
if (abs(diff) > max_amplitude_step){
650650
diff = (diff<0) ? -max_amplitude_step : max_amplitude_step;
651651
}
652-
if (abs(diff)>=1.0){
652+
if (abs(diff)>=1.0f){
653653
amplitude += diff;
654654
}
655655
}

src/AudioFilter/Equilizer.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ class Equilizer3Bands : public AudioStreamX {
7979
memset(&state[j],0,sizeof(EQSTATE));
8080

8181
// Calculate filter cutoff frequencies
82-
state[j].lf = 2 * sin(M_PI * ((float)p_cfg->freq_low / (float)p_cfg->sample_rate));
83-
state[j].hf = 2 * sin(M_PI * ((float)p_cfg->freq_high / (float)p_cfg->sample_rate));
82+
state[j].lf = 2 * sin((float)M_PI * ((float)p_cfg->freq_low / (float)p_cfg->sample_rate));
83+
state[j].hf = 2 * sin((float)M_PI * ((float)p_cfg->freq_high / (float)p_cfg->sample_rate));
8484
}
8585
return true;
8686
}
@@ -170,7 +170,7 @@ class Equilizer3Bands : public AudioStreamX {
170170

171171
/// convert float in the range -1 to 1 to a int16 and clip the values that are out of range
172172
inline int16_t toInt16(float v){
173-
float result = v * 32767.0;
173+
float result = v * 32767.0f;
174174
// clip result
175175
if (result>32767){
176176
result = 32767;
@@ -182,7 +182,7 @@ class Equilizer3Bands : public AudioStreamX {
182182

183183
/// convert float in the range -1 to 1 to a int16 and clip the values that are out of range
184184
inline float toFloat(int16_t v){
185-
return static_cast<float>(v) / 32767.0;
185+
return static_cast<float>(v) / 32767.0f;
186186
}
187187

188188
// calculates a single sample using the indicated state

src/AudioTools/AudioPlayer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ namespace audio_tools {
267267

268268
/// sets the volume - values need to be between 0.0 and 1.0
269269
virtual void setVolume(float volume) {
270-
if (volume >= 0 && volume <= 1.0) {
271-
if (abs(volume - current_volume) > 0.01) {
270+
if (volume >= 0.0f && volume <= 1.0f) {
271+
if (abs(volume - current_volume) > 0.01f) {
272272
LOGI("setVolume(%f)", volume);
273273
volume_out.setVolume(volume);
274274
current_volume = volume;

0 commit comments

Comments
 (0)