-#include <stdio.h>
-#include <math.h>
-#include <windows.h> //temporary
-
+#include "colon3sampler.h"
/*
* colon3sampler: a mediocre sampler for the UTAU singing synthesis program
*
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;
};
--- /dev/null
+#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;
+
+};