]> _ Git - colon3sampler.git/commitdiff
change pitch i think
authorparking_turkeys <roya1060wien@gmail.com>
Fri, 16 Oct 2026 16:14:55 +0000 (12:14 -0400)
committerparking_turkeys <roya1060wien@gmail.com>
Fri, 16 Oct 2026 16:14:55 +0000 (12:14 -0400)
change_pitch.c [new file with mode: 0644]
colon3sampler.h
main.c

diff --git a/change_pitch.c b/change_pitch.c
new file mode 100644 (file)
index 0000000..5098ddd
--- /dev/null
@@ -0,0 +1,58 @@
+#include "colon3sampler.h"
+
+static int change_speed_pitch(int16_t wave[], int w_B, float pitch_mult,
+               int16_t out_wave[], int out_w_B); /* Put NULL to get length */
+static int16_t get_fractional_index(int16_t wave[], float index);
+
+int change_pitch(int16_t w[], int w_B, float p_m, int16_t ow[])
+{
+       /* Oh i dont know how to do this */
+       /* It'll probably be bad to change it too much */
+get_length:
+       int length = floor((w_B/2)/p_m) * 2;
+       printf("original length: %d; mult: %f; new length: %d\n", w_B, p_m, length);
+       if (ow == NULL) goto return_length;
+change_pitch:
+       change_speed_pitch(w, w_B, p_m, ow, length);
+
+return_success:        
+       return 1;
+return_error:
+       fprintf(stderr, "error :(\n");
+       return 0;
+return_length:
+       return length;
+};
+
+static int change_speed_pitch(int16_t w[], int w_B, float p_m, 
+               int16_t ow[], int ow_B)
+{
+       int ow_i = 0;
+       for (double i = 0.0; (i+p_m) < (w_B/2); i+=p_m) {
+               if (ow_i >= (ow_B/2)) {
+                       printf( "ow_i: %d; i: %f; ow_B: %d\n"   
+                               "Too short out wave for change_speed_pitch\n",
+                               ow_i, i, ow_B);
+                       return 0;
+               };
+               /* if it's round it's easy... */
+               if (i == (float)floor(i)) {
+                       ow[ow_i] = w[(int)floor(i)];
+               } else {
+                       ow[ow_i] = get_fractional_index(w, i);
+               };
+               ow_i++;
+       };
+       return 1;
+};
+
+static int16_t get_fractional_index(int16_t w[], float i)
+{
+       /* idk */
+       int lower = w[(int)floor(i)];
+       int higher = w[(int)ceil(i)];
+       float fraction = i - floor(i);
+       int16_t result = higher*fraction + lower*(1-fraction);
+       return result;
+       
+};
index 6dea6d7..2dda5b7 100644 (file)
@@ -2,7 +2,7 @@
 #include <stdio.h>
 #include <stddef.h>
 #include <stdint.h>
-#include <math.h>
+#include <math.h> 
 #include <stdlib.h>
 
 #define ß(arg) arg_to_float(arg)
@@ -14,6 +14,9 @@ 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);
+int change_pitch(int16_t wave[], int w_B, float pitch_multiplier,
+               int16_t out_wave[]); /* Give NULL for length */
+
 
 /* From other things */
 int readWave(char *filename, int16_t buffer[], int STP_ms); /* STP=0 */
diff --git a/main.c b/main.c
index 092e1a7..d6646cc 100644 (file)
--- a/main.c
+++ b/main.c
@@ -150,9 +150,16 @@ int main(int argc, char *argv[])
        change_pitch:
                float pitch_mult = pitch_Hz/midi_to_hz(argv[3]);
                printf("%f", pitch_mult);
-               //change_pitch(phoneme, ph_B, pitch_mult);
+               int resampled_len = change_pitch(phoneme, ph_B, pitch_mult, NULL);
+               int16_t *resampled = malloc(resampled_len);
+               change_pitch(phoneme, ph_B, pitch_mult, resampled);
+       change_length:
+               /* tba */
+       
        cleanup:
                free(wave);
+               free(phoneme);
+               free(resampled);
        return 0;
        error:
                //putc('e');putc('r');putc('r');putc('o');putc('r');