Skip to content

Commit 82099f7

Browse files
authored
Buffer read (#1975)
* Buffer read and peek return bool * cmake build errors (#1972) * Compile errors other platforms * RingBufferFile * Reviewd RingBufferFile * RingBufferFile: remove pos0 * NBufferFile * Buffers comments * Correct compile errors * Tests * VFS open * Compile warnings * VFS_SDMMC 4 bits as default
1 parent 63ee3f1 commit 82099f7

File tree

33 files changed

+819
-455
lines changed

33 files changed

+819
-455
lines changed

examples/build-arch-log.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,3 @@ arduino:avr:nano ./examples-stream/streams-generator-serial -> rc=0
1010
arduino:samd:arduino_zero_native ./examples-stream/streams-generator-serial -> rc=0
1111
arduino:renesas_uno:unor4wifi ./examples-stream/streams-generator-serial -> rc=0
1212
arduino:mbed_nano:nano33ble ./examples-stream/streams-generator-serial -> rc=0
13-
arduino:zephyr:nano33ble ./examples-stream/streams-generator-serial -> rc=0
14-
arduino:mbed_rp2040:pico ./examples-stream/streams-generator-serial -> rc=0

examples/build-arch.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ compile_example "arduino:avr:nano"
3030
compile_example "arduino:samd:arduino_zero_native"
3131
compile_example "arduino:renesas_uno:unor4wifi"
3232
compile_example "arduino:mbed_nano:nano33ble"
33-
compile_example "arduino:zephyr:nano33ble"
3433
compile_example "arduino:mbed_rp2040:pico"
34+
#compile_example "arduino:zephyr:nano33ble"
3535
#compile_example "STMicroelectronics:stm32:GenF4"
3636

3737
./cleanup.sh

examples/build-examples-log.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
../examples/examples-audiokit/streams-memory_mp3-audiokit -> rc=0
7373
../examples/examples-audiokit/streams-memory_pcm-mixer-audiokit -> rc=0
7474
../examples/examples-audiokit/streams-pins-audiokit -> rc=0
75-
../examples/examples-audiokit/streams-sd_flac-audiokit -> rc=1
75+
../examples/examples-audiokit/streams-sd_flac-audiokit -> rc=0
7676
../examples/examples-audiokit/streams-sdmmc_wav-audiokit -> rc=0
7777
../examples/examples-audiokit/streams-sd_mp3-audiokit -> rc=0
7878
../examples/examples-audiokit/streams-synth-audiokit -> rc=0
@@ -148,6 +148,7 @@
148148
../examples/examples-communication/a2dp/basic-a2dp-mixer-i2s -> rc=0
149149
../examples/examples-communication/a2dp/basic-adc-a2dp -> rc=0
150150
../examples/examples-communication/a2dp/basic-audiokit-a2dp -> rc=0
151+
../examples/examples-communication/a2dp/basic-file_mp3-a2dp -> rc=0
151152
../examples/examples-communication/a2dp/basic-generator-a2dp -> rc=0
152153
../examples/examples-communication/a2dp/basic-i2s-a2dp -> rc=0
153154
../examples/examples-communication/a2dp/basic-player-a2dp -> rc=0
@@ -263,7 +264,7 @@
263264
../examples/tests/codecs/test-codec-opusogg -> rc=0
264265
../examples/tests/codecs/test-codec-sbc -> rc=0
265266
../examples/tests/codecs/test-codec-wav-adpcm -> rc=0
266-
../examples/tests/codecs/test-compile-all -> rc=1
267+
../examples/tests/codecs/test-compile-all -> rc=0
267268
../examples/tests/codecs/test-container-binary -> rc=0
268269
../examples/tests/codecs/test-container-binary-meta -> rc=0
269270
../examples/tests/codecs/test-container-ogg -> rc=0
@@ -304,6 +305,7 @@
304305
../examples/tests/effects/pitch-shift-simple -> rc=0
305306
../examples/tests/etc/callback-write -> rc=0
306307
../examples/tests/etc/test-ads1015 -> rc=0
308+
../examples/tests/etc/test-audiolibs -> rc=0
307309
../examples/tests/etc/test-mulit-compilation-units -> rc=0
308310
../examples/tests/etc/test-pins -> rc=0
309311
../examples/tests/etc/test-ringbufferfile -> rc=0
@@ -332,6 +334,7 @@
332334
../examples/tests/player/test-index-sdfat -> rc=0
333335
../examples/tests/player/test-index-sdmmc -> rc=0
334336
../examples/tests/player/test-player -> rc=0
337+
../examples/tests/player/test-vfs -> rc=0
335338
../examples/tests/timer/test-timer -> rc=0
336339
../examples/tests/timer/test-timercb_rx -> rc=0
337340
../examples/tests/timer/test-timercb_tx -> rc=0

examples/examples-communication/a2dp/basic-file_mp3-a2dp/basic-file_mp3-a2dp.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ File file;
1111
MP3DecoderHelix mp3; // or change to MP3DecoderMAD
1212
EncodedAudioStream decoder(&file, &mp3);
1313
BluetoothA2DPSource a2dp_source;
14-
const int chipSelect = SS //PIN_AUDIO_KIT_SD_CARD_CS;
14+
const int chipSelect = SS; //PIN_AUDIO_KIT_SD_CARD_CS;
1515

1616
// callback used by A2DP to provide the sound data - usually len is 128 2 channel int16 frames
1717
int32_t get_sound_data(uint8_t* data, int32_t size) {
@@ -35,7 +35,7 @@ void setup(void) {
3535
}
3636

3737
// make sure we have enough space for the pcm data
38-
decoder.transformationReader().resizeResultQueue(1024 * 8)
38+
decoder.transformationReader().resizeResultQueue(1024 * 8);
3939
if (!decoder.begin()) {
4040
Serial.println("decoder failed");
4141
stop();

examples/examples-communication/http-client/player-url_subclass-i2s/AudioSourceIcyUrl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22
#include "AudioTools.h"
3+
#include "AudioTools/CoreAudio/AudioHttp/URLStream.h"
4+
#include "AudioTools/Disk/AudioSourceURL.h"
35

46
namespace audio_tools {
57

examples/tests/basic/test-buffer/test-buffer.ino

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ void test(BaseBuffer<int16_t>& b, const char* title) {
1010
}
1111
assert(b.isFull());
1212
for (int j = 0; j < 200; j++) {
13-
int16_t v = b.read();
13+
int16_t v = 0;
14+
assert(b.read(v));
1415
assert(v == j);
1516
}
1617
assert(b.isEmpty());
@@ -21,10 +22,10 @@ void test(BaseBuffer<int16_t>& b, const char* title) {
2122
b.clear();
2223
int len = b.writeArray(array, 200);
2324
Serial.println(len);
24-
len = b.readArray(array,200);
25+
len = b.readArray(array, 200);
2526
Serial.println(len);
26-
for (int j=0;j<200;j++){
27-
assert(array[j]==j);
27+
for (int j = 0; j < 200; j++) {
28+
assert(array[j] == j);
2829
}
2930

3031
Serial.println("Test OK");
@@ -38,10 +39,10 @@ void setup() {
3839
test(b1, "SingleBuffer");
3940

4041
RingBuffer<int16_t> b2(200);
41-
test(b2,"RingBuffer");
42+
test(b2, "RingBuffer");
4243

4344
NBuffer<int16_t> b3(50, 4);
44-
test(b3,"NBuffer");
45+
test(b3, "NBuffer");
4546

4647
Serial.println("Tests OK");
4748
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "AudioTools.h"
2+
#include "AudioTools/AudioLibs/All.h"
3+
4+
void setup(){}
5+
6+
void loop(){}

examples/tests/etc/test-ringbufferfile/test-ringbufferfile.ino

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
* @brief genTest for the file backed ringbuffer
55
* @version 0.1
66
* @date 2023-04-30
7-
*
7+
*
88
* @copyright Copyright (c) 2022
9-
*
9+
*
1010
*/
1111
/**
1212
* Test for the file backed ringbuffer
1313
*/
14-
#include "AudioTools.h"
1514
#include <SdFat.h>
15+
16+
#include "AudioTools.h"
1617
// SD pins
1718
#define PIN_SD_CARD_CS 13
1819
#define PIN_SD_CARD_MISO 2
@@ -24,25 +25,29 @@ SdFs SD;
2425
FsFile file;
2526
RingBufferFile<FsFile, int16_t> buffer(file);
2627

27-
2828
void setup() {
2929
Serial.begin(115200);
3030
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);
3131

3232
// setup SD
33-
SPI.begin(PIN_SD_CARD_CLK, PIN_SD_CARD_MISO, PIN_SD_CARD_MOSI, PIN_SD_CARD_CS);
33+
SPI.begin(PIN_SD_CARD_CLK, PIN_SD_CARD_MISO, PIN_SD_CARD_MOSI,
34+
PIN_SD_CARD_CS);
3435
while (!SD.begin(PIN_SD_CARD_CS, SPI_HALF_SPEED)) {
3536
Serial.println("Card Mount Failed");
3637
delay(500);
3738
}
3839

3940
// create file and setup buffer
40-
file = SD.open(file_name, O_RDWR | O_CREAT );
41+
file = SD.open(file_name, O_RDWR | O_CREAT);
4142
if (!file) {
4243
Serial.println("Failed to open file for writing");
4344
return;
4445
}
45-
46+
if (!buffer.begin(file)) {
47+
Serial.println("Failed to create buffer");
48+
return;
49+
}
50+
4651
// test write
4752
for (int j = 0; j < 10; j++) {
4853
buffer.write(j);
@@ -58,9 +63,11 @@ void setup() {
5863
// test read
5964
Serial.println("read");
6065
for (int j = 0; j < 10; j++) {
61-
int16_t result = buffer.read();
62-
Serial.print(result);
63-
Serial.print(" ");
66+
int16_t result;
67+
if (buffer.read(result)) {
68+
Serial.print(result);
69+
Serial.print(" ");
70+
}
6471
assert(result == j);
6572
}
6673
Serial.println();
@@ -70,7 +77,7 @@ void setup() {
7077
Serial.print(" ");
7178
Serial.println(buffer.available());
7279
memset(tmp, 0, 10 * sizeof(int16_t));
73-
80+
7481
int max = buffer.readArray(tmp, buffer.available());
7582
for (int j = 0; j < max; j++) {
7683
Serial.print(tmp[j]);
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include "SPI.h"
2+
#include "SD.h"
3+
#include "SD_MMC.h"
4+
#include <SdFat.h>
5+
#include "AudioTools.h"
6+
#define AUDIOBOARD_SD
7+
#include "AudioTools/Disk/VFS_SDSPI.h"
8+
#include "AudioTools/Disk/VFS_SDMMC.h"
9+
#include "AudioTools/Disk/VFSFile.h"
10+
#define PIN_AUDIO_KIT_SD_CARD_CS 13
11+
#define PIN_AUDIO_KIT_SD_CARD_MISO 2
12+
#define PIN_AUDIO_KIT_SD_CARD_MOSI 15
13+
#define PIN_AUDIO_KIT_SD_CARD_CLK 14
14+
15+
Vector<uint8_t> data;
16+
int len[] = { 1, 10, 100, 1024, 1024 * 10, 1024 * 100, 1024 * 1000 };
17+
int totalSize = 1024 * 1024 * 100;
18+
19+
void testWrite(Stream& file, int writeSize, int totalSize, const char* name) {
20+
data.resize(writeSize);
21+
memset(data.data(), 0, data.size());
22+
int32_t start = millis();
23+
while (totalSize > 0) {
24+
int written = file.write(data.data(), min(writeSize, totalSize));
25+
totalSize -= written;
26+
}
27+
int32_t time = millis() - start;
28+
float thru = (float)totalSize / (float)time;
29+
Serial.printf("Write, %s, %d, %f\n", name, writeSize, thru);
30+
}
31+
32+
void testRead(Stream& file, int writeSize, int totalSize, const char* name) {
33+
data.resize(writeSize);
34+
memset(data.data(), 0, data.size());
35+
int32_t start = millis();
36+
while (totalSize > 0) {
37+
int written = file.readBytes(data.data(), min(writeSize, totalSize));
38+
totalSize -= written;
39+
}
40+
int32_t time = millis() - start;
41+
float thru = (float)totalSize / (float)time;
42+
Serial.printf("Read, %s, %d, %f\n", name, writeSize, time);
43+
}
44+
45+
46+
template<typename SD, typename Open>
47+
void testFS(SD& sd, Open write, Open read, int cs = -1) {
48+
bool ok = cs != -1 ? sd.begin(cs) : sd.begin();
49+
if (!ok){
50+
Serial.println("SD error");
51+
return;
52+
}
53+
54+
auto file = sd.open("/test.txt", write);
55+
for (int i : len) {
56+
testWrite(file, len[i], totalSize, "VFS_SDMMC, Write");
57+
}
58+
file.close();
59+
file = sd.open("/sdmmc/test.txt", read);
60+
for (int i : len) {
61+
testRead(file, len[i], totalSize, "VFS_SDMMC, Read");
62+
}
63+
file.close();
64+
sd.end();
65+
}
66+
67+
void setup() {
68+
VFS_SDSPI sd;
69+
VFS_SDMMC sdmmc;
70+
SdFat sdfat;
71+
72+
Serial.begin(115200);
73+
74+
SPI.begin(PIN_AUDIO_KIT_SD_CARD_CLK, PIN_AUDIO_KIT_SD_CARD_MISO, PIN_AUDIO_KIT_SD_CARD_MOSI, PIN_AUDIO_KIT_SD_CARD_CS);
75+
76+
testFS<fs::SDFS, const char*>(SD, FILE_WRITE, FILE_READ, PIN_AUDIO_KIT_SD_CARD_CS);
77+
testFS<fs::SDMMCFS, const char*>(SD_MMC, FILE_WRITE, FILE_READ);
78+
testFS<SdFat, int>(sdfat, O_WRITE | O_CREAT, O_RDONLY, PIN_AUDIO_KIT_SD_CARD_CS);
79+
testFS<VFS_SDSPI, FileMode>(sd, VFS_FILE_WRITE, VFS_FILE_READ);
80+
testFS<VFS_SDMMC, FileMode>(sdmmc, VFS_FILE_WRITE, VFS_FILE_READ);
81+
stop();
82+
}
83+
84+
void loop() {}

0 commit comments

Comments
 (0)