Skip to content

Commit b3a06f6

Browse files
committed
Filter Test
1 parent ceae914 commit b3a06f6

File tree

6 files changed

+212
-0
lines changed

6 files changed

+212
-0
lines changed

docs/.DS_Store

2 KB
Binary file not shown.

examples/.DS_Store

0 Bytes
Binary file not shown.

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ endif()
3131
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/generator ${CMAKE_CURRENT_BINARY_DIR}/generator)
3232
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/effects ${CMAKE_CURRENT_BINARY_DIR}/effects)
3333
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/filter ${CMAKE_CURRENT_BINARY_DIR}/filter)
34+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/filter-wav ${CMAKE_CURRENT_BINARY_DIR}/filter-wav)
3435
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/mp3-helix ${CMAKE_CURRENT_BINARY_DIR}/mp3-helix)
3536
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/aac-helix ${CMAKE_CURRENT_BINARY_DIR}/aac-helix)
3637
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/aac-fdk ${CMAKE_CURRENT_BINARY_DIR}/aac-fdk)

tests/filter-wav/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
3+
# set the project name
4+
project(filter-wav)
5+
set (CMAKE_CXX_STANDARD 11)
6+
set (DCMAKE_CXX_FLAGS "-Werror")
7+
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
8+
set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
9+
set (CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
10+
endif()
11+
12+
13+
# build sketch as executable
14+
add_executable (filter-wav filter-wav.cpp ../main.cpp)
15+
16+
# set preprocessor defines
17+
target_compile_definitions(filter-wav PUBLIC -DEXIT_ON_STOP -DIS_DESKTOP)
18+
19+
# specify libraries
20+
target_link_libraries(filter-wav portaudio arduino_emulator arduino-audio-tools)
21+

tests/filter-wav/SdFat.h

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#pragma once
2+
3+
#include "Stream.h"
4+
#include <filesystem>
5+
#include <fstream>
6+
#include <iostream>
7+
#include <string>
8+
#include <sys/stat.h>
9+
#include <sys/types.h>
10+
11+
#define SD_SCK_MHZ(maxMhz) (1000000UL * (maxMhz))
12+
#define SPI_FULL_SPEED SD_SCK_MHZ(50)
13+
#define SPI_DIV3_SPEED SD_SCK_MHZ(16)
14+
#define SPI_HALF_SPEED SD_SCK_MHZ(4)
15+
#define SPI_DIV6_SPEED SD_SCK_MHZ(8)
16+
#define SPI_QUARTER_SPEED SD_SCK_MHZ(2)
17+
#define SPI_EIGHTH_SPEED SD_SCK_MHZ(1)
18+
#define SPI_SIXTEENTH_SPEED SD_SCK_HZ(500000)
19+
20+
#define O_RDONLY ios::in ///< Open for reading only.
21+
#define O_WRONLY ios::out ///< Open for writing only.
22+
#define O_RDWR ios::in | ios::out ///< Open for reading and writing.
23+
#define O_AT_END ios::ate ///< Open at EOF.
24+
#define O_APPEND ios::ate ///< Set append mode.
25+
#define O_CREAT ios::trunc ///< Create file if it does not exist.
26+
#define O_TRUNC ios::trunc ///< Truncate file to zero length.
27+
#define O_EXCL 0 ///< Fail if the file exists.
28+
#define O_SYNC 0 ///< Synchronized write I/O operations.
29+
#define O_READ O_RDONLY
30+
#define O_WRITE O_WRONLY
31+
32+
#ifndef SS
33+
#define SS 0
34+
#endif
35+
36+
using namespace std;
37+
38+
/**
39+
* @brief C++ std based emulatoion of SdSpiConfig
40+
*
41+
*/
42+
43+
class SdSpiConfig {
44+
SdSpiConfig(int a = 0, int b = 0, int c = 0) {}
45+
};
46+
47+
/**
48+
* @brief C++ std based emulatoion ofr SdFat
49+
*
50+
*/
51+
class SdFat {
52+
public:
53+
bool begin(int cs = SS, int speed = 0) { return true; }
54+
bool begin(SdSpiConfig &cfg) { return true; }
55+
void errorHalt(const char *msg) {
56+
Serial.println(msg);
57+
exit(0);
58+
}
59+
void initErrorHalt() { exit(0); }
60+
61+
bool exists(char *name) {
62+
struct stat info;
63+
return stat(name, &info) == 0;
64+
}
65+
};
66+
/**
67+
* @brief C++ std based emulatoion ofr SdFile
68+
*
69+
*/
70+
class SdFile : public Stream {
71+
public:
72+
bool isOpen() {
73+
bool result = file.is_open();
74+
return result;
75+
}
76+
77+
bool open(const char *name, int flags) {
78+
this->filename = name;
79+
struct stat info;
80+
if ((flags == O_RDONLY) && stat(name, &info) != 0)
81+
return false;
82+
else if (info.st_mode & S_IFDIR) {
83+
is_dir = true;
84+
size = 0;
85+
dir_path = std::filesystem::path(filename);
86+
iterator = std::filesystem::directory_iterator({dir_path});
87+
return true;
88+
} else {
89+
is_dir = false;
90+
size = info.st_size;
91+
file.open(filename.c_str(), flags);
92+
return isOpen();
93+
}
94+
}
95+
96+
bool close() {
97+
file.close();
98+
return !isOpen();
99+
}
100+
101+
size_t readBytes(uint8_t *buffer, size_t length) override {
102+
return file.readsome((char *)buffer, length);
103+
}
104+
size_t write(const uint8_t *buffer, size_t size) override {
105+
file.write((const char *)buffer, size);
106+
return file.good() ? size : 0;
107+
}
108+
size_t write(uint8_t ch) override {
109+
file.put(ch);
110+
return file.good() ? 1 : 0;
111+
}
112+
int available() override { return size - file.tellg(); };
113+
int availableForWrite() override { return 1024; }
114+
int read() override { return file.get(); }
115+
int peek() override { return file.peek(); }
116+
void flush() override {}
117+
void getName(char *str, size_t len) { strncpy(str, filename.c_str(), len); }
118+
bool isDir() { return is_dir; }
119+
bool isHidden() { return false; }
120+
bool rewind() {
121+
pos = 0;
122+
return is_dir;
123+
}
124+
bool openNext(SdFile &dir, int flags = O_RDONLY) {
125+
if (dir.isDir() && dir.iterator != end(dir.iterator)) {
126+
std::filesystem::directory_entry entry = *dir.iterator++;
127+
return open(entry.path().c_str(), flags);
128+
}
129+
return false;
130+
}
131+
int dirIndex() { return pos; }
132+
133+
protected:
134+
std::fstream file;
135+
size_t size = 0;
136+
bool is_dir;
137+
std::filesystem::directory_iterator iterator;
138+
std::filesystem::path dir_path;
139+
int pos = 0;
140+
std::string filename;
141+
};

tests/filter-wav/filter-wav.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Simple wrapper for Arduino sketch to compilable with cpp in cmake
2+
#include "Arduino.h"
3+
#include "AudioTools.h"
4+
#include "AudioLibs/PortAudioStream.h"
5+
#include "SdFat.h"
6+
7+
// define FIR filter
8+
float coef[] = { 0.021, 0.096, 0.146, 0.096, 0.021};
9+
//
10+
uint16_t sample_rate=44100;
11+
uint8_t channels = 2; // The stream will have 2 channels
12+
NoiseGenerator<int16_t> noise(32000); // subclass of SoundGenerator with max amplitude of 32000
13+
GeneratedSoundStream<int16_t> in_stream(noise); // Stream generated from sine wave
14+
FilteredStream<int16_t, float> in_filtered(in_stream, channels); // Defiles the filter as BaseConverter
15+
SdFat sd;
16+
SdFile file;
17+
EncodedAudioStream out(&file, new WAVEncoder()); // encode as wav file
18+
StreamCopy copier(out, in_stream); // copies sound to out
19+
20+
void setup(){
21+
Serial.begin(115200);
22+
AudioLogger::instance().begin(Serial, AudioLogger::Info);
23+
24+
auto cfg = noise.defaultConfig();
25+
cfg.sample_rate = sample_rate;
26+
cfg.channels = channels;
27+
cfg.bits_per_sample = 16;
28+
noise.begin(cfg);
29+
in_stream.begin();
30+
out.begin();
31+
32+
// setup filters on channel 1 only
33+
in_filtered.setFilter(1, new FIR<float>(coef));
34+
35+
if (!sd.begin(SS, SPI_HALF_SPEED)) sd.initErrorHalt();
36+
if (!file.open("noise.wav", O_RDWR | O_CREAT)) {
37+
sd.errorHalt("opening noise.wav for write failed");
38+
}
39+
40+
for (int j=0;j<1024;j++){
41+
copier.copy();
42+
}
43+
file.close();
44+
stop();
45+
}
46+
47+
void loop(){
48+
}
49+

0 commit comments

Comments
 (0)