Code:
#include <stdio.h>
#include <string.h>
double sample_rate_base; // temporary for getting the fractional parts for rounding - 7 significant figures isn't quite enough given that sample rates go to 400,000
unsigned int sample_rate; // integerical rounded value of the above variable
signed short loop_position; // loop position for the creation of files
signed short loop_start; // starting point of the loop
signed short loop_end; // the point where the loop will end
unsigned int bytes_per_second; // equal to sample_rate*multiplier
char multiplier; // the multiplier used for the bytes_per_second variable
char file_head_start[24]; // for copying identical file header data
char file_head_end[7]; // for copying the last parts of the file header data
unsigned int file_length; // the length of the file after the header data in bytes
char file_contents[]; // the contents of the file for copying, since the samples themselves don't change at all
char file_name[]; // the file name for the output file
FILE *file_pointer;
void find_next_sample_rate()
{
// Based on the new 48-step speed system
if (loop_position < -72)
{
sample_rate_base += (208.0+(1.0/3.0)); // 208 1/3
}
if (loop_position == -72)
{
sample_rate_base = 17500.0; // a special case to prevent rounding flaws from the float - I may need the double
}
if ((loop_position > -72) && (loop_position <= -48))
{
sample_rate_base += 312.5;
}
if ((loop_position > -48) && (loop_position <= -24))
{
sample_rate_base += (416.0+(2.0/3.0)); // 416 2/3
}
if ((loop_position > -24) && (loop_position <= 0))
{
sample_rate_base += 625.0;
}
if ((loop_position > 0) && (loop_position <= 24))
{
sample_rate_base += (833.0+(1.0/3.0)); // 833 1/3
}
if ((loop_position > 24) && (loop_position <= 48))
{
sample_rate_base += 1250.0;
}
if ((loop_position > 48) && (loop_position <= 72))
{
sample_rate_base += (1666.0+(2.0/3.0)); // 1666 2/3
}
if ((loop_position > 72) && (loop_position <= 96))
{
sample_rate_base += 2500.0;
}
if ((loop_position > 96) && (loop_position <= 120))
{
sample_rate_base += (3333.0+(1.0/3.0)); // 3333 1/3
}
if (loop_position > 120)
{
sample_rate_base += 5000.0;
}
loop_position += 1; // advance the loop position
sample_rate = (int)(sample_rate_base+0.5); // use starting value first and round it off
bytes_per_second = sample_rate*multiplier;
}
void get_new_file_name()
{
// chars 48 through 57 are the numbers
char digit_1;
char digit_10;
char digit_100;
char digit_1000;
char digit_10000;
char digit_100000;
int remaining_part;
digit_100000 = sample_rate/100000; // since integers are used, and junk after the decimal is ignored, only a single digit is present - breaks up numbers from 12500 to 400000 into digits
remaining_part = sample_rate-(100000*digit_100000); // gets rid of the 100,000's digit leaving 5 digits left
digit_100000 += 48; // shifts the digit value into the cooresponding character on the character map since 48 through 57 (decimal) are the digits
digit_10000 = remaining_part/10000; // continue splitting the number into the separate digits
remaining_part -= (10000*digit_10000);
digit_10000 += 48;
digit_1000 = remaining_part/1000;
remaining_part -= (1000*digit_1000);
digit_1000 += 48;
digit_100 = remaining_part/100;
remaining_part -= (100*digit_100);
digit_100 += 48;
digit_10 = remaining_part/10;
remaining_part -= (10*digit_10);
digit_10 += 48;
digit_1 = remaining_part;
digit_1 += 48;
digit_100000 = (const char)digit_100000; // random test
strcpy(file_name, "C:\\My Documents\\Songs for MP3\\Desert Zone\\");
if (loop_end >= 48) // if the highest possible is 100,000 or greater, add a sixth digit
{
strcat(file_name, digit_100000); // a special case - write only if base sample rate is 6 or more digits but write the leading 0
}
strcat(file_name, digit_10000); // add the main common digits to the file name that are always present
strcat(file_name, digit_1000);
strcat(file_name, digit_100);
strcat(file_name, digit_10);
strcat(file_name, digit_1);
strcat(file_name, " Desert Zone.wav\0"); // add the space at the end with the null character terminating the string
}
void create_output_files()
{
/*
notes about loop ranges (based of of the spreadsheet document):
-96, 12500 Hz
-84, 15000 Hz
-72, 17500 Hz
-64, 20000 Hz (extended minimum 50K)
-56, 22500 Hz (rarely used)
-48, 25000 Hz (standard minimum 50K)
-36, 30000 Hz (100K)
-24, 35000 Hz (100K)
-16, 40000 Hz (extended minimum 100K)
-8, 45000 Hz (100K)
0, 50000 Hz (standard minimum 100K)
15, 62500 Hz (standard maximum 50K)
24, 70000 Hz (extended maximum 50K)
32, 80000 Hz (50K)
40, 90000 Hz (50K)
48, 100000 Hz (50K)
63, 125000 Hz (standard maximum 100K)
72, 140000 Hz (extended maximum 100K)
80, 160000 Hz
88, 180000 Hz
96, 200000 Hz
108, 240000 Hz
120, 280000 Hz
128, 320000 Hz
136, 360000 Hz
144, 400000 Hz
*/
// first, set the range using the chart commented out above
char temp_var;
loop_start = -48; // starting point
loop_end = 15; // ending point
loop_position = loop_start; // start at the loop starting point
sample_rate = 25000; // copy sample rate from chart above with loop_position as the value to take from
bytes_per_second = sample_rate*multiplier;
while(loop_position <= loop_end) // should create 64 files
{
printf("Current progress: %d Hz, %d of %d processed\n", sample_rate, loop_position-loop_start, loop_end-loop_start);
scanf("%d", &temp_var); // to stop input to see results for testing
get_new_file_name(); // get the strings stitched together for the output file name
/*
file_pointer = fopen(file_name, "wb"); // open the file for writing in binary mode (WAV files are binary)
fwrite(file_head_start, 1, 24, file_pointer); // write the details in the same order as it was read in
fwrite(sample_rate, 4, 1, file_pointer);
fwrite(bytes_per_second, 4, 1, file_pointer);
fwrite(multiplier, 1, 1, file_pointer);
fwrite(file_head_end, 1, 7, file_pointer);
fwrite(file_length, 4, 1, file_pointer);
fwrite(file_contents, 1, file_length, file_pointer); // copies all the rest of the data, the samples themselves
fclose(file_pointer); // close the file to reuse the pointer (or is it handle as with Gamestudio?)
*/
loop_position += 1; // increment the loop for another round
find_next_sample_rate();
}
}
void read_base_file()
{
// this copies the full structure of the WAV file, since only 8 bytes are changed out of several million
/*
file_pointer = fopen("C:\\My Documents\\Songs for MP3\\Desert Zone\\Desert Zone base.wav", "rb"); // assign a pointer (or handle?) for handling the file
fread(file_head_start, 1, 24, file_pointer); // read into file_head_start in one-byte chunks and fill the array of 24 elements with the given file pointer
fread(sample_rate, 4, 1, file_pointer); // same as above but read a 4-byte chunk once instead
fread(bytes_per_second, 4, 1, file_pointer); // same as above
fread(multiplier, 1, 1, file_pointer); // read a one-byte chunk once instead of the above
fread(file_head_end, 1, 7, file_pointer); // read a one-byte chunk 7 times to fill the file header data's end
fread(file_length, 4, 1, file_pointer); // read the file length data value, the number of bytes after the head
fread(file_contents, 1, file_length, file_pointer); // read one-byte chunks for the entire file's length, which is the end of the file
fclose(file_pointer); // close the file
*/
create_output_files(); // initiate the loop for creating the output files copying the data above changing the 8 bytes as needed
}
int main()
{
read_base_file();
}
The get_new_file_name() function is where the errors are occurring due to strcat. strcpy works just fine (no errors nor warnings), but it's the part of stitching the numbers from the process above it together to form the end result.