📄 xm-pattern-generate.c
字号:
#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 *//* * createUserNoteSequence - generates a note sequence string based on user * input. Input values are verified to be in the proper range. */ char *createUserNoteSequence(){ char *noteString; char response[80]; int note; int done=0; int i=0; noteString=(char *)MALLOC(256*sizeof(char)); printf("Please enter the note sequence one note at a time.\n" "Notes are from 'C-0' to 'B-6', '---' means rest," "blank terminates.\nNo more than 255 notes/rests may be used\n\n"); response[1]=0x0; /* while we have a resonse */ while(!done) { /* keep trying to we get a note (only check for three chars */ while(strlen(response) != 3 && strlen(response) != 0) {getnote: printf("Note %d: ",i); fflush(stdout); gets(response); } /* check for empty line */ if(strlen(response)==0) done=1; else { /* if we got a rest, put a rest */ if(response[2] == '-') { noteString[i]=88; /* otherwise, figure out the note value */ } else { /* capitalize if needed */ if(response[0]>=0x61) response[0]-=32; printf("Response was: %s\n",response); /* get the octave */ sscanf(&(response[2]),"%d",¬e); note*=12; /* add in the note offset within the octave */ switch(response[0]) { case 'D': note+=2; break; case 'E': note+=4; break; case 'F': note+=5; break; case 'G': note+=7; break; case 'A': note-=3; break; case 'B': note-=1; break; default: note+=0; break; } /* if sharp, increment note value */ if(response[1]=='#') note++; /* increment note, since for string notes are 1-72 (0=terminate in string) */ note++; noteString[i]=(char)note; } if(note < 1 || note > 72) { printf("Invalid note, please re-enter.\n"); goto getnote; } printf("Note value entered as: %d\n", (int)noteString[i]); i++; response[1]=0x0; if(i>254) break; } } /* write final null character */ noteString[i]=0x0; return noteString;} /* end of createUserNoteSequence */intmain(int argc, char *argv[]) { int i; int playFile=0; int random=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], "-r") == 0) { random=1; } else if (strcmp (argv[i], "-h") == 0) { fprintf(stderr, "\nSyntax: %s [-h] [-v] <filename>\n\n" "\tCreates a XM pattern, with four channels. " "The user is\nallowed to type in decimal note values." "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" "\t-r = create random pattern\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 */ if(random) { noteString=createRandomNoteSequence(200, 0.25, 0, 5); } else { noteString=createUserNoteSequence(); } 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -