struct song * do_read_song (char *s, int type) { struct song *song; struct compression *comp; FILE *fp; char buf[200]; PRINT (("Loading %s... ", s)); fflush (stdout); /* Check if the filename matches any compression extentions */ for (comp = comp_table; comp->extension != NULL; comp++) { if (check_ext (comp->extension, s)) { sprintf (buf, comp->decomp_cmd, s); fp = popen (buf, "rb"); if (fp == NULL) { fprintf (stderr, "Unable to open compressed file %s\n", s); return NULL; } song = read_song (fp, type, transpose); /* now flush pipe, needed for compressed files that fail */ while (fgetc(fp) != EOF); pclose (fp); break; } } /* No compression extentions matched, so just load it straight */ if (!comp->extension) { fp = fopen (s, "rb"); if (fp == NULL) { fprintf (stderr, "Unable to open tune file %s\n", s); return NULL; } song = read_song (fp, type, transpose); fclose (fp); } if (song) PRINT (("Ok!\n")); return song; }
/*============================ LOAD LIBRARY FROM FILE ============================*/ void load_MusicLibrary() { char ch; while ((ch = fgetc(m_fp)) != EOF) { ungetc(ch, m_fp); read_song(); } }
int main(int argc, char** argv) { fftw_real out[N], in[N]; rfftw_plan plan_backward; buffer_t buffer[N]; int i, time = 0; song_t* head; song_t* next; print_prologoue(N, SR); next = head = read_song("song.txt"); dump_song(head); plan_backward = rfftw_create_plan(N, FFTW_COMPLEX_TO_REAL, FFTW_ESTIMATE); while ( next ) { // clear out for ( i = 0; i < N; i++ ) out[i] = 0.0f; i = 0; while (i < ACCORD_SIZE && next->accord[i] != 0) play_note(next->accord[i++], ADSR(time, next->duration), out); rfftw_one(plan_backward, out, in); for ( i = 0; i < N; i++ ) buffer[i] = limit_output(in[i]);; write(1, buffer, N* sizeof(buffer_t)); time ++; if ( time == next->duration ) { // play next note next = next->next; time = 0; // loop: if (next == NULL) next = head; } } rfftw_destroy_plan(plan_backward); free_song(head); print_epilogue(); return 0; }
static int song_action(expression_t *arg, sgen_ctx_t *c) { song_t s; if (c->num_songs == 0) { c->songs = malloc(sizeof(song_t)); } ++c->num_songs; c->songs = realloc(c->songs, c->num_songs*sizeof(song_t)); if (!read_song(arg, &s, c)) return 0; printf("sgen: found song block \"%s\" with tracks: ", s.name); wlist_print(s.active_track_ids); c->songs[c->num_songs-1] = s; return 1; }
int play(FILE *file, int debug) { SDL_AudioSpec audiospec; Song *song = NULL; g_debug = debug; song = read_song(file); if(g_debug) fprintf(stderr, "Initializing SDL...\n"); SDL_Init(SDL_INIT_AUDIO | SDL_INIT_TIMER); audiospec.freq = 44100; audiospec.format = AUDIO_S16SYS; audiospec.channels = 2; audiospec.samples = 1024; audiospec.callback = audio_callback; audiospec.userdata = (void*)song; if(SDL_OpenAudio(&audiospec, NULL) < 0) { fprintf(stderr, "Unable to open audio context\n"); return 1; } if(g_debug) fprintf(stderr, "Starting audio...\n"); SDL_PauseAudio(0); while(!song->ended) SDL_Delay(500); if(g_debug) fprintf(stderr, "Stopping audio and exiting gracefully\n"); SDL_CloseAudio(); SDL_Quit(); return 0; }