#define FMTLENGTH 16
static int writeBlankHeader(char *filename);
-static int addWaveToEnd(int16_t wave[], int wave_length, HANDLE fp);
-static int editHeaderBecauseFileIsLongerUwU(HANDLE fp);
+static int addWaveToEnd(int16_t wave[], int wave_length, FILE *fp);
+static int editHeaderBecauseFileIsLongerUwU(FILE *fp);
int appendWave(int16_t wave[], int wave_length, char *filename)
{
- if (GetFileAttributes(filename) == 0xFFFFFFFF) {
+ FILE *fp;
+
+ if (!(fp = fopen(filename, "r"))) {
//write header
writeBlankHeader(filename);
+ } else {
+ fclose(fp);
};
- HANDLE fp;
-
- fp = CreateFile(
- filename,
- GENERIC_WRITE,
- 0,
- NULL,
- OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL,
- NULL
- );
- addWaveToEnd(wave, wave_length, fp);
- editHeaderBecauseFileIsLongerUwU(fp);
-
- //idk GAY
+ fopen(filename, "ab");
+ addWaveToEnd(wave, wave_length, fp);
+ editHeaderBecauseFileIsLongerUwU(fp);
+ fclose(fp);
+
+ //idk GAY
- //etcetc
+ //etcetc
return 1;
int writeBlankHeader(char *filename)
{
- HANDLE fp;
+ FILE *fp;
long unsigned int bytes_written;
- fp = CreateFile(
- filename,
- GENERIC_WRITE,
- 0,
- NULL,
- CREATE_ALWAYS,
- FILE_ATTRIBUTE_NORMAL,
- NULL
- );
+ fp = fopen(filename, "wb");
const char riff_id[4] = {'R', 'I', 'F', 'F' };
- WriteFile(fp, riff_id, 4, &bytes_written, NULL);
+ fwrite(riff_id, 4, 1, fp);
const uint32_t riff_length = HEADERLENGTH - 8;
- WriteFile(fp, &riff_length, 4, &bytes_written, NULL);
+ fwrite(&riff_length, 4, 1, fp);
const char wave_id[4] = {'W', 'A', 'V', 'E'};
- WriteFile(fp, wave_id, 4, &bytes_written, NULL);
+ fwrite(wave_id, 4, 1, fp);
const char fmt_id[4] = {'f', 'm', 't', ' '};
- WriteFile(fp, fmt_id, 4, &bytes_written, NULL);
+ fwrite(fmt_id, 4, 1, fp);
const uint32_t fmt_length = FMTLENGTH;
- WriteFile(fp, &fmt_length, 4, &bytes_written, NULL);
+ fwrite(&fmt_length, 4, 1, fp);
const uint16_t format_tag = 1; //microsoft PCM i think??
- WriteFile(fp, &format_tag, 2, &bytes_written, NULL);
+ fwrite(&format_tag, 2, 1, fp);
const uint16_t channel_count = 1; //mono
- WriteFile(fp, &channel_count, 2, &bytes_written, NULL);
+ fwrite(&channel_count, 2, 1, fp);
const uint32_t sampling_rate = 44100;
- WriteFile(fp, &sampling_rate, 4, &bytes_written, NULL);
+ fwrite(&sampling_rate, 4, 1, fp);
const uint32_t bytes_per_second = 88200;
- WriteFile(fp, &bytes_per_second, 4, &bytes_written, NULL);
+ fwrite(&bytes_per_second, 4, 1, fp);
const uint16_t block_align = 2; //no idea what this means
- WriteFile(fp, &block_align, 2, &bytes_written, NULL);
+ fwrite(&block_align, 2, 1, fp);
const uint16_t bits_per_sample = 16;
- WriteFile(fp, &bits_per_sample, 2, &bytes_written, NULL);
+ fwrite(&bits_per_sample, 2, 1, fp);
const char data_id[4] = {'d', 'a', 't', 'a'};
- WriteFile(fp, data_id, 4, &bytes_written, NULL);
+ fwrite(data_id, 4, 1, fp);
const uint32_t data_size = 0;
- WriteFile(fp, &data_size, 4, &bytes_written, NULL);
+ fwrite(&data_size, 4, 1, fp);
- CloseHandle(fp);
+ fclose(fp);
return 1;
};
-static int addWaveToEnd(int16_t w[], int w_l, HANDLE fp)
+static int addWaveToEnd(int16_t w[], int w_l, FILE *fp)
{
- SetFilePointer(fp, 0, NULL, FILE_END);
+ fseek(fp, 0, SEEK_END);
unsigned long int gay = 0;
- WriteFile(fp, w, w_l, &gay, NULL);
+ fwrite(w, w_l, 1, fp);
return 1;
};
-static int editHeaderBecauseFileIsLongerUwU(HANDLE fp)
+static int editHeaderBecauseFileIsLongerUwU(FILE *fp)
{
unsigned long int gay = 0;
- uint32_t filesize = GetFileSize(fp, NULL);
+ fseek(fp, 0, SEEK_END);
+ uint32_t filesize = ftell(fp);
uint32_t riff_data_size = filesize - 8;
uint32_t wave_data_size = filesize - 44;
- SetFilePointer(fp, 4, NULL, FILE_BEGIN); //file size - 8
- WriteFile(fp, &riff_data_size, 4, &gay, NULL);
+ fseek(fp, 4, SEEK_SET); //file size - 8
+ fwrite(&riff_data_size, 4, 0, fp);
- SetFilePointer(fp, 40, NULL, FILE_BEGIN); //wave size
- WriteFile(fp, &riff_data_size, 4, &gay, NULL);
+ fseek(fp, 40, SEEK_SET); //wave size
+ fwrite(&riff_data_size, 4, 0, fp);
return 1;
};
#define LOGFILE L"C:\\projects\\colon3wavtool\\log.txt"
static int log_args(int argc, char *argv[]);
+static int fix_filename(char *filename); //if on windows?? i think...
int main(int argc, char *argv[])
{
char *error_msg = "unknown error in main()";
//test ver: just like. log the arguments lol!!
log_args(argc, argv);
-
+ //fix_filename(argv[1]);
+ //fix_filename(argv[2]);
//ok.
if (argc >= 3) {
input_wave_length = readWave(argv[2], NULL);
};
if (input_wave_length > 0) {
- input_wave = GlobalAlloc(0, input_wave_length);
+ input_wave = malloc(input_wave_length);
} else {
/* expected for rests
error_msg = "input wave error || input wave length is 0";
goto error;
*/
//MessageBox(NULL, argv[4], "rest length", MB_OK);
- input_wave_length = ceil(44100 * length_arg_to_float(argv[4]) * 2);
+ input_wave_length = ceil(44100 * length_arg_to_float(argv[4])) * 2; //first the amount of samples, then how many bytes that is :3
printf("%d", input_wave_length);
- input_wave = GlobalAlloc(GPTR,input_wave_length);//initializes to 0
+ input_wave = malloc(input_wave_length);
+
+ for (int index = 0; index < input_wave_length/2; index++) {
+ input_wave[index] = 0;
+ };
+
+
//not working?! //got the wrong argument...
}
int errored = 0;
if (input_wave != NULL) {
errored = readWave(argv[2], input_wave);
} else {
- error_msg = "[main()] GlobalAlloc() failed";
+ error_msg = "[main()] malloc()/calloc() failed";
goto error;
};
error_msg = "[main()] error from readWave()";
goto error;
};
- GlobalFree(input_wave);
+ free(input_wave);
+
+ /*
+ while (getc(stdin) != '\n') {
+ //wait for input to continue
+ };
+ */
return 0;
error:
- MessageBoxA(NULL, error_msg, "colon3wavtool errored", MB_OK | MB_ICONWARNING);
- GlobalFree(input_wave);
+ report_error(error_msg, "main()", *argv);
+ free(input_wave);
};
return 1;
};
+
+int fix_filename(char *filename) {
+ for (int i = 0; filename[i] != '\0'; i++) {
+ if (filename[i] == '\\') {
+ filename[i] = '/';
+ };
+ };
+};
--- /dev/null
+this is licensed under the GPL v3.0 or higher .
+
+yeah ik the license yap isnt in every file. i swear it is licensed like that im
+just really really lazy and also kinda tired rn :(
+
+i think
+
+(if i change my mind and switch it, obviously earlier versions will still be
+under the GPLv3+ and the only thing id change it to would be the other one that
+makes putting it on a network count as distributing. dw this WILL stay free :D)
+
+uhhh it's a wavtool. for UTAU.
+not tested with openUtau yet because im too lazy tbh
+
+uhh tyty for :
+<https://github.com/m13253/wavtool-yawu/> (LGPL v3 only)
+I based a lot of the note length code on this. like a lot. why tf is it
+formatted like that :/// i dont think i took enough code from here
+that i'd have to use the gpl v3 instead of v3+ but idk im not a lawyer if the
+author has an issue ill change it ig
+(the only code i used was like. "ticks/tempo/8" which isnt much code, i just
+couldnt find any real documentation on how UTAU gives length)
+(what even counts as using code?? probably not that idk)
+
+<https://github.com/delta-kimigatame/PyWavTool> (MIT licesne)
+i didnt use any code from here AT ALL. but it has super duper useful notes on
+the arguments. seriously why are the arguments not written down anywhere??
+
+the like wav file specification idk
+
+***
+
+uhhh yeah
+
+im kinda bad at licenses but i think i didnt fuck up too badly :D
+
+this is mainly here as a placeholder so i dont forget what sources i used
+
+***
+
+notes on the code:
+written in C. rn it uses the win32 apis but im going to see if writing it in
+the normal ones will work lol
+
+
+
+
{
//haha
//ok so expect 16-bit samples, and also the length required should be in BYTES
- HANDLE fp;
+ FILE *fp;
char *error_msg = "unknown error in reading wave file";
+
printf("input file: %s \n", filename);
- fp = CreateFileA(
+ fp = fopen(
filename,
- GENERIC_READ,
- 0,
- NULL,
- OPEN_EXISTING, //if it doesnt exist it's bad :>
- FILE_ATTRIBUTE_NORMAL,
- NULL
- );
+ "rb"
+ );
- if (fp == INVALID_HANDLE_VALUE) {
+ 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";
};
//get file length and load it ig...
- DWORD filesize = 0;
- filesize = GetFileSize(fp, NULL);
+ int filesize = 0;
+ fseek(fp, 0, SEEK_END);
+ filesize = ftell(fp);
long unsigned int bytes_read = 0;
//ok so first the header...
char chunk_id[5] = " ";
- ReadFile(fp, chunk_id, 4, &bytes_read, NULL);
+ fread(chunk_id, 4, 1, fp);
if (strcmp("RIFF", chunk_id)) {
error_msg = "input wave file not good :<";
- goto error;
+ //goto error;
};
uint32_t riff_length = 0; //length of the rest of the file
- ReadFile(fp, &riff_length, 4, &bytes_read, NULL);
- if (bytes_read != 4) {
- error_msg = "file ended before the 8th byte?? wha";
- goto error;
- };
+ fread(&riff_length, 4, 1, fp);
//now the WAVE chunk id
- ReadFile(fp, chunk_id, 4, &bytes_read, NULL);
+ fread(chunk_id, 4, 1, fp);
if (strcmp("WAVE", chunk_id)) {
error_msg = "not a WAVE file :<";
goto error;
};
//now the 'fmt ' chunk id
- ReadFile(fp, chunk_id, 4, &bytes_read, NULL);
+ 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;
- ReadFile(fp, &fmt_length, 4, &bytes_read, NULL);
- if (bytes_read != 4) {
- error_msg = "couldn't read length of 'fmt ' chunk in input wave file";
- goto error;
- };
+ fread(&fmt_length, 4, 1, fp);
//now the format tag
uint16_t format_tag = 0;
- ReadFile(fp, &format_tag, 2, &bytes_read, NULL);
+ fread(&format_tag, 2, 1, fp);
if (format_tag != 1) {
error_msg = "not the format i like...";
goto error;
};
uint16_t channel_count = 0;
- ReadFile(fp, &channel_count, 2, &bytes_read, NULL);
+ fread(&channel_count, 2, 1, fp);
if (channel_count != 1) {
error_msg = "input wave file not mono";
goto error;
};
uint32_t sampling_rate = 0;
- ReadFile(fp, &sampling_rate, 4, &bytes_read, NULL);
+ 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};
- ReadFile(fp, boring, 6, &bytes_read, NULL);
- if (bytes_read != 6) {
- error_msg = "input file ended unexpectedly in 'fmt ' chunk";
- goto error;
- };
+ fread(boring, 6, 1, fp);
uint16_t bitsPerSample = 0;
- ReadFile(fp, &bitsPerSample, 2, &bytes_read, NULL);
+ fread(&bitsPerSample, 2, 1, fp);
if (bitsPerSample != 16) {
error_msg = "input file has non-16-bit samples";
goto error;
};
- ReadFile(fp, &chunk_id, 4, &bytes_read, NULL);
+ 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;
- ReadFile(fp, &data_length, 4, &bytes_read, NULL);
- if (bytes_read != 4) {
- error_msg = "input file ended unexpectedly in 'data' chunk";
- goto error;
- };
+ fread(&data_length, 4, 1, fp);
//NOW TO READ THE WAVE wwwwwwwwww
int sample_count = data_length/2;
if (buffer != NULL) {
- ReadFile(fp, buffer, data_length, &bytes_read, NULL);
+ fread(buffer, 2, sample_count, fp);
+ };
+ if (ferror(fp)) {
+ perror("Error!");
+ report_error("Error", "readWave()", "colon3wavtool");
};
- CloseHandle(fp);
+ fclose(fp);
return data_length;
error:
- MessageBoxA(NULL, error_msg, "colon3wavtool error :(((", MB_OK | MB_ICONWARNING);
- CloseHandle(fp);
+ report_error(error_msg, "readWave()", "colon3wavtool");
+ fclose(fp);
return -1;
};
};
return r;
};
+
+int report_error(char *msg, char *portion, char *program)
+{
+ fprintf(stderr, "%s: [%s] %s\n", program, portion, msg);
+ return 1;
+};
*
*/
-#include <windows.h>
#include <stdio.h>
+#include <windows.h>
#include <stdint.h>
#define SAMPLING_RATE 44100
//useful shit
float arg_to_float(char *arg);
float length_arg_to_float(char *arg); //returns float of SECONDS. >:(
+int report_error(char *error_msg, char *portion, char *program); //in a function so that it's possible to change it more easily uwu
//ok so first read the whole file into a thingy
int readWave(char *filename, int16_t buffer[]);