/* * NAME: timer->abs() * DESCRIPTION: return the absolute value of a timer */ mad_timer_t mad_timer_abs(mad_timer_t timer) { if (timer.seconds < 0) mad_timer_negate(&timer); return timer; }
/* * NAME: timer->abs() * DESCRIPTION: return the absolute value of a timer */ mad_timer_t mad_timer_abs(mad_timer_t timer) { if (mad_timer_sign(timer) < 0) mad_timer_negate(&timer); return timer; }
/* * NAME: timer->multiply() * DESCRIPTION: multiply a timer by a scalar value */ void mad_timer_multiply(mad_timer_t *timer, s32 scalar) { mad_timer_t addend; u32 factor; factor = scalar; if (scalar < 0) { factor = -scalar; mad_timer_negate(timer); } addend = *timer; *timer = mad_timer_zero; while (factor) { if (factor & 1) mad_timer_add(timer, addend); mad_timer_add(&addend, addend); factor >>= 1; } }
/* * NAME: timer->multiply() * DESCRIPTION: multiply a timer by a scalar value */ void ICACHE_FLASH_ATTR mad_timer_multiply(mad_timer_t *timer, signed long scalar) { mad_timer_t addend; unsigned long factor; factor = scalar; if (scalar < 0) { factor = -scalar; mad_timer_negate(timer); } addend = *timer; *timer = mad_timer_zero; while (factor) { if (factor & 1) mad_timer_add(timer, addend); mad_timer_add(&addend, addend); factor >>= 1; } }
enum mad_flow read_header(void *data, struct mad_header const * header) { char long_currenttime_str[14]; /* this *will* fill if you're using 100000+ minute mp3s */ char long_remaintime_str[14]; static int frames_played = 0; buffer *playbuf = (buffer *)data; mad_timer_t time_remaining; char *ao_time; if (stop_playing_file) { stop_playing_file = 0; status = MPG321_STOPPED; return MAD_FLOW_STOP; } if(options.opt & MPG321_REMOTE_PLAY) { enum mad_flow mf; /* We might have to stop if the user inputs something */ if ((mf = remote_get_input_nowait(playbuf))) return mf; } /* Stop playing if -n is used, and we're at the frame specified. */ if ((playbuf->max_frames != -1) && (frames_played++ > playbuf->max_frames)) { frames_played = 0; status = MPG321_STOPPED; if(options.opt & MPG321_ENABLE_BUFFER) Decoded_Frames->done = 1; //fprintf(stderr,"Total D: %d\n",Decoded_Frames->total_decoded_frames); return MAD_FLOW_STOP; } current_frame++; mad_timer_add(¤t_time, header->duration); if(options.opt & MPG321_USE_SCROBBLER && scrobbler_time > 0 && scrobbler_time < current_time.seconds) { scrobbler_time = -1; scrobbler_report(); } if(options.opt & (MPG321_VERBOSE_PLAY | MPG321_REMOTE_PLAY)) { mad_timer_string(current_time, long_currenttime_str, "%.2u:%.2u.%.2u", MAD_UNITS_MINUTES, MAD_UNITS_CENTISECONDS, 0); if (mad_timer_compare(playbuf->duration, mad_timer_zero) == 0) time_remaining = current_time; else time_remaining = playbuf->duration; mad_timer_negate(¤t_time); mad_timer_add(&time_remaining, current_time); mad_timer_negate(¤t_time); mad_timer_string(time_remaining, long_remaintime_str, "%.2u:%.2u.%.2u", MAD_UNITS_MINUTES, MAD_UNITS_CENTISECONDS, 0); } /* update cached table of frames & times */ if (current_frame <= playbuf->num_frames) /* we only allocate enough for our estimate. */ { playbuf->frames[current_frame] = playbuf->frames[current_frame-1] + (header->bitrate / 8 / 1000) * mad_timer_count(header->duration, MAD_UNITS_MILLISECONDS); playbuf->times[current_frame] = current_time; } if (file_change) { file_change = 0; if (options.opt & MPG321_REMOTE_PLAY) { printf("@S %s %d %d %s %d %ld %d %d %d %d %ld %d\n",versionstring(header->flags), header->layer, header->samplerate, modestringucase(header->mode), header->mode_extension, (header->bitrate / 8 / 100) * mad_timer_count(header->duration, MAD_UNITS_CENTISECONDS), MAD_NCHANNELS(header), header->flags & MAD_FLAG_COPYRIGHT ? 1 : 0, header->flags & MAD_FLAG_PROTECTION ? 1 : 0, header->emphasis, header->bitrate/1000, header->mode_extension); } else if (options.opt & MPG321_VERBOSE_PLAY)/*zip it good*/ { fprintf(stderr, "MPEG %s, Layer: %s, Freq: %d, mode: %s, modext: %d, BPF : %ld\n" "Channels: %d, copyright: %s, original: %s, CRC: %s, emphasis: %d.\n" "Bitrate: %ld Kbits/s, Extension value: %d\n" "Audio: 1:1 conversion, rate: %d, encoding: signed 16 bit, channels: %d\n", versionstring(header->flags),layerstring(header->layer), header->samplerate, modestringucase(header->mode), header->mode_extension, (header->bitrate / 100) * mad_timer_count(header->duration, MAD_UNITS_CENTISECONDS), MAD_NCHANNELS(header), header->flags & MAD_FLAG_COPYRIGHT ? "Yes" : "No", header->flags & MAD_FLAG_ORIGINAL ? "Yes" : "No", header->flags & MAD_FLAG_PROTECTION ? "Yes" : "No", header->emphasis, header->bitrate/1000, header->mode_extension, header->samplerate, MAD_NCHANNELS(header)); } else if (!(options.opt & MPG321_QUIET_PLAY))/*I love Joey*/ { fprintf(stderr, "MPEG %s layer %s, %ld kbit/s, %d Hz %s\n", versionstring(header->flags),layerstring(header->layer), header->bitrate/1000, header->samplerate, modestring(header->mode)); } } if (status == MPG321_SEEKING && options.seek) { if (!--options.seek) status = MPG321_PLAYING; return MAD_FLOW_IGNORE; } else { status = MPG321_PLAYING; } if(!(options.opt & MPG321_ENABLE_BUFFER)) { if(count > 0) { count++; if(count > 40) { if(!(options.opt & MPG321_VERBOSE_PLAY)) fprintf(stderr," \r"); count = -1; fflush(stderr); } } } if(options.opt & MPG321_ENABLE_BUFFER) { ao_time = (Output_Queue+mad_decoder_position)->time; (Output_Queue+mad_decoder_position)->num_frames = playbuf->num_frames - current_frame; if(Decoded_Frames->is_file) (Output_Queue+mad_decoder_position)->seconds = time_remaining.seconds; } if (options.opt & MPG321_VERBOSE_PLAY) { if (!options.skip_printing_frames || (options.skip_printing_frames && !(current_frame % options.skip_printing_frames))) if(count > 0) { /* if(options.opt & MPG321_ENABLE_BUFFER) { sprintf(ao_time, "Frame# %5lu [%5lu], Time: %s [%s], \r", current_frame, playbuf->num_frames > 0 ? playbuf->num_frames - current_frame : 0, long_currenttime_str, long_remaintime_str); //sprintf(ao_time, "Frame# %5lu [%5lu], Time: %s [%s], \r", current_frame, // playbuf->num_frames > 0 ? playbuf->num_frames - current_frame : 0, long_currenttime_str, long_remaintime_str); //sprintf(ao_time, "Volume: %d%% Frame# %5lu [%5lu], Time: %s [%s], \r",volume, current_frame, // playbuf->num_frames > 0 ? playbuf->num_frames - current_frame : 0, long_currenttime_str, long_remaintime_str); }else*/ { fprintf(stderr, "Volume: %d%% Frame# %5lu [%5lu], Time: %s [%s], \r",volume, current_frame, playbuf->num_frames > 0 ? playbuf->num_frames - current_frame : 0, long_currenttime_str, long_remaintime_str); } } else if(count < 0) { if(options.opt & MPG321_ENABLE_BUFFER) { // sprintf(ao_time, "Frame# %5lu [%5lu], Time: %s [%s], \r", current_frame, sprintf(ao_time, "Frame# %5lu [%5lu], Time: %s [%s], \r", current_frame, playbuf->num_frames > 0 ? playbuf->num_frames - current_frame : 0, long_currenttime_str, long_remaintime_str); } else { fprintf(stderr, "Frame# %5lu [%5lu], Time: %s [%s], \r", current_frame, playbuf->num_frames > 0 ? playbuf->num_frames - current_frame : 0, long_currenttime_str, long_remaintime_str); } } } else if (options.opt & MPG321_REMOTE_PLAY) { if (!options.skip_printing_frames || (options.skip_printing_frames && !(current_frame % options.skip_printing_frames))) { if(options.opt & MPG321_ENABLE_BUFFER) { sprintf(ao_time,"@F %ld %ld %.2f %.2f\n", current_frame, playbuf->num_frames - current_frame, ((double)mad_timer_count(current_time, MAD_UNITS_CENTISECONDS)/100.0), ((double)mad_timer_count(time_remaining, MAD_UNITS_CENTISECONDS)/100.0)); } else { fprintf(stderr,"@F %ld %ld %.2f %.2f\n", current_frame, playbuf->num_frames - current_frame, ((double)mad_timer_count(current_time, MAD_UNITS_CENTISECONDS)/100.0), ((double)mad_timer_count(time_remaining, MAD_UNITS_CENTISECONDS)/100.0)); } } } return MAD_FLOW_CONTINUE; }
/* * NAME: parse_time() * DESCRIPTION: parse a time specification string */ static int parse_time(mad_timer_t *timer, char const *str) { mad_timer_t time, accum = mad_timer_zero; signed long decimal; unsigned long seconds, fraction, fracpart; int minus; while (isspace((unsigned char) *str)) ++str; do { seconds = fraction = fracpart = 0; switch (*str) { case '-': ++str; minus = 1; break; case '+': ++str; default: minus = 0; } do { decimal = strtol(str, (char **) &str, 10); if (decimal < 0) return -1; seconds += decimal; if (*str == ':') { seconds *= 60; ++str; } } while (*str >= '0' && *str <= '9'); if (*str == '.' # if defined(HAVE_LOCALECONV) || *str == *localeconv()->decimal_point # endif ) { char const *ptr; decimal = strtol(++str, (char **) &ptr, 10); if (decimal < 0) return -1; fraction = decimal; for (fracpart = 1; str != ptr; ++str) fracpart *= 10; } else if (*str == '/') { ++str; decimal = strtol(str, (char **) &str, 10); if (decimal < 0) return -1; fraction = seconds; fracpart = decimal; seconds = 0; } mad_timer_set(&time, seconds, fraction, fracpart); if (minus) mad_timer_negate(&time); mad_timer_add(&accum, time); } while (*str == '-' || *str == '+'); while (isspace((unsigned char) *str)) ++str; if (*str != 0) return -1; *timer = accum; return 0; }
/* a -= b */ static void mad_timer_sub(mad_timer_t *a, mad_timer_t b) { /* a = a - b -> a = a + -b */ mad_timer_negate(&b); mad_timer_add(a, b); }
/* * NAME: fadein_filter() * DESCRIPTION: fade-in filter */ enum mad_flow fadein_filter(void *data, struct mad_frame *frame) { struct player *player = data; if (mad_timer_compare(player->stats.play_timer, player->fade_in) < 0) { mad_timer_t frame_start, frame_end, ratio; unsigned int nch, nsamples, s; mad_fixed_t step, scalefactor; /* * Fade-in processing may occur over the entire frame, or it may end * somewhere within the frame. Find out where processing should end. */ nsamples = MAD_NSBSAMPLES(&frame->header); /* this frame has not yet been added to play_timer */ frame_start = frame_end = player->stats.play_timer; mad_timer_add(&frame_end, frame->header.duration); if (mad_timer_compare(player->fade_in, frame_end) < 0) { mad_timer_t length; length = frame_start; mad_timer_negate(&length); mad_timer_add(&length, player->fade_in); mad_timer_set(&ratio, 0, mad_timer_count(length, frame->header.samplerate), mad_timer_count(frame->header.duration, frame->header.samplerate)); nsamples = mad_timer_fraction(ratio, nsamples); } /* determine starting scalefactor and step size */ mad_timer_set(&ratio, 0, mad_timer_count(frame_start, frame->header.samplerate), mad_timer_count(player->fade_in, frame->header.samplerate)); scalefactor = mad_timer_fraction(ratio, MAD_F_ONE); step = MAD_F_ONE / (mad_timer_count(player->fade_in, frame->header.samplerate) / 32); /* scale subband samples */ nch = MAD_NCHANNELS(&frame->header); for (s = 0; s < nsamples; ++s) { unsigned int ch, sb; for (ch = 0; ch < nch; ++ch) { for (sb = 0; sb < 32; ++sb) { frame->sbsample[ch][s][sb] = mad_f_mul(frame->sbsample[ch][s][sb], scalefactor); } } scalefactor += step; } } return MAD_FLOW_CONTINUE; }