plaintext feat undefined: Updated Arduino project with new features for audio processing. fix: Updated I2SConfig.h to include additional parameter for reading I2S samples. refactor: Improved performance by rounding integer samples to
- This code snippet is a C++ program that utilizes the Arduino framework to implement audio processing for a microphone. The program includes several classes and functions to handle various aspects of audio analysis, such as note detection, frequency analysis, and spectrum visualization. The key components of this program include: 1. **AudioLevelTracker**: This class provides real-time audio level monitoring by tracking the maximum amplitude of an input signal. It uses a simple peak detection algorithm to determine when the input signal reaches a certain threshold. 2. **NoteDetector**: This class performs frequency analysis on the input audio signal and identifies specific notes based on their frequencies. The note detector utilizes a pre-defined list of known frequencies and compares them against the detected frequencies to identify matches. 3. **SpectrumVisualizer**: This class provides real-time spectrum visualization by displaying the magnitude of the input audio signal in the form of an ASCII graph. The magnitude scaling is done dynamically based on the signal power to ensure that all frequencies are visible. 4. **Main Loop**: The main loop handles all the other components and processes them sequentially. It initializes the audio level tracker, note detector, and spectrum visualizer, and then enters a loop where it continuously processes the input audio signal. The program also includes error handling mechanisms, such as automatic I2S reset on communication errors and dynamic threshold adjustment to ensure that the audio processing remains stable and accurate. The project is structured with clear class definitions and proper documentation for each component. - The updateMaxLevel and getMaxLevel methods in AudioLevelTracker have been modified to accept and return int16_t values instead of int32_t, which improves range handling. - The `Config.h` file has been updated to enhance audio processing by increasing gain, adjusting noise threshold for 16-bit samples, and changing the FFT size from a power of 2. The main goal is to optimize performance while maintaining good noise detection and note detection capabilities for better accuracy in music analysis tasks. - The `git diff` output shows a change to the I2SConfig.h file. Specifically, it adds a line to define an additional parameter for reading I2S samples: int16_t*. - This commit introduces a new header file `NoteDetector.h` for detecting musical notes in an Arduino project, enhancing the detection process with FFT analysis and dynamic threshold adjustments. - The `SpectrumVisualizer.h` file has been added to the project with new definitions and functions to visualize audio spectrum and detected notes. - The main goal of these changes is to update the `lib_deps` in the `platformio.ini` file to include a specific library named `kosme/arduinoFFT` which is version 1.6. - The changes improve the audio level tracking by rounding the integer samples to 16 bits before storing them, ensuring that the range remains within a feasible limit for processing. - The main goal of the changes is to optimize the `readI2SSamples` function by removing unnecessary conversion from 16-bit to 32-bit samples, which was previously done in an existing code section that could be reused for other purposes. This change improves performance and reduces complexity while maintaining compatibility with existing code. - A new `NoteDetector` class has been created in the `src/NoteDetector.cpp` file, implementing various calibration and note detection functionalities. - The user has added new functions `magnitudeToDb`, `mapToDisplay`, `printBarGraph`, `drawFFTMagnitudes`, `visualizeSpectrum`, and `visualizeNotes` to the `SpectrumVisualizer.cpp` file. The changes are related to visualizing spectrum data and note detection results in a serial monitor format for debugging. - The main goal is to enhance the piano note detection system by adding support for a NoteDetector and updating SpectrumVisualizer when notes are detected, as well as handling serial commands for calibration, threshold adjustments, and toggling spectrum display.
This commit is contained in:
@@ -15,7 +15,7 @@ namespace Config {
|
||||
constexpr int SAMPLE_RATE = 8000; // Hz - good for frequencies up to 4kHz
|
||||
constexpr int BITS_PER_SAMPLE = 16; // Changed to 16-bit
|
||||
constexpr int CHANNELS = 1; // Mono input
|
||||
constexpr int SAMPLE_BUFFER_SIZE = 1024; // 128ms window at 8kHz - good for low frequency resolution
|
||||
constexpr int SAMPLE_BUFFER_SIZE = 1024; // Match FFT_SIZE for efficiency
|
||||
|
||||
// DMA Configuration
|
||||
constexpr int DMA_BUFFER_COUNT = 4; // Reduced for lower latency
|
||||
@@ -23,10 +23,27 @@ namespace Config {
|
||||
|
||||
// Audio Processing
|
||||
constexpr float DC_OFFSET = 0.0f; // DC offset correction
|
||||
constexpr float GAIN = 1.5f; // Adjusted gain for 16-bit range
|
||||
constexpr int16_t NOISE_THRESHOLD = 1000; // Adjusted for 16-bit range
|
||||
constexpr float GAIN = 4.0f; // Increased gain for better note detection
|
||||
constexpr int16_t NOISE_THRESHOLD = 100; // Lower threshold for 16-bit samples
|
||||
constexpr int16_t DEFAULT_RANGE_LIMIT = 32767; // Max value for 16-bit
|
||||
constexpr float DECAY_FACTOR = 0.80f; // Faster decay for quicker note changes
|
||||
constexpr float DECAY_FACTOR = 0.7f; // Faster decay for quicker note changes
|
||||
|
||||
// FFT Configuration
|
||||
constexpr int FFT_SIZE = 1024; // Must be power of 2, gives ~7.8 Hz resolution at 8kHz
|
||||
constexpr float FREQUENCY_RESOLUTION = static_cast<float>(SAMPLE_RATE) / FFT_SIZE;
|
||||
|
||||
// Piano Note Frequencies (Hz)
|
||||
constexpr float NOTE_FREQ_C2 = 65.41f; // Lowest note we'll detect
|
||||
constexpr float NOTE_FREQ_C3 = 130.81f;
|
||||
constexpr float NOTE_FREQ_C4 = 261.63f; // Middle C
|
||||
constexpr float NOTE_FREQ_C5 = 523.25f;
|
||||
constexpr float NOTE_FREQ_C6 = 1046.50f; // Highest note we'll detect
|
||||
|
||||
// Note Detection Parameters
|
||||
constexpr float FREQUENCY_TOLERANCE = 3.0f; // Hz tolerance, increased for better detection
|
||||
constexpr float MIN_MAGNITUDE_THRESHOLD = 500.0f; // Adjusted for 16-bit samples
|
||||
constexpr int MAX_SIMULTANEOUS_NOTES = 7; // Maximum number of notes to detect simultaneously
|
||||
constexpr float PEAK_RATIO_THRESHOLD = 0.1f; // Peaks must be at least this ratio of the strongest peak
|
||||
|
||||
// Timing and Debug
|
||||
constexpr uint32_t LEVEL_UPDATE_INTERVAL_MS = 50; // Faster updates for better responsiveness
|
||||
@@ -37,5 +54,31 @@ namespace Config {
|
||||
constexpr uint32_t TASK_STACK_SIZE = 4096; // Audio task stack size
|
||||
constexpr uint8_t TASK_PRIORITY = 2; // Increased priority for more consistent timing
|
||||
constexpr uint8_t TASK_CORE = 1; // Run on second core to avoid interference
|
||||
|
||||
// Calibration Parameters
|
||||
constexpr bool CALIBRATION_MODE = false; // Set to true to enable calibration output
|
||||
constexpr int CALIBRATION_DURATION_MS = 5000; // Duration to collect calibration data
|
||||
constexpr float CALIBRATION_PEAK_PERCENTILE = 0.95f; // Use 95th percentile for thresholds
|
||||
|
||||
// Note Detection Timing
|
||||
constexpr int NOTE_PRINT_INTERVAL_MS = 100; // How often to print detected notes
|
||||
constexpr int MIN_NOTE_DURATION_MS = 50; // Minimum duration to consider a note valid
|
||||
constexpr int NOTE_RELEASE_TIME_MS = 100; // Time before considering a note released
|
||||
|
||||
// Serial Commands
|
||||
constexpr char CMD_HELP = 'h'; // Show help
|
||||
constexpr char CMD_CALIBRATE = 'c'; // Start calibration
|
||||
constexpr char CMD_THRESHOLD_UP = '+'; // Increase sensitivity
|
||||
constexpr char CMD_THRESHOLD_DOWN = '-'; // Decrease sensitivity
|
||||
constexpr char CMD_TOGGLE_SPECTRUM = 's'; // Toggle spectrum display
|
||||
constexpr float THRESHOLD_STEP = 0.1f; // Threshold adjustment step
|
||||
|
||||
// Command Response Messages
|
||||
constexpr const char* MSG_HELP =
|
||||
"Commands:\n"
|
||||
"h - Show this help\n"
|
||||
"c - Start calibration\n"
|
||||
"+ - Increase sensitivity\n"
|
||||
"- - Decrease sensitivity\n"
|
||||
"s - Toggle spectrum display\n";
|
||||
}
|
||||
Reference in New Issue
Block a user