summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorgramanas <anastasis.gramm2@gmail.com>2023-04-19 04:00:08 +0300
committergramanas <anastasis.gramm2@gmail.com>2023-04-19 04:00:08 +0300
commit1e374b426b4182c7b73b35219aadaca267b9623c (patch)
treee98b19e737c8744ca55952191e59e3d521fef68c
parent7cd2ef47ccf7730ada9a61deacfce6ea03b5516d (diff)
downloadsynth-project-1e374b426b4182c7b73b35219aadaca267b9623c.tar.gz
synth-project-1e374b426b4182c7b73b35219aadaca267b9623c.tar.bz2
synth-project-1e374b426b4182c7b73b35219aadaca267b9623c.zip
fake poly
-rw-r--r--src/synth_engine.c34
-rw-r--r--src/synth_engine.h5
-rw-r--r--src/synth_gui.c40
3 files changed, 67 insertions, 12 deletions
diff --git a/src/synth_engine.c b/src/synth_engine.c
index fc0a53d..6bd6bf6 100644
--- a/src/synth_engine.c
+++ b/src/synth_engine.c
@@ -127,10 +127,10 @@ gen1(float f, unsigned long long phase, float x, unsigned int sample_rate)
float
gen2(float f, unsigned long long phase, float x, unsigned int sample_rate)
{
- return sin_sample(0.5, f * sqrt(2) , phase, sample_rate)
- + sin_sample(0.5, f, phase, sample_rate);
+ /* return sin_sample(0.5, f * sqrt(2) , phase, sample_rate) */
+ /* + sin_sample(0.5, f, phase, sample_rate); */
- /* sawX_sample(1, synth->freq, 5, phase, sample_rate); */
+ return sawX_sample(1, f, 5, phase, sample_rate);
}
float
@@ -151,10 +151,14 @@ make_sample(unsigned long long phase, void *synthData, unsigned int sample_rate,
//LFO!
//if (synth->adsr.elapsed > SAMPLE_RATE / 2) synth->adsr.elapsed = 0;
-
- int n = 1;
- for (int i = 0; i < n; i++) {
- sample += (1.0 / n) * synth->gen[synth->geni](synth->n.freq + synth->freq_offset, phase, synth->x, sample_rate);
+
+ if (synth->poly) {
+ int n = synth->notes_active;
+ for (int i = 0; i < n; i++) {
+ sample += (1.0 / n) * synth->gen[synth->geni](synth->freq[i] + synth->freq_offset, synth->freq_count[i], synth->x, sample_rate);
+ }
+ } else {
+ sample = synth->gen[synth->geni](synth->n.freq + synth->freq_offset, phase, synth->x, sample_rate);
}
if (!viz && synth->filter) {
@@ -202,7 +206,7 @@ sound_gen(const void *inputBuffer, void *outputBuffer,
continue;
}
if (adsr_amplitude(synth, synth->adsr.elapsed) == 0 && synth->n.noteOff != 0) {
- printf("SYNTH OPFF\n");
+ //printf("SYNTH OPFF\n");
synth->active = 0;
*out++ = 0.0f;
*out++ = 0.0f;
@@ -213,7 +217,13 @@ sound_gen(const void *inputBuffer, void *outputBuffer,
*out++ = s;
synth->adsr.elapsed++;
synth->n.elapsed++;
+ for (int j = 0; j < synth->notes_active; j++) {
+ synth->freq_count[j]++;
+ }
if (!synth->multi) {
+ for (int j = 0; j < synth->notes_active; j++) {
+ if (synth->freq_count[j] >= (1.0 / synth->freq[i]) * SAMPLE_RATE) synth->freq_count[j] = 0;
+ }
if (synth->n.elapsed >= (1.0 / synth->n.freq) * SAMPLE_RATE) synth->n.elapsed = 0;
} else {
@@ -230,13 +240,18 @@ init_synth(synth_t * synth)
synth->gain = 1;
synth->x = 1;
+ synth->notes_active = 0;
+ for (int i = 0; i<100;i++) {
+ synth->freq[i] = 0;
+ synth->freq_count[i] = 0;
+ }
+
synth->n.freq = 0;
synth->n.noteOn = 0;
synth->n.noteOff = 1;
synth->n.key = 0;
synth->n.elapsed = 0;
-
synth->adsr.a = 0.0;
synth->adsr.peak = 1.0f;
synth->adsr.d = 0.3;
@@ -246,6 +261,7 @@ init_synth(synth_t * synth)
synth->octave = 3;
+ synth->poly = 0;
synth->multi = 0;
synth->filter = 0;
synth->cutoff = 22000.0f;
diff --git a/src/synth_engine.h b/src/synth_engine.h
index a540126..1191892 100644
--- a/src/synth_engine.h
+++ b/src/synth_engine.h
@@ -47,11 +47,16 @@ typedef struct {
float gain;
float x;
+ int notes_active;
+ float freq[100];
+ unsigned long long freq_count[100];
+
note_t n;
adsr_t adsr;
int octave;
+ int poly;
int multi;
int filter;
int clamp;
diff --git a/src/synth_gui.c b/src/synth_gui.c
index f5e02cf..d09a9b6 100644
--- a/src/synth_gui.c
+++ b/src/synth_gui.c
@@ -14,8 +14,16 @@ set_note(void *synthData, float note, PaTime time, int key)
synth->n.noteOn = time;
synth->n.noteOff = 0;
synth->n.elapsed = 0;
- synth->adsr.elapsed = 0;
+ if (synth->poly) {
+ if (!synth->notes_active) synth->adsr.elapsed = 0;
+ } else {
+ synth->adsr.elapsed = 0;
+ }
+
synth->active = 1;
+
+ synth->freq[synth->notes_active] = note;
+ synth->freq_count[synth->notes_active++] = 0;
}
@@ -33,15 +41,40 @@ keyboard(void *synthData, PaStream *stream)
note = notes[i % 12][(synth->octave + (i / 12)) % 8];
if (note) {
set_note(synth, note, Pa_GetStreamTime(stream), keys[i]);
- printf("Note On : %s[%d]\n", int_to_note(i % 12), (synth->octave + (i / 12)) % 8);
+ //printf("Note On : %s[%d] %fHz\n", int_to_note(i % 12), (synth->octave + (i / 12)) % 8, note);
+ }
+ printf("[");
+ for (int j = 0; j < synth->notes_active; j++) {
+ printf("%f ", synth->freq[j]);
}
+ printf("]\n");
}
}
for (int i = 0; keys[i]; i++) {
if (IsKeyReleased(keys[i])) {
+ note = notes[i % 12][(synth->octave + (i / 12)) % 8];
+ int j;
+ for (j = 0; j < synth->notes_active; j++) {
+ // printf("Comparing freq(j): %f to note: %f\n", synth->freq[j], note);
+ if (synth->freq[j] == note) {
+ synth->freq[j] = 0;
+ synth->notes_active--;
+ //printf("Note down %f\n", note);
+ break;
+ }
+ }
+ for (int k = j; k < synth->notes_active + 1; k++) {
+ //printf("moving %f <-- %f\n", synth->freq[k], synth->freq[k + 1]);
+ synth->freq[k] = synth->freq[k + 1];
+ }
+ printf("[");
+ for (int j = 0; j < synth->notes_active; j++) {
+ printf("%f ", synth->freq[j]);
+ }
+ printf("]\n");
if (synth->n.key == keys[i]) {
synth->n.noteOff = Pa_GetStreamTime(stream);
- printf("Note Off: %s[%d]\n", int_to_note(i % 12), (synth->octave + (i / 12)) % 8);
+ //printf("Note Off: %s[%d]\n", int_to_note(i % 12), (synth->octave + (i / 12)) % 8);
}
}
}
@@ -147,6 +180,7 @@ rayrun(void *synthData, PaStream *stream)
synth->multi = GuiToggle((Rectangle){ WIDTH - 100 - 50 - 100 - 50 , 50, 100, 24 }, "!MULTI!", synth->multi);
synth->filter = GuiToggle((Rectangle){ WIDTH - 100 - 50 - 100 - 50 , 50 + 24 + 6, 100, 24 }, "FILTER", synth->filter);
+ synth->poly = GuiToggle((Rectangle){ WIDTH - 100 - 50 - 100 - 50 , 50 + 24 + 6 + 24 + 6, 100, 24 }, "POLY", synth->poly);
GuiSpinner((Rectangle){ WIDTH - 100 - 50 , 50, 100, 24 }, "oct: ", &(synth->octave), 0, 7, 0);
snprintf(buf, sizeof buf, "generator %d --> ", synth->geni);