#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <unistd.h>
// Start Generation Here
void glitch_wav(const char *input_filename, const char *output_filename) {
FILE *fin = fopen(input_filename, "rb");
if (!fin) {
perror("Error opening input file");
return;
}
FILE *fout = fopen(output_filename, "wb");
if (!fout) {
perror("Error opening output file");
fclose(fin);
return;
}
uint8_t header[44];
if (fread(header, sizeof(uint8_t), 44, fin) != 44) {
perror("Error reading WAV header");
fclose(fin);
fclose(fout);
return;
}
fwrite(header, sizeof(uint8_t), 44, fout);
// Determine data size from header
uint32_t data_size = *(uint32_t *)&header[40];
// Seed random number generator
srand(time(NULL));
uint8_t *buffer = malloc(data_size);
if (!buffer) {
perror("Memory allocation failed");
fclose(fin);
fclose(fout);
return;
}
if (fread(buffer, sizeof(uint8_t), data_size, fin) != data_size) {
perror("Error reading WAV data");
free(buffer);
fclose(fin);
fclose(fout);
return;
}
// Introduce glitches by randomly modifying bytes
for (uint32_t i = 0; i < data_size; i++) {
if (rand() % 100 < 5) { // 5% chance to alter each byte
buffer[i] = rand() % 256;
}
}
fwrite(buffer, sizeof(uint8_t), data_size, fout);
free(buffer);
fclose(fin);
fclose(fout);
printf("Glitched WAV file created: %s\n", output_filename);
// Start Generation Here
pid_t pid = fork();
if (pid == 0) {
// Child process: Play the glitched WAV file
execlp("afplay", "afplay", output_filename, (char *)NULL);
perror("Failed to start afplay");
exit(1);
} else if (pid > 0) {
// Parent process: Further glitch the WAV file during playback
FILE *fout_append = fopen(output_filename, "rb+");
if (!fout_append) {
perror("Error opening WAV file for further glitching");
return;
}
uint8_t *buffer = malloc(data_size);
if (!buffer) {
perror("Memory allocation failed for further glitching");
fclose(fout_append);
return;
}
if (fread(buffer, sizeof(uint8_t), data_size, fout_append) != data_size) {
perror("Error reading WAV data for further glitching");
free(buffer);
fclose(fout_append);
return;
}
// Introduce additional glitches while playback is ongoing
for (uint32_t i = 0; i < data_size; i++) {
if (rand() % 100 < 5) { // 5% chance to alter each byte
buffer[i] = rand() % 256;
}
}
// Move file pointer to the beginning of data section
fseek(fout_append, 44, SEEK_SET);
fwrite(buffer, sizeof(uint8_t), data_size, fout_append);
free(buffer);
fclose(fout_append);
// Wait for the child process to finish
wait(NULL);
} else {
perror("Fork failed");
}
}
int main() {
glitch_wav("input.wav", "output.wav");
return 0;
}
//how do i compile this?
//gcc glitchwav-madeby-o1-mini.c -o glitchwav
//./glitchwav