diff options
author | grm <grm@eyesin.space> | 2025-02-22 03:40:43 +0200 |
---|---|---|
committer | grm <grm@eyesin.space> | 2025-02-22 03:40:43 +0200 |
commit | 35208c579c9e7d0078d786e61f4a323919e2dcdf (patch) | |
tree | 022f464e6350c18f76443a6e96eccd02698df7d1 | |
parent | 500b9a07b93d6cd3e771edc5698e06d163da60f1 (diff) | |
download | synth-project-35208c579c9e7d0078d786e61f4a323919e2dcdf.tar.gz synth-project-35208c579c9e7d0078d786e61f4a323919e2dcdf.tar.bz2 synth-project-35208c579c9e7d0078d786e61f4a323919e2dcdf.zip |
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | b.c | 10 | ||||
-rw-r--r-- | src/midi.c | 6 | ||||
-rw-r--r-- | src/sound.c | 14 | ||||
-rw-r--r-- | src/sound.h | 1 | ||||
-rw-r--r-- | src/synth_common.h | 2 | ||||
-rw-r--r-- | src/synth_engine.c | 545 | ||||
-rw-r--r-- | src/synth_engine.h | 4 | ||||
-rw-r--r-- | src/synth_gui.c | 47 | ||||
-rw-r--r-- | src/web.c | 18 |
11 files changed, 56 insertions, 594 deletions
@@ -18,3 +18,4 @@ config.status configure configure.scan .deps/ +build/ diff --git a/Makefile.am b/Makefile.am deleted file mode 100644 index a1489c9..0000000 --- a/Makefile.am +++ /dev/null @@ -1,2 +0,0 @@ -#SUBDIRS = src . tests -SUBDIRS = src @@ -4,7 +4,7 @@ #define BUILD_DIR "build/" -int debug_level = 5; +int debug_level = 0; void debug_or_release(B_Cmd* cmd) @@ -26,7 +26,7 @@ void inlcude_dirs(B_Cmd* cmd) { b_cmd_append(cmd, "-I./src/"); -} +} void cflags(B_Cmd* cmd) @@ -90,7 +90,7 @@ build_c(bool force, int dep_rebuild = 0; if (rebuild_is_needed == 0) dep_rebuild = b_needs_rebuild(output_path, dep_paths, dep_paths_len); - + if (rebuild_is_needed < 0 || dep_rebuild < 0) return false; if (force || rebuild_is_needed || dep_rebuild) { @@ -112,7 +112,7 @@ main(int argc, char *argv[]) B_GO_REBUILD_URSELF(argc, argv); const char *program_name = b_shift_args(&argc, &argv); - + (void)program_name; bool force = false; while (argc > 0) { @@ -174,7 +174,7 @@ main(int argc, char *argv[]) }; B_Cmd cmd = {0}; - + b_mkdir_if_not_exists(BUILD_DIR); if (!build_c(force, &cmd, synth_paths, B_ARRAY_LEN(synth_paths), synth_deps, @@ -2,6 +2,8 @@ #include "notes.h" #include "control.h" +#include <stdio.h> +#include <stdlib.h> #include <string.h> void midi_decode(uint32_t msg, synth_t * synth) { @@ -146,7 +148,7 @@ midiCallback(PtTimestamp timestamp, void *userData) { //printf("%d\n", buf.message); midi_decode(buf.message, m->synth); - + } void @@ -154,7 +156,7 @@ init_midi(midi_t *m, synth_t *synth) { m->stream = NULL; m->synth = synth; - + Pm_Initialize(); printf("midi devs: %d\n", Pm_CountDevices()); diff --git a/src/sound.c b/src/sound.c index 04b3f9b..9cdb75e 100644 --- a/src/sound.c +++ b/src/sound.c @@ -1,5 +1,8 @@ #include "sound.h" #include <portaudio.h> +#include <stdio.h> +#include <string.h> +#include <stdlib.h> static void @@ -65,13 +68,14 @@ init_sound(synth_t * synth, PaStreamCallback *streamCallback, const int device_i if (deviceInfo->maxOutputChannels == 0) { continue; } - //if (!strcmp("HyperX Cloud II Wireless: USB Audio (hw:2,0)", deviceInfo->name)) break; - printf("dev: %s || %f || %d channels\n", deviceInfo->name, deviceInfo->defaultSampleRate, deviceInfo->maxOutputChannels); - //if (!strcmp("HDA Intel PCH: ALC1220 Analog (hw:0,0)", deviceInfo->name)) break; + printf("dev: %s || %f || id:%d out:%d || lil:%f lol:%f\n", deviceInfo->name, + deviceInfo->defaultSampleRate, deviceInfo->maxInputChannels, + deviceInfo->maxOutputChannels, deviceInfo->defaultLowInputLatency, + deviceInfo->defaultLowOutputLatency); + if (c == device_id) break; c++; } - PaStreamParameters outputParameters; outputParameters.device = i; Pa_GetDefaultOutputDevice(); /* default output device */ @@ -94,7 +98,7 @@ init_sound(synth_t * synth, PaStreamCallback *streamCallback, const int device_i if (err != paNoError) { printf("Error opening stream with %s!!!!!", Pa_GetDeviceInfo(outputParameters.device)->name); } - + Pa_SetStreamFinishedCallback(synth->stream, &StreamFinished); Pa_StartStream(synth->stream); diff --git a/src/sound.h b/src/sound.h index 384c775..29ec561 100644 --- a/src/sound.h +++ b/src/sound.h @@ -2,7 +2,6 @@ #define SOUND_H #include <portaudio.h> -#include <string.h> #include "synth_engine.h" diff --git a/src/synth_common.h b/src/synth_common.h index b9d2b90..0110597 100644 --- a/src/synth_common.h +++ b/src/synth_common.h @@ -8,7 +8,7 @@ #define VIZ_BUF 1024 #define WIDTH 1024 -#define HEIGHT 600 +#define HEIGHT 1200 #define MIDI_NOTES 128 diff --git a/src/synth_engine.c b/src/synth_engine.c deleted file mode 100644 index 34f876a..0000000 --- a/src/synth_engine.c +++ /dev/null @@ -1,545 +0,0 @@ -#include "synth_engine.h" -#include "synth_math.h" -#include "lowpass.h" -#include "filter.h" -#include "control.h" -#include "sound.h" -#include "osc.h" - -#include <string.h> - -float -sin_sample(float amp, float freq, unsigned long long phase, unsigned int sample_rate) -{ - return amp * sinf(2.0 * M_PI * freq * ((float)phase / (float)sample_rate)); -} - -float -saw_sample(float amp, float freq, unsigned long long phase, unsigned int sample_rate) -{ - return amp * (0.17 * (1.0 - (2.0 * M_PI * freq * fmod((float)phase, (float)(sample_rate / (freq)))) / (float)sample_rate)); -} - -float -sawX_sample(float amp, float freq, float sm, unsigned long long phase, unsigned int sample_rate) -{ - float dOutput = 0.0; - for (float n = 1.0; n < sm; n++) - dOutput += (sinf(n * 2.0 * M_PI * freq * ((float)phase / (float)sample_rate))) / n; - return 0.5 * amp * dOutput; -} - -float -sqr_sample(float amp, float freq, float duty_cycle, unsigned long long phase, unsigned int sample_rate) -{ - if (duty_cycle < 0.0001 || duty_cycle > 0.9999) { - duty_cycle = 0.5; - } - - return (fmod((float)phase / (float)sample_rate, 1.0 / freq) < duty_cycle * (1.0 / freq)) ? amp : -amp; -} - -float -gen10(float f, unsigned long long phase, float x, unsigned int sample_rate) -{ - return fmodf(phase, sample_rate/f) / (sample_rate/f); -} - -float -gen110(float f, unsigned long long phase, float x, unsigned int sample_rate) -{ - float a = fmodf(phase, sample_rate/f); - return a > (sample_rate/f) / 2.0f ? 0.9999f : -0.9999f; -} - -float -gen0(float f, midi_note_t * midi_note, float x, unsigned int sample_rate) -{ - float sample = osc_sin(midi_note->wvt_index); - midi_note->wvt_index = osc_sin_next(f, midi_note->wvt_index); - return sample; -} - -float -gen1(float f, midi_note_t * midi_note, float x, unsigned int sample_rate) -{ - float sample = osc_saw(midi_note->wvt_index); - midi_note->wvt_index = osc_saw_next(f, midi_note->wvt_index); - return sample; -} - -float -gen2(float f, midi_note_t * midi_note, float x, unsigned int sample_rate) -{ - float sample = osc_weird(midi_note->wvt_index); - midi_note->wvt_index = osc_weird_next(f, midi_note->wvt_index); - return sample; -} - -float -gen3(float f, midi_note_t * midi_note, float x, unsigned int sample_rate) -{ - float sample = osc_tri(midi_note->wvt_index); - midi_note->wvt_index = osc_tri_next(f, midi_note->wvt_index); - return sample; -} - -float -gen4(float f, midi_note_t * midi_note, float x, unsigned int sample_rate) -{ - float sample = osc_sound(midi_note->wvt_index); - midi_note->wvt_index = osc_sound_next(f, midi_note->wvt_index); - return sample; -} - -float -gen5(float f, midi_note_t * midi_note, float x, unsigned int sample_rate) -{ - float sample = osc_digisaw(midi_note->wvt_index); - midi_note->wvt_index = osc_digisaw_next(f, midi_note->wvt_index); - return sample; -} - -float -gen6(float f, midi_note_t * midi_note, float x, unsigned int sample_rate) -{ - float sample = osc_sqr(midi_note->wvt_index); - midi_note->wvt_index = osc_sqr_next(f, midi_note->wvt_index); - return sample; -} - -float -lfo(float f, unsigned long long phase, unsigned int sample_rate) { - return sin_sample(1, f, phase, sample_rate); -} - -int -notes_active(synth_t *synth) -{ - int flag = 0; - - for (int i = 0; i < MIDI_NOTES; i++) { - if (!synth->midi_note[i].active) - continue; - - if (!fix_adsr(&synth->adsr, - (float)synth->midi_note[i].noteOn, - (float)synth->midi_note[i].noteOff, - synth->midi_note[i].elapsed, - synth->midi_note[i].noteOffSample) - && synth->midi_note[i].noteOff != 0) { - synth->midi_note[i].freq = 0; - synth->midi_note[i].channel = -1; - synth->midi_note[i].noteOn = -1; - synth->midi_note[i].noteOff = -1; - synth->midi_note[i].wvt_index = 0; - synth->midi_note[i].lfo_index = 0; - synth->midi_note[i].velocity = -1; - synth->midi_note[i].elapsed = -1; - synth->midi_note[i].noteOffSample = 0; - synth->midi_note[i].active = 0; - } else { - flag++; - } - } - - return flag; -} - -float lfo_index; - -float -make_sample(void *synthData, unsigned int sample_rate, int frame) -{ - synth_t *synth = (synth_t*)synthData; - float sample = 0.0f; - - float n = notes_active(synth); - if (n == 0) return sample; - - float rms = 0; - for (int i = 0; i < MIDI_NOTES; i++) { - if (!synth->midi_note[i].active) - continue; - - rms += synth->midi_note[i].velocity * synth->midi_note[i].velocity; - } - - // if rms == 0 we can deduce there are no notes however notes_active handles - // stopping any already playing notes and if we remove it we need to make - // sure that notes stop some other way (it also happens every frame) - rms = sqrt(rms / (float)n); - // printf("rms %f\n", rms); - - for (int i = 0; i < MIDI_NOTES; i++) { - if (!synth->midi_note[i].active) - continue; - - float adsr = fix_adsr(&synth->adsr, - synth->midi_note[i].noteOn, - synth->midi_note[i].noteOff, - synth->midi_note[i].elapsed, - synth->midi_note[i].noteOffSample); - - float targ_freq = synth->midi_note[i].freq * cc_iget(&synth->cc_pitch, frame, FRAMES_PER_BUFFER); - targ_freq = targ_freq + targ_freq * - cc_iget(&synth->cc_lfo_amp, frame, FRAMES_PER_BUFFER) * - // (1.05946309436/24.0) * - osc_sin(synth->midi_note[i].lfo_index); - //(wvt_next(wvt_tri, (wvt_next(wvt_sound, 2 * cc_iget(&synth->cc_lfo_freq, frame, FRAMES_PER_BUFFER), SAMPLE_RATE, &lfo_index) / 2.0 + 0.5) * cc_iget(&synth->cc_lfo_freq, frame, FRAMES_PER_BUFFER), SAMPLE_RATE, &synth->midi_note[i].lfo_index) / 2.0 + 0.5); - - synth->midi_note[i].lfo_index = osc_sin_next(cc_iget(&synth->cc_lfo_freq, frame, FRAMES_PER_BUFFER), synth->midi_note[i].lfo_index); - /* float synth_sample = synth->osctri->ops->sample(synth->osctri, synth->midi_note[i].wvt_index); */ - /* synth->midi_note[i].wvt_index = synth->osctri->ops->next(synth->osctri, targ_freq, synth->midi_note[i].wvt_index); */ - float synth_sample = synth->gen[synth->geni](targ_freq, - &synth->midi_note[i], - synth->x, - sample_rate); - - sample += synth->midi_note[i].velocity * adsr * synth_sample; - } - - /* filter */ - if (synth->filter) { - // ALLL THE FILTERS - float cutoff = cc_iget(&synth->cc_cutoff, frame, FRAMES_PER_BUFFER); - float reso = round(cc_iget(&synth->cc_resonance, frame, FRAMES_PER_BUFFER)); - - //cutoff = cutoff + cutoff * (synth->lfo.amp) * lfo(cc_iget(&synth->cc_lfo_freq, frame, FRAMES_PER_BUFFER), synth->lfo.elapsed, sample_rate); - if (cutoff == 0) cutoff = 0.001; - lpf_update(reso, cutoff, sample_rate); - sample = lpf_filter(sample); - - update_bw_low_pass_filter(synth->fff, SAMPLE_RATE,cutoff, reso); - sample = bw_low_pass(synth->fff, sample); - } - - sample = synth->gain * sample; - - // band stop for high freqs - sample = bw_band_stop(synth->fff2, sample); - - if (synth->clamp) sample = clamp(sample, -1, 1); - - return sample; -} - -void -add_to_viz(synth_t *synth, float sample) -{ - synth->viz.wave[synth->viz.wi++] = sample; - - if (synth->viz.wi >= VIZ_BUF) { - synth->viz.wi = 0; - } -} - -void -add_to_fftviz(synth_t *synth, float sample) -{ - synth->fftviz.wave[synth->fftviz.wi++] = sample; - - if (synth->fftviz.wi >= VIZ_BUF) { - synth->fftviz.wi = 0; - } -} - -void -add_to_delay(synth_t *synth, float sample) -{ - synth->del[synth->deli++] = sample; - - if (synth->deli >= SAMPLE_RATE * 10) { - synth->deli = 0; - } -} - -void -increment_synth(synth_t *synth) -{ - synth->lfo.elapsed++; - synth->adsr.elapsed++; - for (int i = 0; i < MIDI_NOTES; i++) { - if (!synth->midi_note[i].active) - continue; - - synth->midi_note[i].elapsed++; - } -} - -void -get_frame(void *outputBuffer, synth_t *synth, int i) -{ - float *out = (float*)outputBuffer + 2 * i; - float s = 0.0f; - - - if (!synth->active) { - lfo_index = 0; - } - - if (!notes_active(synth)) { - synth->active = 0; - lfo_index = 0; - } - - if (!synth->delay) { - synth->counter = 0; - } - - s = make_sample(synth, SAMPLE_RATE, i); - synth->counter++; - if (synth->counter >= (int)(synth->del_time * SAMPLE_RATE * 10)) { - int idx = (synth->deli - (int)(synth->del_time * SAMPLE_RATE * 10)) % (SAMPLE_RATE * 10); - float tmp; - if (idx >= 0) { - tmp = synth->del[idx]; - } else { - tmp = synth->del[SAMPLE_RATE * 10 + idx]; - } - - s = clamp(s + synth->del_feedback * tmp, -1, 1); - } - - add_to_delay(synth, s); - *out++ = s; - *out++ = s; - - // move time - increment_synth(synth); - - // viz - add_to_viz(synth, s); -} - -#include "fftw3.h" - -int -get_spectrum(synth_t * synth, float * buffer) -{ - int fft_output_len = FRAMES_PER_BUFFER / 2 + 1; - float input[FRAMES_PER_BUFFER]; - - for( unsigned long i=0; i < FRAMES_PER_BUFFER * 2; i += 2 ) { - input[i / 2] = buffer[i]; - } - fftwf_complex* output = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * fft_output_len); - - fftwf_plan forward_plan = fftwf_plan_dft_r2c_1d(fft_output_len, input, output, FFTW_ESTIMATE); - - fftwf_execute(forward_plan); - - for (int i=0; i < fft_output_len - 2*fft_output_len/3; i++ ) { - add_to_fftviz(synth, sqrt(output[i][0] * output[i][0] + output[i][1] * output[i][1])); - } - - fftwf_destroy_plan(forward_plan); - fftwf_free(output); -} - -int -process(synth_t * synth, float * buffer) -{ - int len = FRAMES_PER_BUFFER; - - float buf[FRAMES_PER_BUFFER]; - - for( unsigned long i=0; i < len * 2; i += 2 ) { - buf[i / 2] = buffer[i]; - } - - fftwf_complex* frequency_signal = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * (len / 2 + 1)); - //double* time_signal = (double*)fftwf_malloc(sizeof(double) * len); - - fftwf_plan forward_plan = fftwf_plan_dft_r2c_1d(len, buf, frequency_signal, FFTW_ESTIMATE); - fftwf_plan backward_plan = fftwf_plan_dft_c2r_1d(len, frequency_signal, buf, FFTW_ESTIMATE); - - fftwf_execute(forward_plan); - - // cont: - /* printf("ASDASD!!!\n"); */ - /* for (int i=0; i < (len / 2 + 1); i++ ) { */ - /* if (frequency_signal[i][0] > 1) { */ - /* frequency_signal[i][0] = 0; */ - /* } */ - /* if (frequency_signal[i][1] > 1) { */ - /* frequency_signal[i][1] = 0; */ - /* } */ - /* printf("%f %f | \n", sqrt(frequency_signal[i][0] * frequency_signal[i][0] + frequency_signal[i][1] * frequency_signal[i][1]), atan2(frequency_signal[i][1], frequency_signal[i][0])); */ - - /* /\* float f = atan2(frequency_signal[i][1], frequency_signal[i][0]); *\/ */ - /* /\* if (f > M_PI / 2 && f < 2 * M_PI / 3) { *\/ */ - /* /\* frequency_signal[i][0] = 0.0; *\/ */ - /* /\* frequency_signal[i][1] = 0.0; *\/ */ - /* /\* } *\/ */ - /* } */ - - /* printf("ASDASD!!!\n"); */ - fftwf_execute(backward_plan); - - for (unsigned long i=0; i < len * 2; i += 2) { - buffer[i] = buf[i / 2] / len ; - buffer[i + 1] = buf[i / 2] / len ; - } - - fftwf_destroy_plan(forward_plan); - fftwf_destroy_plan(backward_plan); - fftwf_free(frequency_signal); - //fftwf_free(time_signal); - - /* for( unsigned long i=0; i < len * 2; i += 2 ) { */ - /* buf[i / 2] = buffer[i]; */ - /* } */ - /* float kernel[7] = {.01, .88, .21, .36, .21, .13, .01}; */ - /* float res[len + 7 - 1]; */ - /* convole(buf, kernel, len, 7, res); */ - - /* int N = (len + 7 - 1); */ - /* int M = len; */ - - /* float ratio = (float) N / M; */ - - /* for (int i = 0; i < M; i++) { */ - /* int index = (int)(i * ratio); */ - /* buf[i] = res[index]; */ - /* } */ - - /* for (unsigned long i=0; i < len * 2; i += 2) { */ - /* buffer[i] = buf[i / 2] ; */ - /* buffer[i + 1] = buf[i / 2] ; */ - /* } */ - - return 0; -} - -int -sound_gen(const void *inputBuffer, void *outputBuffer, - unsigned long framesPerBuffer, - const PaStreamCallbackTimeInfo* timeInfo, - PaStreamCallbackFlags statusFlags, - void *synthData) -{ - synth_t *synth = (synth_t*)synthData; - float *out = (float*)outputBuffer; - - float buffer[2 * FRAMES_PER_BUFFER]; - - (void) timeInfo; - (void) statusFlags; - (void) inputBuffer; - - // get_changes(); - for (int i = 0; i < synth->cci; i++) { - cc_prep(synth->ccs[i]); - } - // fill buffer - for( unsigned long i=0; i<framesPerBuffer; i++ ) { - // use iget inside - get_frame(buffer, synth, i); - } - // process buffer - // process(synth, buffer); - get_spectrum(synth, buffer); - - // output buffer - for( unsigned long i=0; i<framesPerBuffer * 2; i += 2 ) { - // use iget inside - *out++ = buffer[i]; - *out++ = buffer[i+1]; - } - // finalize_changes(); - for (int i = 0; i < synth->cci; i++) { - cc_fix(synth->ccs[i]); - } - return paContinue; -} - -void -init_synth(synth_t * synth) -{ - synth->cci = 0; - CC(synth->cc_cutoff, "cutoff", 10, 5000, 30, 5000); - CC(synth->cc_resonance, "resonance", 1, 10, .02, 1); - //CC(synth->cc_lfo_freq, "lfo_freq", .01, 10, .02, 1); - CC(synth->cc_lfo_freq, "lfo_freq", 1, 1000, 2, 1); - CC(synth->cc_lfo_amp, "lfo_amp", 0, 1, .01, 0); - CC(synth->cc_pitch, "pitch", -3, 4, 0.01, 1); - CC(synth->cc_adsr_a, "attack", 0, 3, 0.05, 0.00); - CC(synth->cc_adsr_d, "decay", 0, 2, 0.05, 0.3); - CC(synth->cc_adsr_s, "sustain", 0, 1.0f, 0.05f, 0.7f); - CC(synth->cc_adsr_r, "release", 0, 5, 0.05f, 0.2f); - - synth->modi = 0; - - synth->gain = 0.5; - synth->x = 1; - - synth->adsr.a = 0.00001f; - synth->adsr.peak = 1.0f; - synth->adsr.d = 0.3; - synth->adsr.s = 0.7; - synth->adsr.r = 0.4; - synth->adsr.elapsed = 0; - - synth->lfo.freq = 1.0f; - synth->lfo.amp = 0.0f; - synth->lfo.elapsed = 0; - - for (int i =0; i<MIDI_NOTES; i++) { - synth->midi_note[i].freq = 0; - synth->midi_note[i].channel = -1; - synth->midi_note[i].noteOn = -1; - synth->midi_note[i].noteOff = -1; - synth->midi_note[i].velocity = -1; - synth->midi_note[i].elapsed = -1; - synth->midi_note[i].active = 0; - synth->midi_note[i].wvt_index = 0; - synth->midi_note[i].lfo_index = 0; - synth->midi_note[i].adsr = &(synth->adsr); - } - - synth->octave = 3; - - synth->delay = 0; - synth->del[SAMPLE_RATE * 10]; - synth->deli = 0; - synth->del_time = .1; - synth->del_feedback = 0.5f; - synth->counter; - - synth->filter = 1; - synth->clamp = 1; - - synth->gen[0] = gen0; - synth->gen[1] = gen1; - synth->gen[2] = gen2; - synth->gen[3] = gen3; - synth->gen[4] = gen4; - synth->gen[5] = gen5; - synth->gen[6] = gen6; - synth->geni = 3; - - synth->active = 0; - - synth->viz.sample_rate_divider = 1; - synth->viz.wi = 0; - synth->fftviz.sample_rate_divider = 1; - synth->fftviz.wi = 0; - - lpf_init(); - synth->fff = create_bw_low_pass_filter(2, SAMPLE_RATE, 400); - synth->fff2 = create_bw_band_stop_filter(8, SAMPLE_RATE, 15000, 22000); - - init_sound(synth, sound_gen); - - synth->osctri = make_tri("triangle"); -} - -void -free_synth(synth_t * synth) -{ - destroy_sound(synth); - - free_bw_low_pass(synth->fff); - free_bw_band_stop(synth->fff2); -} diff --git a/src/synth_engine.h b/src/synth_engine.h index 5ff7d8d..16c2135 100644 --- a/src/synth_engine.h +++ b/src/synth_engine.h @@ -1,17 +1,13 @@ #ifndef SYNTH_ENGINE_H #define SYNTH_ENGINE_H -#include <stdlib.h> -#include <stdio.h> #include <math.h> #include <portaudio.h> #include "synth_common.h" -#include "notes.h" #include "filter.h" #include "adsr.h" #include "control.h" -#include "osc.h" #include "pa_ringbuffer.h" #ifndef M_PI diff --git a/src/synth_gui.c b/src/synth_gui.c index 6f8e06b..cb899db 100644 --- a/src/synth_gui.c +++ b/src/synth_gui.c @@ -1,4 +1,5 @@ #include "synth_gui.h" +#include "osc.h" #include <portaudio.h> #define RAYGUI_IMPLEMENTATION #include "raygui.h" @@ -113,7 +114,7 @@ keyboard(synth_t * synth) //note = notes[i % 12][(synth->octave + (i / 12)) % 8]; } } - + int patates[] = {KEY_Z, KEY_X, KEY_C, KEY_V, KEY_B, KEY_N, KEY_M, 0}; synth->modi = 0; for (int i = 0; patates[i]; i++) { @@ -121,7 +122,7 @@ keyboard(synth_t * synth) synth->modifiers[synth->modi++] = patates[i]; } } - + if (IsKeyDown(265)) { // up for (int i = 0; i < synth->modi; i++) { if (synth->modifiers[i] == KEY_Z) { @@ -246,7 +247,7 @@ draw_adsr(synth_t *synth, int x, int y, int width, int height) int x_prev = x; for (int i = 0; i < synth->midi_active_n; i++) { int rec_y = y + height - 1 + - 4.5 * floor( (width / 24) * + 4.5 * floor( (width / 24.0) * - fix_adsr(&synth->adsr, synth->midi_active[i]->noteOn, synth->midi_active[i]->noteOff, @@ -280,7 +281,7 @@ draw_wave(synth_t *synth, int x, int y, int width, int height) col = MAGENTA; else col = WHITE; - DrawPixel(i + x , y + height / 2 + floor(50 * synth->viz.wave_viz_buffer[ii + j]), col); + DrawPixel(i + x , y + height / 2 + (int)floor(50.0 * synth->viz.wave_viz_buffer[ii + j]), col); } } /* for (int j = 0; j < 100; j++) { */ @@ -297,13 +298,13 @@ draw_fft(synth_t *synth, int x, int y, int width, int height) size_t fft_output_len = viz_size / 2 + 1; fftwf_complex* output = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * fft_output_len); - + fftwf_plan forward_plan = fftwf_plan_dft_r2c_1d(fft_output_len, synth->viz.fft_input_buffer, output, FFTW_ESTIMATE); fftwf_execute(forward_plan); fftwf_destroy_plan(forward_plan); - + // "Squash" into the Logarithmic Scale float step = 1.06; float lowf = 1.0f; @@ -343,7 +344,7 @@ draw_fft(synth_t *synth, int x, int y, int width, int height) Color color = ColorFromHSV(hue*360, saturation, value); Vector2 startPos = { x + i*cell_width + cell_width/2, - y + height - height*2/3*t, + y + height - height*2/3.0*t, }; Vector2 endPos = { x + i*cell_width + cell_width/2, @@ -394,9 +395,9 @@ draw_osc(synth_t * synth, int x, int y, int width, int height) if (synth->midi_active_n) { for (int i = 0; i < width; i++) { - DrawPixel(i + x , y + height / 2 + floor(height/2 * osc_wave[i] / max), RED); + DrawPixel(i + x , y + height / 2.0 + floor(height/2.0 * osc_wave[i] / max), RED); } - } + } } @@ -454,7 +455,7 @@ draw_adsr_graph(synth_t * synth, int x, int y, int width, int height) for (int i = 0; i < width; i++) { DrawPixel(i + x , y + height - adsr_graph[i], RED); } - + for (int i = 0; i < synth->midi_active_n; i++) { midi_note_t * note = synth->midi_active[i]; int elapsed_samples = note->elapsed; @@ -486,7 +487,7 @@ draw_signals(synth_t * synth, int x, int y, int width, int height) synth->viz.rate_divider = GuiSlider((Rectangle){ x + (width / 2.0) / 2, y - 12, width / 2.0, 12 }, "", NULL, synth->viz.rate_divider , 1, 150); int viz_size = width * synth->viz.rate_divider; - + float samples[RING_SIZE]; int rc = PaUtil_ReadRingBuffer( &synth->viz.wave_buffer, &samples, RING_SIZE); @@ -531,8 +532,8 @@ draw_signals(synth_t * synth, int x, int y, int width, int height) } } -char * flag = NULL; -char * flag_circle = NULL; +char * flag = NULL; +char * flag_circle = NULL; void draw_cc_circle(cc_t * cc, int x, int y, int width, int height) { @@ -553,15 +554,15 @@ draw_cc_circle(cc_t * cc, int x, int y, int width, int height) { } if (dx.y < 0) cc_step(cc, 1*x); if (dx.y > 0) cc_step(cc, -1*x); - } + } if (IsMouseButtonReleased(0) && flag_circle == cc->name) { flag_circle = 0; } int min = 110; int max = 110 + (360+70 - 110) * (cc->target) / (cc->max - cc->min); - DrawRing((Vector2){x + width/2, y + height/2}, width / 2 - 6, width / 2, min, max, 0, Fade(MAROON, 0.7f)); - DrawCircle(x + width/2, y + height/2, width / 2 - 5, BLACK); // Draw circle sector outline + DrawRing((Vector2){x + width/2.0, y + height/2.0}, width / 2.0 - 6, width / 2.0, min, max, 0, Fade(MAROON, 0.7f)); + DrawCircle(x + width/2, y + height/2, width / 2.0 - 5, BLACK); // Draw circle sector outline char buf[32]; snprintf(buf, sizeof buf, "%0.2f", cc->target); @@ -603,7 +604,7 @@ draw_cc_hbar(cc_t * cc, int x, int y, int width, int height) { char tmp[128] = ""; sprintf(tmp, "%f", cc->value); ws_send_message(tmp); - } + } if (IsMouseButtonReleased(0) && flag == cc->name) { flag = 0; } @@ -645,7 +646,7 @@ draw_cc_vbar(cc_t * cc, int x, int y, int width, int height) { //cc->target = cc->min + cc->max - (cc->min + (cc->max - cc->min) * ((((p.y - y) * (float)1/height) - 0) / (1 - 0))); cc_set(cc, (cc->min + cc->max - (cc->min + (cc->max - cc->min) * ((((p.y - y) * (float)1/height) - 0) / (1 - 0))))); } - } + } if (IsMouseButtonReleased(0) && flag == cc->name) { flag = 0; } @@ -667,7 +668,7 @@ void draw_bars(synth_t * synth, int x, int y, int width, int height, int offset) { int count = 0; - + draw_cc_hbar(&synth->cc_adsr_a , x, y + count++ * (height + offset), width, height); synth->f_adsr_enabled = GuiCheckBox((Rectangle){ x + width + offset, y + (count - 1) * (height + offset), height, 16 }, "", synth->f_adsr_enabled); draw_cc_hbar(&synth->cc_adsr_peak , x, y + count++ * (height + offset), width, height); @@ -696,7 +697,7 @@ rayrun(synth_t *synth){ int old_soundcard_id = synth->soundcard_id; int old_midi_device_id = synth->midi_device_id; - InitWindow(WIDTH, HEIGHT, "Raylib synth"); + InitWindow(WIDTH, HEIGHT, "Raylib synth"); SetTargetFPS(60); // Set our game to run at 60 frames-per-second while (!WindowShouldClose()) { keyboard(synth); @@ -735,7 +736,7 @@ rayrun(synth_t *synth){ synth->wvt_pos = GuiSlider((Rectangle){WIDTH / 2.0 - 108, 150 + 42 + 42, 216, 24 }, "", buf, synth->wvt_pos , 0, 127); set_sound_start(synth->wvt_pos*2048); set_sound_len(synth->wvt_pos*2048 + 2048); - + draw_bars(synth, 20, 20, 200, 16, 3); draw_text(synth, WIDTH / 2 - 108, 20); @@ -765,7 +766,7 @@ rayrun(synth_t *synth){ synth->clamp = GuiToggle((Rectangle){ WIDTH - 100 - 50, 50 + 24 + 6 + 24 + 6, 100, 24 }, "clamp", synth->clamp); synth->delay = GuiToggle((Rectangle){ WIDTH - 100 - 50, 50 + 24 + 6 + 24 + 6 + 24 + 6, 100, 24 }, "delay", synth->delay); - + if ( GuiButton((Rectangle){ WIDTH - 100 - 50, 50 + 24 + 6 + 24 + 6 + 24 + 6 + 24 + 6 + 24 + 6, 100, 24 }, "SAVE!")) { save_synth(synth, "asdas"); } @@ -773,7 +774,7 @@ rayrun(synth_t *synth){ if ( GuiButton((Rectangle){ WIDTH - 100 - 50, 50 + 24 + 6 + 24 + 6 + 24 + 6 + 24 + 6 + 24 + 6 + 24 + 6, 100, 24 }, "LOAD!")) { load_synth(synth, "asdas"); } - + static int edit_midi = 0; static int edit_sound = 0; @@ -104,6 +104,10 @@ enum MHD_Result handle_request(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls) { + (void)version; + (void)upload_data; + (void)upload_data_size; + (void)con_cls; struct MHD_Response *response; enum MHD_Result ret; @@ -126,7 +130,7 @@ enum MHD_Result handle_request(void *cls, struct MHD_Connection *connection, MHD_destroy_response(response); return ret; } - + response = MHD_create_response_from_buffer(strlen(html_content), (void *)html_content, MHD_RESPMEM_PERSISTENT); ret = MHD_queue_response(connection, MHD_HTTP_OK, response); MHD_destroy_response(response); @@ -136,6 +140,7 @@ enum MHD_Result handle_request(void *cls, struct MHD_Connection *connection, // Callback to handle WebSocket events static int callback_ws(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) { + (void)user; char buf[10000] = ""; char tmp[10000] = ""; switch (reason) { @@ -175,10 +180,10 @@ static int callback_ws(struct lws *wsi, enum lws_callback_reasons reason, lws_write(wsi, (unsigned char *)buf, strlen(buf), LWS_WRITE_HTTP); return -1; break; - } + } case LWS_CALLBACK_CLOSED: { printf("WebSocket connection closed with client: %p\n", wsi); - } + } default: break; } @@ -187,12 +192,13 @@ static int callback_ws(struct lws *wsi, enum lws_callback_reasons reason, // Thread function to run the WebSocket server void *websocket_server_thread(void *arg) { + (void)arg; struct lws_context_creation_info info; struct lws_context *context; struct lws_protocols protocols[] = { //{ "http-only", callback_http, 0, 0 }, - { "ws", callback_ws, 0, 128 }, - { NULL, NULL, 0, 0 } // Terminator + { "ws", callback_ws, 0, 128, 0, 0, 0 }, + { NULL, NULL, 0, 0, 0, 0, 0 } // Terminator }; memset(&info, 0, sizeof(info)); @@ -251,7 +257,7 @@ void ws_send_message(const char *message) { memcpy(&buffer[LWS_PRE], message, msg_len); lws_write(client_wsi, &buffer[LWS_PRE], msg_len, LWS_WRITE_TEXT); printf("[WS]: Sent <<%s>>\n", message); - + //lws_write(client_wsi, (unsigned char *)message, strlen(message), LWS_WRITE_TEXT); } } |