Files
sound-scope-convert/scope.h
2026-11-14 11:52:18 -08:00

322 lines
11 KiB
C++

#ifndef SCOPE
#define SCOPE
#include <pulse/simple.h>
#include <pulse/error.h>
#include <fftw3.h>
#include <alsa/asoundlib.h>
#define SAMPLE_RATE 44100
#define FRAME_TIME 17
// this must be in power of 2 for fourier transform
#define READ_BUFFER_SIZE 2048
#define INPUT_SOUND_DATA_TYPE Sint16
#define ALSA_BUFFER_FRAMES 2048
#define TOTAL_ALSA_BUFFER_SIZE_IN_WORDS (ALSA_BUFFER_FRAMES * 2)
#define FFT_NUMBER_READ_BUFFERS 5
#define FFT_BUFFER_SIZE (READ_BUFFER_SIZE * FFT_NUMBER_READ_BUFFERS)
#define MAIN_BUFFER_SIZE 8192
#define AUDIO_BUFFER_SIZE 6000
#define FFT_START_BUCKET 0
#define MAIN_VOLUME .01
#define SPECTRUM_VOLUME .1
#define PIANO_SIZE_MULTIPLY .35
#define KEYBOARD_POSITION_X 50
#define KEYBOARD_POSITION_Y 10
#define OCTIVE_POSITION_X 400
#define OCTIVE_POSITION_Y 10
#define NUMBER_KEY_LIGHTS 12
#define NUMBER_OCTIVE_LIGHTS 8
#define NUMBER_OCTIVES 6
#define NOTES_PER_OCTIVE 12
#define TOTAL_NOTES NOTES_PER_OCTIVE*NUMBER_OCTIVES
// 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; // 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 {
public:
main_window_class (int x_main_window_size, int y_main_window_size);
private:
SDL_Window *window_for_main;
SDL_Renderer *renderer_for_main;
int call_result;
};
class alsa_read_class {
public:
alsa_read_class();
INPUT_SOUND_DATA_TYPE *initialize_alsa(char *device_name);
int read_alsa();
// Finds the first USB audio card with a capture device and writes
// "plughw:CARD=<id>,DEV=<n>" into name. Returns 0 on success, -1 if none.
static int find_usb_capture_device(char *name, size_t name_size);
private:
INPUT_SOUND_DATA_TYPE *alsa_input_data;
snd_pcm_t *capture_handle;
snd_pcm_hw_params_t *hw_params;
snd_pcm_format_t format;
int function_return;
int i;
int err;
unsigned int rate = SAMPLE_RATE;
int buffer_frames = ALSA_BUFFER_FRAMES;
long min, max;
snd_mixer_t *mixer_handle;
snd_mixer_selem_id_t *sid;
const char *card = "CMTECK,DEV=0";
const char *selem_name = "Capture";
};
class audio_window_class {
public:
audio_window_class (int x_main_window_size, int y_main_window_size);
int draw_trace(int *audio_data, int audio_data_size);
int get_max_audio_trace_width();
int get_max_audio_trace_height();
private:
int x_audio_window_position;
int y_audio_window_position;
int x_audio_window_size;
int y_audio_window_size;
int x_audio_window_trace_base;
int y_audio_window_trace_base;
int max_audio_trace_width;
int max_audio_trace_height;
SDL_Window *window_for_audio;
SDL_Renderer *renderer_for_audio;
SDL_Rect rect_for_audio;
SDL_Texture *texture_for_audio;
SDL_Surface *surface_for_audio;
TTF_Font *font_for_audio;
SDL_Color color_for_audio;
};
class spectrum_window_class {
public:
spectrum_window_class (int x_main_window_size, int y_main_window_size);
int draw_trace(int *spectrum_data, int spectrum_data_size);
int get_max_spectrum_trace_width();
int get_max_spectrum_trace_height();
private:
int x_spectrum_window_position;
int y_spectrum_window_position;
int x_spectrum_window_size;
int y_spectrum_window_size;
int x_spectrum_window_trace_base;
int y_spectrum_window_trace_base;
int max_spectrum_trace_width;
int max_spectrum_trace_height;
SDL_Window *window_for_spectrum;
SDL_Renderer *renderer_for_spectrum;
SDL_Rect rect_for_spectrum;
SDL_Texture *texture_for_spectrum;
SDL_Surface *surface_for_spectrum;
TTF_Font *font_for_spectrum;
SDL_Color color_for_spectrum;
};
class piano_window_class {
public:
piano_window_class (int x_main_window_size, int y_main_window_size);
int draw_trace(piano_data_struct *input_piano_data);
int get_max_piano_trace_width();
int get_max_piano_trace_height();
private:
const char* keyboard_lights[12] = {
"/home/maallyn/sound-scope/keyboard-key1.bmp",
"/home/maallyn/sound-scope/keyboard-key2.bmp",
"/home/maallyn/sound-scope/keyboard-key3.bmp",
"/home/maallyn/sound-scope/keyboard-key4.bmp",
"/home/maallyn/sound-scope/keyboard-key5.bmp",
"/home/maallyn/sound-scope/keyboard-key6.bmp",
"/home/maallyn/sound-scope/keyboard-key7.bmp",
"/home/maallyn/sound-scope/keyboard-key8.bmp",
"/home/maallyn/sound-scope/keyboard-key9.bmp",
"/home/maallyn/sound-scope/keyboard-key10.bmp",
"/home/maallyn/sound-scope/keyboard-key11.bmp",
"/home/maallyn/sound-scope/keyboard-key12.bmp"};
const char* octive_lights[8] = {
"/home/maallyn/sound-scope/octive-1.bmp",
"/home/maallyn/sound-scope/octive-2.bmp",
"/home/maallyn/sound-scope/octive-3.bmp",
"/home/maallyn/sound-scope/octive-4.bmp",
"/home/maallyn/sound-scope/octive-5.bmp",
"/home/maallyn/sound-scope/octive-6.bmp",
"/home/maallyn/sound-scope/octive-7.bmp",
"/home/maallyn/sound-scope/octive-8.bmp"};
struct piano_data_struct *my_piano_data;
int x_piano_window_position;
int y_piano_window_position;
int x_piano_window_size;
int y_piano_window_size;
int x_piano_window_trace_base;
int y_piano_window_trace_base;
int max_piano_trace_width;
int max_piano_trace_height;
int function_result;
int loop_counter;
SDL_Window *window_for_piano;
SDL_Renderer *renderer_for_piano;
SDL_Rect rect_for_piano_text;
SDL_Texture *texture_for_piano;
SDL_Surface *surface_for_piano_text;
SDL_Surface *surface_key_blank;
SDL_Surface *surface_keys[12];
SDL_Surface *surface_octive_blank;
SDL_Surface *surface_octives[8];
SDL_Texture *texture_key_blank;
SDL_Texture *texture_keys[12];
SDL_Texture *texture_octive_blank;
SDL_Texture *texture_octives[8];
SDL_Texture *texture_current_piano_light;
SDL_Texture *texture_current_octive_light;
SDL_Rect rect_for_keys;
SDL_Rect rect_for_octives;
TTF_Font *font_for_piano;
SDL_Color color_for_piano;
SDL_Rect rect_for_piano_image;
SDL_Surface *piano_image_surface;
SDL_Texture *texture_of_piano_image;
};
class main_buffer_manager_class {
public:
main_buffer_manager_class();
~main_buffer_manager_class();
Sint16 *initial_preparation();
Sint16 *read_main_buffer();
private:
Sint16 *my_read_main_buffer;
int pulse_error;
int function_return;
pa_simple *pulse_s = NULL;
int main_buffer_index;
Sint16 main_buffer_hold;
};
class audio_buffer_manager_class {
public:
audio_buffer_manager_class();
~audio_buffer_manager_class();
int initialize_audio(int horizontal_size,int vertical_size, Sint16 *main_buffer);
int *process_audio();
private:
Sint16 *my_main_buffer;
int *my_formatted_audio_buffer;
int my_audio_horizontal_size;
int my_audio_vertical_size;
int my_read_audio_index;
int function_return;
Sint16 audio_holding;
};
class spectrum_buffer_manager_class {
public:
spectrum_buffer_manager_class();
~spectrum_buffer_manager_class();
int initialize_spectrum(int horizontal_size,int vertical_size, Sint16 *main_buffer);
int *process_spectrum();
private:
Sint16 *my_main_buffer;
int *my_formatted_spectrum_buffer;
int my_spectrum_horizontal_size;
int my_spectrum_vertical_size;
int my_read_spectrum_index;
int my_output_fft_buffer_index;
int function_return;
int read_buffer_counter;
Sint16 spectrum_holding;
fftw_plan my_fftw_plan;
double *input_fftw;
double *input_fftw_write_position;
Sint16 main_buffer_hold;
fftw_complex *output_fftw;
};
class piano_process_class {
public:
piano_process_class();
~piano_process_class();
int initialize_piano_data();
struct piano_data_struct *process_piano_data(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 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