From: parking_turkeys Date: Wed, 14 Oct 2026 12:47:38 +0000 (-0400) Subject: do like midi stuff yk X-Git-Url: http://git.p-t.hackclub.app/?a=commitdiff_plain;h=d4968a58e4b8797ac0692c478949f55982f30b1d;p=colon3sampler.git do like midi stuff yk --- diff --git a/Makefile b/Makefile index c665250..c4b11aa 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,6 @@ -$(CC) = gcc linux: $(CC) *.h *.c -Werror -Wall -Wno-unused-label -lm -o colon3sampler -std=c99 linux-test: linux - ./colon3sampler test/a.wav + ./colon3sampler test/a.wav test/a_out.wav F4 100 / 24.0 550 56 73 100 0 !120 diff --git a/colon3sampler.h b/colon3sampler.h index d6a1c51..6dea6d7 100644 --- a/colon3sampler.h +++ b/colon3sampler.h @@ -13,6 +13,7 @@ int get_important_wave(int16_t orig_wave[], int w_B, int16_t new_wave[], float offset, float cutoff, float consonant); float get_pitch(int16_t wave[], int w_B); +float midi_to_hz(char *note); /* From other things */ int readWave(char *filename, int16_t buffer[], int STP_ms); /* STP=0 */ diff --git a/get_pitch.c b/get_pitch.c index 1458392..8ae18a4 100644 --- a/get_pitch.c +++ b/get_pitch.c @@ -1,12 +1,14 @@ #include "colon3sampler.h" +static int is_through_zero(int16_t last, int16_t current); float get_pitch(int16_t w[], int w_B) { /* count the things. */ int P = 0; int P_i = 0; int *Ps = malloc(w_B); + int16_t last = 0; for (int i = 0; i < (w_B/2); i++) { - if (w[i] == 0) { + if (is_through_zero(last, w[i])) { if (P > 1) { Ps[P_i] = P; P_i++; @@ -15,6 +17,7 @@ float get_pitch(int16_t w[], int w_B) }; //printf("%d\n", w[i]); P++; + last = w[i]; }; /* get average period */ int av = 0; @@ -40,4 +43,10 @@ float get_pitch(int16_t w[], int w_B) }; - +static int is_through_zero(int16_t l, int16_t c) +{ + if (c == 0) return 1; + if ((c > 0) && (l < 0)) return 1; + if ((c < 0) && (l > 0)) return 1; + return 0; +}; diff --git a/main.c b/main.c index 7daebf8..092e1a7 100644 --- a/main.c +++ b/main.c @@ -147,6 +147,10 @@ int main(int argc, char *argv[]) get_pitch: float pitch_Hz = get_pitch(phoneme, ph_B); printf("The pitch is %f Hz.\n", pitch_Hz); + change_pitch: + float pitch_mult = pitch_Hz/midi_to_hz(argv[3]); + printf("%f", pitch_mult); + //change_pitch(phoneme, ph_B, pitch_mult); cleanup: free(wave); return 0; diff --git a/midi.c b/midi.c new file mode 100644 index 0000000..eef89c4 --- /dev/null +++ b/midi.c @@ -0,0 +1,83 @@ +#include "colon3sampler.h" + +const char *midi_key[128] = { + "C-1", "C#-1", "D-1", "D#-1", "E-1", "F-1", "F#-1", "G-1", "G#-1", "A-1", "A#-1", "B-1" + "C0", "C#0", "D0", "D#0", "E0", "F0", "F#0", "G0", "G#0", "A0", "A#0", "B0" + "C1", "C#1", "D1", "D#1", "E1", "F1", "F#1", "G1", "G#1", "A1", "A#1", "B1" + "C2", "C#2", "D2", "D#2", "E2", "F2", "F#2", "G2", "G#2", "A2", "A#2", "B2" + "C3", "C#3", "D3", "D#3", "E3", "F3", "F#3", "G3", "G#3", "A3", "A#3", "B3" + "C4", "C#4", "D4", "D#4", "E4", "F4", "F#4", "G4", "G#4", "A4", "A#4", "B4" + "C5", "C#5", "D5", "D#5", "E5", "F5", "F#5", "G5", "G#5", "A5", "A#5", "B5" + "C6", "C#6", "D6", "D#6", "E6", "F6", "F#6", "G6", "G#6", "A6", "A#6", "B6" + "C7", "C#7", "D7", "D#7", "E7", "F7", "F#7", "G7", "G#7", "A7", "A#7", "B7" + "C8", "C#8", "D8", "D#8", "E8", "F8", "F#8", "G8", "G#8", "A8", "A#8", "B8" + "C9", "C#9", "D9", "D#9", "E9", "F9", "F#9", "G9" +}; + + +static int midi_to_midi_int(char *note); +static float midi_int_to_hz(int midi_int); + +float midi_to_hz(char *note) +{ + int midi = midi_to_midi_int(note); + float hz = midi_int_to_hz(midi); + return hz; +}; + +static int midi_to_midi_int(char *n) +{ + int m = 0; int s = 0; + switch (n[0]) { + case 'B': m++; + case 'A': m++; + case 'G': m++; + case 'F': m++; + case 'E': m++; + case 'D': m++; + }; + if (n[1] == '#') { + m++; s = 1; + }; + + int oct = arg_to_float(n+1+s); + m += 12*(oct+1); + return m; + +}; + +static float midi_int_to_hz(int midi) +{ + /* From github.com/UtaUtaUtau/straycat */ + /* Under the MIT license (ADD MIT LICENSE :<) */ + /* ok here */ + /* + * Copyright 2022 UtaUtaUtau + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + /* i hope it's ok to format it like that... */ + /* it's a bit different bc straycat (original) is in python :) */ + + /* "A4 == 440Hz" (quote) */ + return 440.0 * pow(2,(midi - 69.0) / 12.0); +}; +