diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..63b3af4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,34 @@ +# PlatformIO build files +.pio/ +.pioenvs/ +.piolibdeps/ + +# VS Code settings +.vscode/ + +# PlatformIO package cache +.platformio/ + +# Python virtual environment +venv/ +env/ + +# macOS system files +.DS_Store + +# Windows system files +Thumbs.db + +# Compiled object files +*.o +*.obj + +# Compiled Dynamic libraries +*.dll +*.so +*.dylib + +# Firmware binaries +*.bin +*.elf +*.hex diff --git a/include/README b/include/README new file mode 100644 index 0000000..49819c0 --- /dev/null +++ b/include/README @@ -0,0 +1,37 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the convention is to give header files names that end with `.h'. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..9379397 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into the executable file. + +The source code of each library should be placed in a separate directory +("lib/your_library_name/[Code]"). + +For example, see the structure of the following example libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +Example contents of `src/main.c` using Foo and Bar: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +The PlatformIO Library Dependency Finder will find automatically dependent +libraries by scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/partitions.csv b/partitions.csv new file mode 100644 index 0000000..4ff3ca1 --- /dev/null +++ b/partitions.csv @@ -0,0 +1,5 @@ +# Name, Type, SubType, Offset, Size, Flags +nvs, data, nvs, 0x9000, 0x5000, +otadata, data, ota, 0xe000, 0x2000, +app0, app, ota_0, 0x10000, 0x2F0000, +spiffs, data, spiffs, 0x300000,0x100000 \ No newline at end of file diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..938b322 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,36 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32-s3-devkitm-1] +platform = espressif32 +board = esp32-s3-devkitm-1 +framework = arduino ;, espidf + +board_build.arduino.memory_type = qio_qspi +board_build.flash_mode = qio +board_build.psram_type = qio +board_upload.flash_size = 4MB +board_upload.maximum_size = 4194304 +board_build.partitions = partitions.csv # Use custom partition table +build_flags = -DARDUINO_USB_CDC_ON_BOOT=1 + -DBOARD_HAS_PSRAM + -DARDUINO_ESP32S3_DEV + -DBOARD_HAS_TEMP_SENSOR + ; -DCONFIG_FREERTOS_HZ=1000 + ; -DESP_PLATFORM + ; -DARDUINO_RUNNING +board_build.filesystem = spiffs +monitor_speed = 115200 +upload_speed = 921600 +monitor_filters = esp32_exception_decoder +lib_deps = + https://github.com/pschatzmann/arduino-audio-tools.git + https://github.com/pschatzmann/arduino-audio-driver.git + \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..afd15cf --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,88 @@ +#include "AudioTools.h" +// #include "AudioTools/AudioLibs/AudioI2SStream.h" +#include "AudioTools/AudioLibs/AudioRealFFT.h" // or AudioKissFFT + +I2SStream i2sStream; // I2S input stream for INMP441 +AudioRealFFT fft; // FFT analyzer +StreamCopy copier(fft, i2sStream); // copy I2S mic to FFT + +int channels = 1; // INMP441 is mono +int samples_per_second = 11025; +int bits_per_sample = 32; // INMP441 sends 24-bit data in 32-bit words + +const char* solfegeName(uint8_t midiNote) { + static const char* solfegeNames[] = { + "Do", "Do#", "Re", "Re#", "Mi", "Fa", "Fa#", "Sol", "Sol#", "La", "La#", "Si" + }; + return solfegeNames[midiNote % 12]; +} + +void fftResult(AudioFFTBase &fft) { + float diff; + auto result = fft.result(); + + if (result.magnitude > 100) { // avoid noise floor + float magnitude_dB = 20.0 * log10(result.magnitude); + float freq = result.frequency; + + // MIDI note number + int midiNote = round(69 + 12.0 * log2(freq / 440.0)); + const char* solfege = solfegeName(midiNote); + int octave = (midiNote / 12) - 1; + + Serial.print(freq, 2); + Serial.print(" Hz | "); + + Serial.print("MIDI "); + Serial.print(midiNote); + Serial.print(" | "); + + Serial.print("Note: "); + Serial.print(result.frequencyAsNote(diff)); + Serial.print(" | "); + + Serial.print("Solfège: "); + Serial.print(solfege); + Serial.print(octave); + + Serial.print(" | dB: "); + Serial.print(magnitude_dB, 2); + + Serial.print(" | Diff: "); + Serial.println(diff, 2); + } +} + + +void setup() { + Serial.begin(115200); + AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Warning); + + // Configure I2SStream for INMP441 + auto cfg = i2sStream.defaultConfig(RX_MODE); + cfg.i2s_format = I2S_STD_FORMAT; + cfg.bits_per_sample = bits_per_sample; + cfg.channels = channels; + cfg.sample_rate = samples_per_second; + cfg.is_master = true; + cfg.pin_bck = 8; // SCK + cfg.pin_ws = 9; // WS + cfg.pin_data = 10; // SD + i2sStream.begin(cfg); + + // Configure FFT + auto tcfg = fft.defaultConfig(); + tcfg.length = 8192; // 186ms @ 11kHz minimun C2 theoretical + tcfg.channels = channels; + tcfg.sample_rate = samples_per_second; + tcfg.bits_per_sample = bits_per_sample; + tcfg.callback = &fftResult; + fft.begin(tcfg); + + Serial.println("Setup complete. Listening..."); + +} + +void loop() { + copier.copy(); // Stream mic data into FFT processor +} diff --git a/test/README b/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html