Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. #define MAX_ORDERS 16
  7. #define MAX_PATTERNS 64
  8.  
  9. #define FILENAME "random.mod"
  10. #define SONGNAME "Random lulz" // TODO: Make random name generator? Meh...
  11.  
  12. void putstr_padzero(char *str, int length, FILE *fmodule);
  13.  
  14. typedef struct
  15. {
  16.     unsigned long length;
  17.     char *data;
  18. } MODULE_SAMPLE;
  19.  
  20. MODULE_SAMPLE samples[31];
  21.  
  22. int main(int argc, char *argv[])
  23. {
  24.     char MOD_name[20], MOD_tag[5];
  25.     unsigned long MOD_size;
  26.     char order_count, pattern_count;
  27.     FILE *fmodule;
  28.  
  29.     int sample_count = 0;
  30.  
  31.     int i, j;
  32.  
  33.     memset(samples, 0, sizeof (MODULE_SAMPLE));
  34.  
  35.     printf(" +-----------------------------------+ \n");
  36.     printf(" | 8bitbubsy's random .MOD generator | \n");
  37.     printf(" +-----------------------------------+ \n");
  38.  
  39.     printf("\n");
  40.  
  41.     // Load samples
  42.     printf("* Loading samples (samples/NUMBER.raw)... ");
  43.     for (i = 0; i < 31; i++)
  44.     {
  45.         FILE *sample;
  46.         char file_to_open[128];
  47.         sprintf(file_to_open, "samples/%d.raw", i + 1);
  48.  
  49.         sample = fopen(file_to_open, "rb");
  50.         if (sample == NULL) continue;
  51.  
  52.         // Get filesize
  53.         fseek(sample, 0, SEEK_END);
  54.         samples[i].length = ftell(sample);
  55.         fseek(sample, 0, SEEK_SET);
  56.  
  57.         if (samples[i].length > 0xFFFF)
  58.         {
  59.             printf("- %s is over 65535 bytes... Skipping sample\n", i);
  60.  
  61.             samples[i].length = 0;
  62.             samples[i].data = NULL;
  63.            
  64.             fclose(sample);
  65.  
  66.             continue;
  67.         }
  68.  
  69.         samples[i].data = malloc(samples[i].length);
  70.         if (fread(samples[i].data, 1, samples[i].length, sample) != samples[i].length)
  71.         {
  72.             printf("- Couldn't read all the bytes from %s... Skipping sample\n", file_to_open);
  73.            
  74.             samples[i].length = 0;
  75.             fclose(sample);
  76.  
  77.             free(samples[i].data);
  78.  
  79.             continue;
  80.         }
  81.  
  82.         sample_count++;
  83.  
  84.         fclose(sample);
  85.     }
  86.  
  87.     if (sample_count == 0)
  88.     {
  89.         printf("Failed!\n");
  90.         printf("- No samples found...\n");
  91.  
  92.         return 1;
  93.     }
  94.     printf("OK\n");
  95.  
  96.     // Order count and pattern count
  97.     srand((unsigned int)time(NULL));
  98.  
  99.     order_count = (rand() + 1) % MAX_ORDERS;
  100.     pattern_count = (rand() + 1) % MAX_PATTERNS;
  101.     // ------------------------------------
  102.  
  103.     printf("* Opening %s for writing... ", FILENAME);
  104.     fmodule = fopen(FILENAME, "wb");
  105.     if (fmodule == NULL)
  106.     {
  107.         printf("Failed!\n");
  108.  
  109.         for (i = 0; i < 31; i++)
  110.         {
  111.             if (samples[i].data != NULL)
  112.                 free(samples[i].data);
  113.         }
  114.  
  115.         return 1;
  116.     }
  117.     printf("OK\n");
  118.  
  119.     // Song title
  120.     strcpy(MOD_name, SONGNAME); // TODO: Make random name generator? Meh
  121.     putstr_padzero(MOD_name, 20, fmodule);
  122.  
  123.     // Samples
  124.     printf("* Writing sample information... ");
  125.     srand((unsigned int)time(NULL));
  126.     for (i = 0; i < 31; i++)
  127.     {
  128.         unsigned short loop_start, loop_end;
  129.  
  130.         loop_start = (unsigned short)samples[i].length ? (rand() % ((unsigned short)samples[i].length - 4)) : 0;
  131.         loop_end = loop_start ? ((rand() + loop_start) % ((unsigned short)samples[i].length - 2)) : 0;
  132.  
  133.         // Sample name
  134.         if (samples[i].length != 0)
  135.         {
  136.             char sample_name[22];
  137.             sprintf(sample_name, "Sample %02d", i + 1);
  138.             putstr_padzero(sample_name, 22, fmodule);
  139.         }
  140.         else
  141.         {
  142.             putstr_padzero("", 22, fmodule);
  143.         }
  144.  
  145.         // Sample length
  146.         fputc(samples[i].length >> 9, fmodule);
  147.         fputc(samples[i].length >> 1, fmodule);
  148.  
  149.         fputc(0, fmodule); // Finetune
  150.         fputc(64, fmodule); // Volume
  151.  
  152.         // Loop start
  153.         fputc(loop_start >> 9, fmodule);
  154.         fputc(loop_start >> 1, fmodule);
  155.  
  156.         // Loop end
  157.         fputc(loop_end >> 9, fmodule);
  158.         fputc(loop_end >> 1, fmodule);
  159.     }
  160.     printf("OK\n");
  161.  
  162.     fputc(order_count, fmodule); // Number of orders
  163.     fputc(0, fmodule); // Restart position
  164.  
  165.     // Write order list
  166.     srand((unsigned int)time(NULL));
  167.     for (i = 0; i < 128; i++)
  168.         fputc(rand() % pattern_count, fmodule);
  169.  
  170.     // File format tag
  171.     if (pattern_count <= 64) // Yes... Stupid ProTracker versions
  172.     {
  173.         strcpy(MOD_tag, "M.K.");
  174.         fwrite(MOD_tag, 1, 4, fmodule);
  175.     }
  176.     else
  177.     {
  178.         strcpy(MOD_tag, "M!K!");
  179.         fwrite(MOD_tag, 1, 4, fmodule);
  180.     }
  181.  
  182.     // Write pattern data
  183.     printf("* Writing pattern data... ");
  184.     {
  185.         int pattern;
  186.  
  187.         srand((unsigned int)time(NULL));
  188.         for (pattern = 0; pattern < pattern_count; pattern++)
  189.         {
  190.             int row;
  191.             for (row = 0; row < 64; row++)
  192.             {
  193.                 int channel;
  194.                 for (channel = 0; channel < 4; channel++)
  195.                 {
  196.                     unsigned short period = 0;
  197.                     char sample_number = 0, command = 0, param = 0;
  198.  
  199.                     int note_rand, cmd_rand;
  200.  
  201.                     note_rand = rand() % 6;
  202.                     if (note_rand == 1)
  203.                     {
  204.                         period = rand() % 420;
  205.                         if (period < 150) period = 0;
  206.                     }
  207.                     if (period != 0)
  208.                     {
  209.                         sample_number = rand() % 31;
  210.                         if ((samples[sample_number].length == 0) || (sample_number == 0))
  211.                         {
  212.                             sample_number = 0;
  213.                             period = 0;
  214.                         }
  215.                     }
  216.  
  217.                     // Trigger volume command
  218.                     cmd_rand = rand() % 8; // less often please
  219.                     if (cmd_rand == 1)
  220.                     {
  221.                         command = 0x0C;
  222.                         param = rand() % 0x40;
  223.                     }
  224.                     // Trigger vibrato
  225.                     cmd_rand = rand() % 16; // less often
  226.                     if (cmd_rand == 1)
  227.                     {
  228.                         command = 0x04;
  229.                         param = (rand() + 0x80) % 0x28;
  230.                     }
  231.  
  232.                     // Trigger portamento
  233.                     cmd_rand = rand() % 4; // less often
  234.                     if ((cmd_rand == 1) && sample_number)
  235.                     {
  236.                         command = 0x03;
  237.                         param = rand() % 0x80;
  238.                     }
  239.  
  240.                     fputc(((period >> 8) & 0x0F) | (sample_number & 0x10), fmodule);
  241.                     fputc(period & 0xFF, fmodule);
  242.                     fputc(((sample_number & 0x0F) << 4) | (command & 0x0F), fmodule);
  243.                     fputc(param, fmodule);
  244.                 }
  245.             }
  246.         }
  247.     }
  248.     printf("OK\n");
  249.  
  250.     // Write sample data
  251.     printf("* Writing sample data... ");
  252.     for (i = 0; i < 31; i++)
  253.     {
  254.         char *ptr = samples[i].data;
  255.         if (ptr == NULL) continue;
  256.  
  257.         for (j = 0; j < (signed)samples[i].length; j++)
  258.             fputc(*ptr++, fmodule);
  259.     }
  260.     printf("OK\n");
  261.  
  262.     for (i = 0; i < 31; i++)
  263.     {
  264.         if (samples[i].data != NULL)
  265.             free(samples[i].data);
  266.     }
  267.  
  268.     MOD_size = ftell(fmodule);
  269.  
  270.     fclose(fmodule);
  271.  
  272.     printf("\n");
  273.     printf(" +------------------+ \n");
  274.     printf(" | .MOD information | \n");
  275.     printf(" +------------------+ \n");
  276.     printf("\n");
  277.     printf("  Name:     %s\n", MOD_name);
  278.     printf("  Tag:      %s\n", MOD_tag);
  279.     printf("  Orders:   %03d/128\n", order_count);
  280.     printf("  Patterns: %03d/127\n", pattern_count);
  281.     printf("  Samples:  %02d/31\n", sample_count);
  282.     printf("\n");
  283.     printf("  Filename: %s\n", FILENAME);
  284.     printf("  Filesize: %dkB\n", MOD_size / 1000);
  285.     printf("\n");
  286.  
  287.     return 0;
  288. }
  289.  
  290. void putstr_padzero(char *str, int length, FILE *fmodule)
  291. {
  292.     int str_len = strlen(str);
  293.  
  294.     int i;
  295.     for (i = 0; i < length; i++)
  296.     {
  297.         if (i < str_len)
  298.             fputc(str[i], fmodule);
  299.         else
  300.             fputc('\0', fmodule);
  301.     }
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement