From: parking_turkeys Date: Fri, 9 Oct 2026 17:05:47 +0000 (-0400) Subject: it's not working but i kinda have a start X-Git-Url: http://git.p-t.hackclub.app/?a=commitdiff_plain;h=e0be65d36d921b93ffe2df77f43fbd101c9a56d6;p=colon3sampler.git it's not working but i kinda have a start --- diff --git a/colon3sampler.h b/colon3sampler.h new file mode 100644 index 0000000..016ddbc --- /dev/null +++ b/colon3sampler.h @@ -0,0 +1,11 @@ +/* colon3sampler. it's peak. */ +#include +#include +#include +#include +#include + +/* FUNCTIONS */ +int get_pitch(int16_t wave[], int w_B); + +int readWave(char *filename, int16_t buffer[], int STP_ms); /* STP=0 */ diff --git a/get_pitch.c b/get_pitch.c new file mode 100644 index 0000000..f1e5817 --- /dev/null +++ b/get_pitch.c @@ -0,0 +1,37 @@ +#include "colon3sampler.h" + +int get_pitch(int16_t w[], int w_B) +{ + /* count the things. */ + int P = 0; int P_i = 0; + int *Ps = malloc(w_B); + for (int i = 0; i < (w_B*2-5); i++) { + if (w[i] == 0) { + if (P > 1) { + Ps[P_i] = P; + P_i++; + P = 0; + }; + }; + P++; + }; + /* get average period */ + int av = 0; + for (int i = 0; i < P_i; i++) { + av += Ps[i]; + }; + free(Ps); + if (P_i == 0) goto error; + if (av == 0) goto error; + av = av/P_i; + /* get frequency */ + float frq_f = 1/av; + int frq = ceil(frq_f); + /* return frequency in Hz */ + return frq; + error: + return -1; + +}; + + diff --git a/main.c b/main.c index 5e2aed1..185dd3f 100644 --- a/main.c +++ b/main.c @@ -1,7 +1,4 @@ -#include -#include -#include //temporary - +#include "colon3sampler.h" /* * colon3sampler: a mediocre sampler for the UTAU singing synthesis program * @@ -116,22 +113,46 @@ static int log_args(int argc, char *argv[]); int main(int argc, char *argv[]) { - log_args(argc, argv); - /* - if (argc == 14) { - ; - } else { - printf("Too few arguments"); - return 1; + if (argc < 2) goto error; + setup: + log_args(argc, argv); + /* + if (argc == 14) { + ; + } else { + printf("Too few arguments"); + return 1; + }; + */ + int wave_length_bytes = readWave(argv[1], NULL, 0); + if (wave_length_bytes == 0) { + return 0; /* Rest */ + } else if (wave_length_bytes == -1) { + goto error; }; - */ + get_wave: + int16_t *wave = malloc(wave_length_bytes); + if (wave == NULL) goto error; + readWave(argv[1], wave, 0); + printf("Wave acquired."); + get_pitch: + int pitch_Hz = get_pitch(wave, wave_length_bytes); + printf("The pitch is %d Hz.\n", pitch_Hz); + cleanup: + free(wave); return 0; + error: + //putc('e');putc('r');putc('r');putc('o');putc('r'); + return 1; } static int log_args(int argc, char *argv[]) { //using windows api because it pauses and stuff... + /* for (int i = 0; i < argc; i++) { MessageBoxA(NULL, argv[i], "Argument to wavtool", MB_OK); }; + */ + return 1; }; diff --git a/readwave.c b/readwave.c new file mode 100644 index 0000000..369efd8 --- /dev/null +++ b/readwave.c @@ -0,0 +1,111 @@ +#include "colon3sampler.h" + +int readWave(char *filename, int16_t buffer[], int STP_ms) //if buffer is NULL it only returns the length requried??? +{ + //haha + //ok so expect 16-bit samples, and also the length required should be in BYTES + FILE *fp; + char *error_msg = "unknown error in reading wave file"; + + int STP_samples = ceil(STP_ms * 44100 * 0.001); + + printf("input file: %s \n", filename); + + fp = fopen( + filename, + "rb" + ); + + if (fp == NULL) { + /* UTAU has rests setup as simply notes with no file so you cant fail here + * instead we will add rest by making like blank. uwu? or no just return 0 and make main do it. + error_msg = "error opening input wave file"; + goto error; + */ + return 0; + }; + + //get file length and load it ig... + fseek(fp, 40, SEEK_SET); //LMAO I FORGOT THIS SOB + /* ykw who needs to read the header. if utau is giving it to me it'd better be ok + long unsigned int bytes_read = 0; + //ok so first the header... + char chunk_id[5] = " "; + fread(chunk_id, 4, 1, fp); + if (strcmp("RIFF", chunk_id)) { + error_msg = "input wave file not good :<"; + goto error; + }; + uint32_t riff_length = 0; //length of the rest of the file + fread(&riff_length, 4, 1, fp); + //now the WAVE chunk id + fread(chunk_id, 4, 1, fp); + if (strcmp("WAVE", chunk_id)) { + error_msg = "not a WAVE file :<"; + goto error; + }; + //now the 'fmt ' chunk id + fread(chunk_id, 4, 1, fp); + if (strcmp("fmt ", chunk_id)) { + error_msg = "fmt chunk not first chunk :("; + goto error; + }; + //length + uint32_t fmt_length = 0; + fread(&fmt_length, 4, 1, fp); + //now the format tag + uint16_t format_tag = 0; + fread(&format_tag, 2, 1, fp); + if (format_tag != 1) { + error_msg = "not the format i like..."; + goto error; + }; + uint16_t channel_count = 0; + fread(&channel_count, 2, 1, fp); + if (channel_count != 1) { + error_msg = "input wave file not mono"; + goto error; + }; + uint32_t sampling_rate = 0; + fread(&sampling_rate, 4, 1, fp); + if (sampling_rate != SAMPLING_RATE) { + error_msg = "incorrect sampling rate (should be 44100)"; + goto error; + }; + uint8_t boring[6] = {0}; + fread(boring, 6, 1, fp); + uint16_t bitsPerSample = 0; + fread(&bitsPerSample, 2, 1, fp); + if (bitsPerSample != 16) { + error_msg = "input file has non-16-bit samples"; + goto error; + }; + fread(&chunk_id, 4, 1, fp); + if (strcmp(chunk_id, "data")) { + error_msg = "input file does not have data chunk right after fmt chunk"; + goto error; + }; */ + uint32_t data_length; + fread(&data_length, 4, 1, fp); + //NOW TO READ THE WAVE wwwwwwwwww + int sample_count = data_length/2; + + if (buffer != NULL) { + fseek(fp, STP_samples*2, SEEK_CUR); + fread(buffer, 2,sample_count - STP_samples, fp); + }; + if (ferror(fp)) { + goto error; + }; + + fclose(fp); + + return data_length - (STP_samples*2); + + error: + /*report_error(error_msg, "readWave()", "colon3wavtool");*/ + printf("[readwave()] %s", error_msg); + fclose(fp); + return -1; + +}; diff --git a/test/a.wav b/test/a.wav new file mode 100644 index 0000000..66f2eb4 Binary files /dev/null and b/test/a.wav differ