Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #define MAX_ORDERS 16
- #define MAX_PATTERNS 64
- #define FILENAME "random.mod"
- #define SONGNAME "Random lulz" // TODO: Make random name generator? Meh...
- void putstr_padzero(char *str, int length, FILE *fmodule);
- typedef struct
- {
- unsigned long length;
- char *data;
- } MODULE_SAMPLE;
- MODULE_SAMPLE samples[31];
- int main(int argc, char *argv[])
- {
- char MOD_name[20], MOD_tag[5];
- unsigned long MOD_size;
- char order_count, pattern_count;
- FILE *fmodule;
- int sample_count = 0;
- int i, j;
- memset(samples, 0, sizeof (MODULE_SAMPLE));
- printf(" +-----------------------------------+ \n");
- printf(" | 8bitbubsy's random .MOD generator | \n");
- printf(" +-----------------------------------+ \n");
- printf("\n");
- // Load samples
- printf("* Loading samples (samples/NUMBER.raw)... ");
- for (i = 0; i < 31; i++)
- {
- FILE *sample;
- char file_to_open[128];
- sprintf(file_to_open, "samples/%d.raw", i + 1);
- sample = fopen(file_to_open, "rb");
- if (sample == NULL) continue;
- // Get filesize
- fseek(sample, 0, SEEK_END);
- samples[i].length = ftell(sample);
- fseek(sample, 0, SEEK_SET);
- if (samples[i].length > 0xFFFF)
- {
- printf("- %s is over 65535 bytes... Skipping sample\n", i);
- samples[i].length = 0;
- samples[i].data = NULL;
- fclose(sample);
- continue;
- }
- samples[i].data = malloc(samples[i].length);
- if (fread(samples[i].data, 1, samples[i].length, sample) != samples[i].length)
- {
- printf("- Couldn't read all the bytes from %s... Skipping sample\n", file_to_open);
- samples[i].length = 0;
- fclose(sample);
- free(samples[i].data);
- continue;
- }
- sample_count++;
- fclose(sample);
- }
- if (sample_count == 0)
- {
- printf("Failed!\n");
- printf("- No samples found...\n");
- return 1;
- }
- printf("OK\n");
- // Order count and pattern count
- srand((unsigned int)time(NULL));
- order_count = (rand() + 1) % MAX_ORDERS;
- pattern_count = (rand() + 1) % MAX_PATTERNS;
- // ------------------------------------
- printf("* Opening %s for writing... ", FILENAME);
- fmodule = fopen(FILENAME, "wb");
- if (fmodule == NULL)
- {
- printf("Failed!\n");
- for (i = 0; i < 31; i++)
- {
- if (samples[i].data != NULL)
- free(samples[i].data);
- }
- return 1;
- }
- printf("OK\n");
- // Song title
- strcpy(MOD_name, SONGNAME); // TODO: Make random name generator? Meh
- putstr_padzero(MOD_name, 20, fmodule);
- // Samples
- printf("* Writing sample information... ");
- srand((unsigned int)time(NULL));
- for (i = 0; i < 31; i++)
- {
- unsigned short loop_start, loop_end;
- loop_start = (unsigned short)samples[i].length ? (rand() % ((unsigned short)samples[i].length - 4)) : 0;
- loop_end = loop_start ? ((rand() + loop_start) % ((unsigned short)samples[i].length - 2)) : 0;
- // Sample name
- if (samples[i].length != 0)
- {
- char sample_name[22];
- sprintf(sample_name, "Sample %02d", i + 1);
- putstr_padzero(sample_name, 22, fmodule);
- }
- else
- {
- putstr_padzero("", 22, fmodule);
- }
- // Sample length
- fputc(samples[i].length >> 9, fmodule);
- fputc(samples[i].length >> 1, fmodule);
- fputc(0, fmodule); // Finetune
- fputc(64, fmodule); // Volume
- // Loop start
- fputc(loop_start >> 9, fmodule);
- fputc(loop_start >> 1, fmodule);
- // Loop end
- fputc(loop_end >> 9, fmodule);
- fputc(loop_end >> 1, fmodule);
- }
- printf("OK\n");
- fputc(order_count, fmodule); // Number of orders
- fputc(0, fmodule); // Restart position
- // Write order list
- srand((unsigned int)time(NULL));
- for (i = 0; i < 128; i++)
- fputc(rand() % pattern_count, fmodule);
- // File format tag
- if (pattern_count <= 64) // Yes... Stupid ProTracker versions
- {
- strcpy(MOD_tag, "M.K.");
- fwrite(MOD_tag, 1, 4, fmodule);
- }
- else
- {
- strcpy(MOD_tag, "M!K!");
- fwrite(MOD_tag, 1, 4, fmodule);
- }
- // Write pattern data
- printf("* Writing pattern data... ");
- {
- int pattern;
- srand((unsigned int)time(NULL));
- for (pattern = 0; pattern < pattern_count; pattern++)
- {
- int row;
- for (row = 0; row < 64; row++)
- {
- int channel;
- for (channel = 0; channel < 4; channel++)
- {
- unsigned short period = 0;
- char sample_number = 0, command = 0, param = 0;
- int note_rand, cmd_rand;
- note_rand = rand() % 6;
- if (note_rand == 1)
- {
- period = rand() % 420;
- if (period < 150) period = 0;
- }
- if (period != 0)
- {
- sample_number = rand() % 31;
- if ((samples[sample_number].length == 0) || (sample_number == 0))
- {
- sample_number = 0;
- period = 0;
- }
- }
- // Trigger volume command
- cmd_rand = rand() % 8; // less often please
- if (cmd_rand == 1)
- {
- command = 0x0C;
- param = rand() % 0x40;
- }
- // Trigger vibrato
- cmd_rand = rand() % 16; // less often
- if (cmd_rand == 1)
- {
- command = 0x04;
- param = (rand() + 0x80) % 0x28;
- }
- // Trigger portamento
- cmd_rand = rand() % 4; // less often
- if ((cmd_rand == 1) && sample_number)
- {
- command = 0x03;
- param = rand() % 0x80;
- }
- fputc(((period >> 8) & 0x0F) | (sample_number & 0x10), fmodule);
- fputc(period & 0xFF, fmodule);
- fputc(((sample_number & 0x0F) << 4) | (command & 0x0F), fmodule);
- fputc(param, fmodule);
- }
- }
- }
- }
- printf("OK\n");
- // Write sample data
- printf("* Writing sample data... ");
- for (i = 0; i < 31; i++)
- {
- char *ptr = samples[i].data;
- if (ptr == NULL) continue;
- for (j = 0; j < (signed)samples[i].length; j++)
- fputc(*ptr++, fmodule);
- }
- printf("OK\n");
- for (i = 0; i < 31; i++)
- {
- if (samples[i].data != NULL)
- free(samples[i].data);
- }
- MOD_size = ftell(fmodule);
- fclose(fmodule);
- printf("\n");
- printf(" +------------------+ \n");
- printf(" | .MOD information | \n");
- printf(" +------------------+ \n");
- printf("\n");
- printf(" Name: %s\n", MOD_name);
- printf(" Tag: %s\n", MOD_tag);
- printf(" Orders: %03d/128\n", order_count);
- printf(" Patterns: %03d/127\n", pattern_count);
- printf(" Samples: %02d/31\n", sample_count);
- printf("\n");
- printf(" Filename: %s\n", FILENAME);
- printf(" Filesize: %dkB\n", MOD_size / 1000);
- printf("\n");
- return 0;
- }
- void putstr_padzero(char *str, int length, FILE *fmodule)
- {
- int str_len = strlen(str);
- int i;
- for (i = 0; i < length; i++)
- {
- if (i < str_len)
- fputc(str[i], fmodule);
- else
- fputc('\0', fmodule);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement