void Music_Emu::set_fade( int start_msec, int length_msec ) { fade_set = true; this->length_msec = start_msec; this->fade_msec = length_msec; track_filter.set_fade( start_msec < 0 ? Track_Filter::indefinite_count : msec_to_samples( start_msec ), length_msec * sample_rate() / (1000 / stereo) ); }
blargg_err_t Music_Emu::seek( int msec ) { int time = msec_to_samples( msec ); if ( time < track_filter.sample_count() ) { RETURN_ERR( start_track( current_track_ ) ); if ( fade_set ) set_fade( length_msec, fade_msec ); } return skip( time - track_filter.sample_count() ); }
SAMPLE * create_silence_sample(unsigned long ms) { SAMPLE * sp = NULL; int bits; int stereo; int freq; unsigned long samples, i; unsigned channels; eof_log("create_silence_sample() entered", 1); if(eof_music_track) { bits = alogg_get_wave_bits_ogg(eof_music_track); stereo = alogg_get_wave_is_stereo_ogg(eof_music_track); freq = alogg_get_wave_freq_ogg(eof_music_track); samples = msec_to_samples(ms); channels = stereo ? 2 : 1; } else { bits = 16; stereo = 1; freq = 44100; samples = (double)(ms * freq) / 1000.0; channels = 2; } sp = create_sample(bits, stereo, freq, samples); if(!sp) return NULL; //Return error if(bits == 8) { for(i = 0; i < samples * channels; i++) { ((unsigned char *)(sp->data))[i] = 0x80; } } else { for(i = 0; i < samples * channels; i++) { ((unsigned short *)(sp->data))[i] = 0x8000; } } return sp; }
int eof_add_silence_recode(char * oggfn, unsigned long ms) { char sys_command[1024] = {0}; char backupfn[1024] = {0}; char wavfn[1024] = {0}; char soggfn[1024] = {0}; ALOGG_OGG *oggfile = NULL; SAMPLE *decoded = NULL, *combined = NULL; int bits; int stereo; int freq; unsigned long samples; int channels; unsigned long ctr,index; void * oggbuffer = NULL; int bitrate; eof_log("eof_add_silence_recode() entered", 1); if(!oggfn || (ms == 0) || eof_silence_loaded) { return 41; //Return failure: Invalid parameters } set_window_title("Adjusting Silence..."); /* back up original file */ (void) snprintf(backupfn, sizeof(backupfn) - 1, "%s.backup", oggfn); if(!exists(backupfn)) { (void) eof_copy_file(oggfn, backupfn); } /* Decode the OGG file into memory */ //Load OGG file into memory oggbuffer = eof_buffer_file(oggfn, 0); //Decode the OGG from buffer instead of from file because the latter cannot support special characters in the file path due to limitations with fopen() if(!oggbuffer) { (void) snprintf(eof_log_string, sizeof(eof_log_string) - 1, "\tError reading OGG: \"%s\"", strerror(errno)); //Get the Operating System's reason for the failure eof_log(eof_log_string, 1); return 42; //Return failure: Could not buffer chart audio into memory } oggfile=alogg_create_ogg_from_buffer(oggbuffer, (int)file_size_ex(oggfn)); if(oggfile == NULL) { eof_log("ALOGG failed to open input audio file", 1); free(oggbuffer); return 43; //Return failure: Could not process buffered chart audio } //Decode OGG into memory decoded=alogg_create_sample_from_ogg(oggfile); if(decoded == NULL) { alogg_destroy_ogg(oggfile); free(oggbuffer); return 44; //Return failure: Could not decode chart audio to memory } /* Create a SAMPLE array large enough for the leading silence and the decoded OGG */ bits = alogg_get_wave_bits_ogg(oggfile); stereo = alogg_get_wave_is_stereo_ogg(oggfile); freq = alogg_get_wave_freq_ogg(oggfile); alogg_destroy_ogg(oggfile); //This is no longer needed oggfile = NULL; samples = msec_to_samples(ms); channels = stereo ? 2 : 1; combined = create_sample(bits,stereo,freq,samples+decoded->len); //Create a sample array long enough for the silence and the OGG file if(combined == NULL) { destroy_sample(decoded); return 45; //Return failure: Could not create a sample array for the combined audio } /* Add the PCM data for the silence */ if(bits == 8) { //Create 8 bit PCM data for(ctr=0,index=0;ctr < samples * channels;ctr++) { ((unsigned char *)(combined->data))[index++] = 0x80; } } else { //Create 16 bit PCM data for(ctr=0,index=0;ctr < samples * channels;ctr++) { ((unsigned short *)(combined->data))[index++] = 0x8000; } } /* Add the decoded OGG PCM data*/ if(bits == 8) { //Copy 8 bit PCM data for(ctr=0;ctr < decoded->len * channels;ctr++) { ((unsigned char *)(combined->data))[index++] = ((unsigned char *)(decoded->data))[ctr]; } } else { //Copy 16 bit PCM data for(ctr=0;ctr < decoded->len * channels;ctr++) { ((unsigned short *)(combined->data))[index++] = ((unsigned short *)(decoded->data))[ctr]; } } /* encode the audio */ destroy_sample(decoded); //This is no longer needed free(oggbuffer); (void) replace_filename(wavfn, eof_song_path, "encode.wav", 1024); (void) save_wav(wavfn, combined); destroy_sample(combined); //This is no longer needed (void) replace_filename(soggfn, eof_song_path, "encode.ogg", 1024); bitrate = alogg_get_bitrate_ogg(eof_music_track) / 1000; if(!bitrate) { //A user found that in an audio file with a really high sample rate (ie. 96KHz), alogg_get_bitrate_ogg() may return zero instead of an expected value bitrate = 256; //In case this happens, use a bitrate of 256Kbps, which should be good enough for a very high quality file } #ifdef ALLEGRO_WINDOWS (void) uszprintf(sys_command, (int) sizeof(sys_command) - 1, "oggenc2 -o \"%s\" -b %d \"%s\"", soggfn, bitrate, wavfn); #else (void) uszprintf(sys_command, (int) sizeof(sys_command) - 1, "oggenc -o \"%s\" -b %d \"%s\"", soggfn, bitrate, wavfn); #endif (void) snprintf(eof_log_string, sizeof(eof_log_string) - 1, "\tCalling oggenc as follows: %s", sys_command); eof_log(eof_log_string, 1); if(eof_system(sys_command)) { //If oggenc failed, retry again by specifying a quality level (specifying bitrate can fail in some circumstances) eof_log("\t\toggenc failed. Retrying by specifying a quality level instead of a target bitrate", 1); #ifdef ALLEGRO_WINDOWS (void) uszprintf(sys_command, (int) sizeof(sys_command) - 1, "oggenc2 -o \"%s\" -q 9 \"%s\"", soggfn, wavfn); #else (void) uszprintf(sys_command, (int) sizeof(sys_command) - 1, "oggenc -o \"%s\" -q 9 \"%s\"", soggfn, wavfn); #endif (void) snprintf(eof_log_string, sizeof(eof_log_string) - 1, "\tCalling oggenc as follows: %s", sys_command); eof_log(eof_log_string, 1); if(eof_system(sys_command)) { //If oggenc failed again char tempfname[30] = {0}; char redirect[35] = {0}; if(eof_validate_temp_folder()) { //Ensure the correct working directory and presence of the temporary folder eof_log("\tCould not validate working directory and temp folder", 1); return 46; //Return failure: Could not validate cwd and temp folder } (void) snprintf(tempfname, sizeof(tempfname) - 1, "%soggenc.log", eof_temp_path_s); (void) snprintf(redirect, sizeof(redirect) - 1, " 2> %s", tempfname); (void) ustrzcat(sys_command, (int) sizeof(sys_command) - 1, redirect); //Append a redirection to the command to capture the output of oggenc if(eof_system(sys_command)) { //Run one last time to catch the error output (void) snprintf(eof_log_string, sizeof(eof_log_string) - 1, "\tOggenc failed. Please see %s for any errors it gave.", tempfname); eof_log(eof_log_string, 1); eof_fix_window_title(); return 47; //Return failure: Could not encode combined audio } } } /* replace the current OGG file with the new file */ (void) eof_copy_file(soggfn, oggfn); //Copy encode.ogg to the filename of the original OGG /* clean up */ (void) delete_file(soggfn); //Delete encode.ogg (void) delete_file(wavfn); //Delete encode.wav if(eof_load_ogg(oggfn, 0)) { //If the combined audio was loaded eof_fix_waveform_graph(); eof_fix_spectrogram(); eof_fix_window_title(); eof_chart_length = eof_music_length; return 0; //Return success } eof_fix_window_title(); return 48; //Return error: Could not load new audio }