xm-rand-pattern.c

来自「Genetic Programing of music」· C语言 代码 · 共 123 行

C
123
字号
#include <stdio.h>#include <stdlib.h>#include <math.h>#include <time.h>#include "xm_lib.h"#define MALLOC malloc/* *   createRandomNoteSequence - generates a string of 'length' random notes *      between lowOctave and highOctave, with a probability of a note at *      any point being noteProb (the rest are rests).  Notes will fall between *      the C of the low octave and the C of the high octave. Octave values *      should range between 0 and 5. * *      Note: drand48() is used; the caller should seed the f'n if needed *            with srand48() */      char *createRandomNoteSequence(int length, double noteProb,			 int lowOctave, int highOctave){  char *noteString;  int i;  /* allocate noteString */  noteString=(char *)MALLOC((length+1)*sizeof(char));  /* generate notes */  for(i=0;i<length;i++)    if ( drand48() < noteProb )      noteString[i]=(char)floor(12*(drand48()*(highOctave-lowOctave+1)				    +lowOctave)+1);    else      noteString[i]=88;  return noteString;} /* end of createRandomNoteSequence */intmain(int argc, char *argv[]) {  int i;  int playFile=0;  long seed;  char command[255];  char *filename=NULL;  FILE *xmFile=NULL;  FILE *outPutFile=NULL;  char *noteString;  time_t tempTime;    /* seed the random number generator */  seed=(long)time(&tempTime);  srand48(seed);  /* parse command line arguments */  for (i=1; i<argc; i++) {        if (strcmp (argv[i], "-v") == 0) {      outPutFile=stdout;         } else if (strcmp (argv[i], "-p") == 0) {      playFile=1;         } else if (strcmp (argv[i], "-h") == 0) {      fprintf(stderr, "\nSyntax: %s [-h] [-v] <filename>\n\n"	      "\tCreates a random XM pattern, with four channels. "	      "Only the first\nhas notes, and only instrument 0 is used. "	      "The output\nis placed into <filename>\n\n"	      "\t-h  = display this help message\n"	      "\t-v  = verbose description of created pattern\n"	      "\t-p  = play created file\n\n",	      argv[0]);      exit(1);    } else {      filename = argv[i];    }  }  /* set appropriate output file */  if(outPutFile==NULL)    outPutFile=fopen("/dev/null","w+");    /*   * Open XM File   */  if(filename != NULL)    xmFile = fopen(filename,"w+");    if (xmFile==NULL) {        fprintf(stderr,"Couln't open %s\n",filename);    exit(1);  }  /* Create the pattern information */  noteString=createRandomNoteSequence(200, 0.25, 0, 5);  fwriteXMPatternInfo(xmFile, noteString);  /* Now print pattern info */  fseek(xmFile, 0, SEEK_SET);  fprintf(outPutFile, "Pattern Information:\n");  fprintXMPatternInfo(xmFile, outPutFile, 4, 1);  /* Now play the file if requested */  if(playFile) {    sprintf(command, "play_pattern %s", filename);     system(command);  }    fclose(outPutFile);  fclose(xmFile);    return 0;  } /* end of main */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?