change to autocorrelate

This commit is contained in:
Mark Allyn
2026-11-14 11:52:18 -08:00
parent 3a6fa74735
commit 644a5d29de
3 changed files with 490 additions and 291 deletions

86
scope.h
View File

@@ -19,7 +19,6 @@
#define AUDIO_BUFFER_SIZE 6000
#define FFT_START_BUCKET 0
#define MAIN_VOLUME .01
#define PIANO_MIN_VOLUME 100.
#define SPECTRUM_VOLUME .1
#define PIANO_SIZE_MULTIPLY .35
#define KEYBOARD_POSITION_X 50
@@ -28,38 +27,52 @@
#define OCTIVE_POSITION_Y 10
#define NUMBER_KEY_LIGHTS 12
#define NUMBER_OCTIVE_LIGHTS 8
#define RADIANS_PER_SAMPLE_AT_1_HZ (double)6.28319/(double)SAMPLE_RATE
#define NUMBER_OCTIVES 6
#define BUCKETS_PER_NOTE 2
#define NOTES_PER_OCTIVE 12
#define BUCKETS_PER_OCTIVE BUCKETS_PER_NOTE*NOTES_PER_OCTIVE
#define TOTAL_BUCKETS BUCKETS_PER_OCTIVE*NUMBER_OCTIVES
#define TOTAL_NOTES NOTES_PER_OCTIVE*NUMBER_OCTIVES
#define Q_QUALITY 6
#define FIRST_FREQUENCY 32
#define PIANO_FULL_ARRAY_SIZE FFT_BUFFER_SIZE
//Structure for piano notes
struct piano_note_struct {
double note_strength;
int keyboard_key;
int octive;;
};
//Structure for processing buckets
struct bucket_data_struct {
double bucket_frequency;
double bucket_strength;
int buffer_size;
double radians_per_sample;
struct piano_note_struct *this_piano_note;
};
// Autocorrelation (McLeod NSDF) pitch detection settings, tuned for
// singing voice (the "piano" classes show the sung note on a keyboard)
// Analysis window: number of samples examined per pitch estimate.
// Must be a multiple of READ_BUFFER_SIZE and at least 2 * PIANO_MAX_LAG.
// It slides: each new read buffer shifts in and a new estimate is made.
#define PIANO_WINDOW_SIZE 4096
#define PIANO_FULL_ARRAY_SIZE PIANO_WINDOW_SIZE
// FFT size for autocorrelation; power of 2 and >= 2 * PIANO_WINDOW_SIZE
// so that the circular correlation does not wrap around
#define ACF_FFT_SIZE 8192
// Frequency of C1; note 0 (keyboard key 0, octive 0) is C1
#define PIANO_REFERENCE_FREQ 32.70319566
// Search range covering singing voice, low bass (~C2) to high soprano (~C6)
#define PIANO_MIN_FREQ 60.0
#define PIANO_MAX_FREQ 1100.0
#define PIANO_MIN_LAG ((int)((double)SAMPLE_RATE / PIANO_MAX_FREQ))
#define PIANO_MAX_LAG ((int)((double)SAMPLE_RATE / PIANO_MIN_FREQ) + 1)
// Max number of candidate peaks tracked in the NSDF
#define PIANO_MAX_PEAKS 128
// Silence gate: RMS of the (MAIN_VOLUME scaled) samples below this = no note
#define PIANO_MIN_RMS 5.0
// Highest NSDF peak must reach this for the sound to count as pitched (0..1);
// rejects breath and unvoiced consonants (s, sh, f)
#define PIANO_CLARITY_THRESHOLD 0.8
// Pick the first NSDF peak that is at least this fraction of the highest peak
#define PIANO_PEAK_THRESHOLD 0.9
// Smoothing: median of the last few voiced estimates (removes single frame
// octave jumps)
#define PIANO_MEDIAN_SIZE 3
// Hysteresis in semitones: the displayed note only changes once the sung
// pitch is more than this far from it (keeps vibrato from flickering)
#define PIANO_HYSTERESIS_SEMITONES 0.6
// Keep showing the note through this many unvoiced frames before blanking
#define PIANO_HOLD_FRAMES 2
// Structure used by both piano and main classes
struct piano_data_struct {
int keyboard_key;
int octive;
int keyboard_key; // 0 - 11 (C .. B); only meaningful when valid
int octive; // 0 - 5 (octave 1 .. 6); only meaningful when valid
int done;
int valid; // 1 if a pitched note was detected, 0 for silence/noise
double frequency; // detected fundamental in Hz (0 when not valid)
};
class main_window_class {
@@ -282,16 +295,27 @@ class piano_process_class {
~piano_process_class();
int initialize_piano_data();
struct piano_data_struct *process_piano_data(Sint16 *input_buffer);
double goertzel(int num_samples, int target_frequency, int sample_rate, Sint16 *input_buffer);
private:
int compute_nsdf();
double find_fundamental_lag();
double median_of_history();
int note_to_key_and_octive(int note_number, int *keyboard_key, int *octive);
struct piano_data_struct my_piano_data_struct;
Sint16 *local_main_buffer;
int ctr1;
struct piano_note_struct my_piano_notes[TOTAL_NOTES];
struct bucket_data_struct my_bucket_data[TOTAL_BUCKETS];
Sint16 full_size_buffer[PIANO_FULL_ARRAY_SIZE];
int main_buffer_read_count;
double window_samples[PIANO_WINDOW_SIZE];
double nsdf[PIANO_MAX_LAG + 2];
double *acf_time;
fftw_complex *acf_freq;
fftw_plan acf_forward_plan;
fftw_plan acf_inverse_plan;
// Smoothing state
double note_history[PIANO_MEDIAN_SIZE]; // fractional note numbers
int note_history_count;
int note_history_index;
int current_note; // displayed note, -1 = none
int unvoiced_frames;
};
#endif