void al_duh_set_loop(AL_DUH_PLAYER *dp, int loop) { DUH_SIGRENDERER *sr = al_duh_get_sigrenderer(dp); DUMB_IT_SIGRENDERER *itsr = duh_get_it_sigrenderer(sr); if (itsr == NULL) return; if (loop) dumb_it_set_loop_callback(itsr, NULL, NULL); else dumb_it_set_loop_callback(itsr, dumb_it_callback_terminate, itsr); }
static int cdumb_read (DB_fileinfo_t *_info, char *bytes, int size) { trace ("cdumb_read req %d\n", size); dumb_info_t *info = (dumb_info_t *)_info; int samplesize = (_info->fmt.bps >> 3) * _info->fmt.channels; int length = size / samplesize; long ret; DUMB_IT_SIGRENDERER *itsr = duh_get_it_sigrenderer (info->renderer); if (conf_play_forever && info->can_loop) dumb_it_set_loop_callback (itsr, &cdumb_it_callback_loop_forever, NULL); else dumb_it_set_loop_callback (itsr, &dumb_it_callback_terminate, NULL); ret = duh_render (info->renderer, _info->fmt.bps, 0, 1, 65536.f / _info->fmt.samplerate, length, bytes); _info->readpos += ret / (float)_info->fmt.samplerate; trace ("cdumb_read %d\n", ret*samplesize); return ret*samplesize; }
static int cdumb_startrenderer (DB_fileinfo_t *_info) { dumb_info_t *info = (dumb_info_t *)_info; // reopen if (info->renderer) { duh_end_sigrenderer (info->renderer); info->renderer = NULL; } info->renderer = duh_start_sigrenderer (info->duh, 0, 2, 0); if (!info->renderer) { return -1; } DUMB_IT_SIGRENDERER *itsr = duh_get_it_sigrenderer (info->renderer); dumb_it_set_loop_callback (itsr, &dumb_it_callback_terminate, NULL); int q = conf_resampling_quality; if (q < 0) { q = 0; } else if (q >= DUMB_RQ_N_LEVELS) { q = DUMB_RQ_N_LEVELS - 1; } dumb_it_set_resampling_quality (itsr, q); dumb_it_set_xm_speed_zero_callback (itsr, &dumb_it_callback_terminate, NULL); dumb_it_set_global_volume_zero_callback (itsr, &dumb_it_callback_terminate, NULL); int rq = conf_ramping_style; if (rq < 0) { rq = 0; } else if (rq > 2) { rq = 2; } dumb_it_set_ramp_style(itsr, rq); dumb_it_sr_set_global_volume (itsr, conf_global_volume); return 0; }
int main(int argc, const char *const *argv) /* I'm const-crazy! */ { DUH *duh; /* Encapsulates the music file. */ AL_DUH_PLAYER *dp; /* Holds the current playback state. */ /* Initialise Allegro */ if (allegro_init()) return EXIT_FAILURE; /* Check that we have one argument (plus the executable name). */ if (argc != 2) usage(argv[0]); /* Tell Allegro where to find configuration data. This means you can * put any settings for Allegro in dumb.ini. See Allegro's * documentation for more information. */ set_config_file("dumb.ini"); /* Initialise Allegro's keyboard input. */ if (install_keyboard()) { allegro_message("Failed to initialise keyboard driver!\n"); return EXIT_FAILURE; } /* This function call is appropriate for a program that will play one * sample or one audio stream at a time. If you have sound effects * too, you may want to increase the parameter. See Allegro's * documentation for details on what the parameter means. Note that * newer versions of Allegro act as if set_volume_per_voice() was * called with parameter 1 initially, while older versions behave as * if -1 was passed, so you should call the function if you want * consistent behaviour. */ set_volume_per_voice(0); /* Initialise Allegro's sound output system. */ if (install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL)) { allegro_message("Failed to initialise sound driver!\n%s\n", allegro_error); return EXIT_FAILURE; } /* dumb_exit() is a function defined by DUMB. This operation arranges * for dumb_exit() to be called last thing before the program exits. * dumb_exit() does a bit of cleaning up for you. atexit() is * declared in stdlib.h. */ atexit(&dumb_exit); /* DUMB defines its own wrappers for file input. There is a struct * called DUMBFILE that holds function pointers for the various file * operations needed by DUMB. You can decide whether to use stdio * FILE objects, Allegro's PACKFILEs or something else entirely. No * wrapper is installed initially, so you must call this or * dumb_register_stdfiles() or set up your own before trying to load * modules by file name. (If you are using another method, such as * loading an Allegro datafile with modules embedded in it, then DUMB * never opens a file by file name so this doesn't apply.) */ dumb_register_packfiles(); /* Load the module file into a DUH object. Quick and dirty: try the * loader for each format until one succeeds. Note that 15-sample * mods have no identifying features, so dumb_load_mod() may succeed * on files that aren't mods at all. We therefore try that one last. */ duh = dumb_load_it(argv[1]); if (!duh) { duh = dumb_load_xm(argv[1]); if (!duh) { duh = dumb_load_s3m(argv[1]); if (!duh) { duh = dumb_load_mod(argv[1]); if (!duh) { allegro_message("Failed to load %s!\n", argv[1]); return EXIT_FAILURE; } } } } /* Read the quality values from the config file we told Allegro to * use. You may want to hardcode these or provide a more elaborate * interface via which the user can control them. */ dumb_resampling_quality = get_config_int("sound", "dumb_resampling_quality", 4); dumb_it_max_to_mix = get_config_int("sound", "dumb_it_max_to_mix", 128); /* If we're not in DOS, show a window and register our close hook * function. */ # ifndef ALLEGRO_DOS { const char *fn = get_filename(argv[1]); gfx_half_width = strlen(fn); if (gfx_half_width < 22) gfx_half_width = 22; gfx_half_width = (gfx_half_width + 2) * 4; /* set_window_title() is not const-correct (yet). */ set_window_title((char *)"DUMB Music Player"); if (set_gfx_mode(GFX_DUMB_MODE, gfx_half_width*2, 80, 0, 0) == 0) { acquire_screen(); textout_centre(screen, font, fn, gfx_half_width, 20, 14); textout_centre(screen, font, "Press any key to exit.", gfx_half_width, 52, 11); release_screen(); } else gfx_half_width = 0; } /* Silly check to get around the fact that someone stupidly removed * an old function from Allegro instead of deprecating it. The old * function was put back a version later, but we may as well use the * new one if it's there! */ # if ALLEGRO_VERSION*10000 + ALLEGRO_SUB_VERSION*100 + ALLEGRO_WIP_VERSION >= 40105 set_close_button_callback(&closehook); # else set_window_close_hook(&closehook); # endif # endif /* We want to continue running if the user switches to another * application. */ set_display_switch_mode(SWITCH_BACKGROUND); /* We have the music loaded, but it isn't playing yet. This starts it * playing. We construct a second object, the AL_DUH_PLAYER, to * represent the playing music. This means you can play the music * twice at the same time should you want to! * * Specify the number of channels (2 for stereo), which 'signal' to * play (always 0 for modules), the volume (1.0f for default), the * buffer size (4096 generally works well) and the sampling frequency * (ideally match the final output frequency Allegro is using). An * Allegro audio stream will be started. */ dp = al_start_duh(duh, 2, 0, 1.0f, get_config_int("sound", "buffer_size", 4096), get_config_int("sound", "sound_freq", 44100)); /* Register our callback functions so that they are called when the * music loops or stops. See docs/howto.txt for more information. * There is no threading issue: DUMB will only process playback * in al_poll_duh(), which we call below. */ { DUH_SIGRENDERER *sr = al_duh_get_sigrenderer(dp); DUMB_IT_SIGRENDERER *itsr = duh_get_it_sigrenderer(sr); dumb_it_set_loop_callback(itsr, &loop_callback, NULL); dumb_it_set_xm_speed_zero_callback(itsr, &xm_speed_zero_callback, NULL); } /* Main loop. */ for (;;) { /* Check for keys in the buffer. If we get one, discard it * and exit the main loop. */ if (keypressed()) { readkey(); break; } /* Poll the music. We exit the loop if al_poll_duh() has * returned nonzero (music finished) or the window has been * closed. al_poll_duh() might return nonzero if you have set * up a callback that tells the music to stop. */ if (al_poll_duh(dp) || closed) break; /* Give other threads a look-in, or allow the processor to * sleep for a bit. YIELD() is defined further up in this * file. */ YIELD(); } /* Remove the audio stream and deallocate the memory being used for * the playback state. We set dp to NULL to emphasise that the object * has gone. */ al_stop_duh(dp); dp = NULL; /* Free the DUH object containing the actual music data. */ unload_duh(duh); duh = NULL; /* All done! */ return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { int retcode = 1; int nerrors = 0; streamer_t streamer; memset(&streamer, 0, sizeof(streamer_t)); // Signal handlers signal(SIGINT, sig_fn); signal(SIGTERM, sig_fn); // Initialize SDL2 if (SDL_Init(SDL_INIT_AUDIO) != 0) { fprintf(stderr, "%s\n", SDL_GetError()); return 1; } // Defaults streamer.freq = 44100; streamer.n_channels = 2; streamer.bits = 16; streamer.volume = 1.0f; streamer.quality = DUMB_RQ_CUBIC; // commandline argument parser options struct arg_lit *arg_help = arg_lit0("h", "help", "print this help and exits"); struct arg_dbl *arg_volume = arg_dbl0("v", "volume", "<volume", "sets the output volume (-8.0 to +8.0, default 1.0)"); struct arg_int *arg_samplerate = arg_int0( "s", "samplerate", "<freq>", "sets the sampling rate (default 44100)"); struct arg_int *arg_quality = arg_int0( "r", "quality", "<quality>", "specify the resampling quality to use"); struct arg_lit *arg_mono = arg_lit0("m", "mono", "generate mono output instead of stereo"); struct arg_lit *arg_eight = arg_lit0("8", "eight", "generate 8-bit instead of 16-bit"); struct arg_lit *arg_noprogress = arg_lit0("n", "noprogress", "hide progress bar"); struct arg_file *arg_output = arg_file0("o", "output", "<file>", "output file"); struct arg_file *arg_input = arg_file1(NULL, NULL, "<file>", "input module file"); struct arg_end *arg_fend = arg_end(20); void *argtable[] = {arg_help, arg_input, arg_volume, arg_samplerate, arg_quality, arg_mono, arg_eight, arg_noprogress, arg_fend}; const char *progname = "dumbplay"; // Make sure everything got allocated if (arg_nullcheck(argtable) != 0) { fprintf(stderr, "%s: insufficient memory\n", progname); goto exit_0; } // Parse inputs nerrors = arg_parse(argc, argv, argtable); // Handle help if (arg_help->count > 0) { fprintf(stderr, "Usage: %s", progname); arg_print_syntax(stderr, argtable, "\n"); fprintf(stderr, "\nArguments:\n"); arg_print_glossary(stderr, argtable, "%-25s %s\n"); goto exit_0; } // Handle libargtable errors if (nerrors > 0) { arg_print_errors(stderr, arg_fend, progname); fprintf(stderr, "Try '%s --help' for more information.\n", progname); goto exit_0; } // Handle the switch options streamer.input = arg_input->filename[0]; if (arg_eight->count > 0) { streamer.bits = 8; } if (arg_mono->count > 0) { streamer.n_channels = 1; } if (arg_noprogress->count > 0) { streamer.no_progress = true; } if (arg_volume->count > 0) { streamer.volume = arg_volume->dval[0]; if (streamer.volume < -8.0f || streamer.volume > 8.0f) { fprintf(stderr, "Volume must be between -8.0f and 8.0f.\n"); goto exit_0; } } if (arg_samplerate->count > 0) { streamer.freq = arg_samplerate->ival[0]; if (streamer.freq < 1 || streamer.freq > 96000) { fprintf(stderr, "Sampling rate must be between 1 and 96000.\n"); goto exit_0; } } if (arg_quality->count > 0) { streamer.quality = arg_quality->ival[0]; if (streamer.quality < 0 || streamer.quality >= DUMB_RQ_N_LEVELS) { fprintf(stderr, "Quality must be between %d and %d.\n", 0, DUMB_RQ_N_LEVELS - 1); goto exit_0; } } // Load source file. dumb_register_stdfiles(); streamer.src = dumb_load_any(streamer.input, 0, 0); if (!streamer.src) { fprintf(stderr, "Unable to load file %s for playback!\n", streamer.input); goto exit_0; } // Set up playback streamer.renderer = duh_start_sigrenderer(streamer.src, 0, streamer.n_channels, 0); streamer.delta = 65536.0f / streamer.freq; streamer.sbytes = (streamer.bits / 8) * streamer.n_channels; streamer.ssize = duh_get_length(streamer.src); // Stop producing samples on module end DUMB_IT_SIGRENDERER *itsr = duh_get_it_sigrenderer(streamer.renderer); dumb_it_set_loop_callback(itsr, &dumb_it_callback_terminate, NULL); dumb_it_set_xm_speed_zero_callback(itsr, &dumb_it_callback_terminate, NULL); dumb_it_set_resampling_quality(itsr, streamer.quality); // Set up the SDL2 format we want for playback. SDL_AudioSpec want; SDL_zero(want); want.freq = streamer.freq; want.format = (streamer.bits == 16) ? AUDIO_S16 : AUDIO_S8; want.channels = streamer.n_channels; want.samples = SAMPLES; want.callback = stream_audio; want.userdata = &streamer; // Find SDL2 audio device, and request the format we just set up. // SDL2 will tell us what we got in the "have" struct. SDL_AudioSpec have; streamer.dev = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0); if (streamer.dev == 0) { fprintf(stderr, "%s\n", SDL_GetError()); goto exit_1; } // Make sure we got the format we wanted. If not, stop here. if (have.format != want.format) { fprintf(stderr, "Could not get correct playback format.\n"); goto exit_2; } // Play file SDL_PauseAudioDevice(streamer.dev, 0); // Show initial state of the progress bar (if it is enabled) int time_start = SDL_GetTicks(); float seek = 0.0f; int ms_played = 0; if (!streamer.no_progress) { show_progress(PROGRESSBAR_LENGTH, seek, ms_played); } // Loop while dumb is still giving data. Update progressbar if enabled. while (!stop_signal && !streamer.ended) { if (!streamer.no_progress) { seek = ((float)streamer.spos) / ((float)streamer.ssize); ms_played = SDL_GetTicks() - time_start; show_progress(PROGRESSBAR_LENGTH, seek, ms_played); } SDL_Delay(100); } // We made it this far without crashing, so let's just exit with no error :) retcode = 0; // Free up resources and exit. if (streamer.sig_samples) { destroy_sample_buffer(streamer.sig_samples); } exit_2: SDL_CloseAudioDevice(streamer.dev); exit_1: if (streamer.renderer) { duh_end_sigrenderer(streamer.renderer); } if (streamer.src) { unload_duh(streamer.src); } exit_0: arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); SDL_Quit(); return retcode; }
void init_sigrenderer(DUH_SIGRENDERER *sr) { DUMB_IT_SIGRENDERER *itsr = duh_get_it_sigrenderer(sr); dumb_it_set_loop_callback(itsr, &loop_callback, NULL); dumb_it_set_xm_speed_zero_callback(itsr, &dumb_it_callback_terminate, NULL); }