int construction::adjusted_time() const { int final_time = time; int assistants = 0; for( auto &elem : g->u.get_crafting_helpers() ) { if( elem->get_skill_level( skill ) >= difficulty ) { assistants++; } } if( assistants >= 2 ) { final_time *= 0.4f; } else if( assistants == 1 ) { final_time *= 0.75f; } final_time *= time_scale(); return final_time; }
u_int32_t AmAdaptivePlayout::read(u_int32_t ts, int16_t* buf, u_int32_t len) { bool do_plc=false; if(ts_less()(w_ts,ts+len) && (plc_cnt < 6)){ if(!plc_cnt){ int nlen = time_scale(w_ts-len,2.0, len); wsola_off += nlen-len; } else { do_plc = true; } plc_cnt++; } if(do_plc){ short plc_buf[FRAMESZ]; for(unsigned int i=0; i<len/FRAMESZ; i++){ fec.dofe(plc_buf); buffer_put(w_ts,plc_buf,FRAMESZ); } buffer_get(ts,buf,len); } else { buffer_get(ts,buf,len); for(unsigned int i=0; i<len/FRAMESZ; i++) fec.addtohistory(buf + i*FRAMESZ); } return len; }
void AmAdaptivePlayout::write(u_int32_t ref_ts, u_int32_t ts, int16_t* buf, u_int32_t len) { // predict next delay u_int32_t p_delay = next_delay(ref_ts,ts); u_int32_t old_off = wsola_off; ts += old_off; if(short_scaled.mean() > 2.0){ if(shr_threshold < 3000) shr_threshold += 10; } else if(short_scaled.mean() < 1.0){ if(shr_threshold > 100) shr_threshold -= 2; } // need to scale? if( ts_less()(wsola_off+EXP_THRESHOLD,p_delay) || // expand packet ts_less()(p_delay+shr_threshold,wsola_off) ) { // shrink packet wsola_off = p_delay; } else { if(ts_less()(r_ts,ts+len)){ plc_cnt = 0; buffer_put(ts,buf,len); } else { // lost } // statistics short_scaled.push(0.0); return; } int32_t n_len = len + wsola_off - old_off; if(n_len < 0) n_len = 1; float f = float(n_len) / float(len); if(f > TSM_MAX_SCALE) f = TSM_MAX_SCALE; n_len = (int32_t)(float(len) * f); if(ts_less()(ts+n_len,r_ts)){ // statistics short_scaled.push(0.0); return; } u_int32_t old_wts = w_ts; buffer_put(ts,buf,len); n_len = time_scale(ts,f,len); wsola_off = old_off + n_len - len; //ts += n_len - len; if(w_ts != old_wts) plc_cnt = 0; // statistics short_scaled.push(100.0); }
int main(int argc, char *argv[]) { AFfilehandle inhandle; AFfilehandle outhandle; AFfilesetup filesetup; int frames; int new_frames; int out_frames; int count; time_scale_t state; float x; float rate; int16_t in[BLOCK_LEN]; int16_t out[5*BLOCK_LEN]; if ((inhandle = afOpenFile(IN_FILE_NAME, "r", 0)) == AF_NULL_FILEHANDLE) { printf(" Cannot open wave file '%s'\n", IN_FILE_NAME); exit(2); } if ((x = afGetFrameSize(inhandle, AF_DEFAULT_TRACK, 1)) != 2.0) { printf(" Unexpected frame size in wave file '%s'\n", IN_FILE_NAME); exit(2); } if ((x = afGetRate(inhandle, AF_DEFAULT_TRACK)) != (float) SAMPLE_RATE) { printf(" Unexpected sample rate in wave file '%s'\n", IN_FILE_NAME); exit(2); } if ((x = afGetChannels(inhandle, AF_DEFAULT_TRACK)) != 1.0) { printf(" Unexpected number of channels in wave file '%s'\n", IN_FILE_NAME); exit(2); } if ((filesetup = afNewFileSetup()) == AF_NULL_FILESETUP) { fprintf(stderr, " Failed to create file setup\n"); exit(2); } afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16); afInitRate(filesetup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE); afInitFileFormat(filesetup, AF_FILE_WAVE); afInitChannels(filesetup, AF_DEFAULT_TRACK, 1); if ((outhandle = afOpenFile(OUT_FILE_NAME, "w", filesetup)) == AF_NULL_FILEHANDLE) { fprintf(stderr, " Cannot create wave file '%s'\n", OUT_FILE_NAME); exit(2); } rate = 1.8; time_scale_init(&state, rate); count = 0; while ((frames = afReadFrames(inhandle, AF_DEFAULT_TRACK, in, BLOCK_LEN))) { new_frames = time_scale(&state, out, in, frames); out_frames = afWriteFrames(outhandle, AF_DEFAULT_TRACK, out, new_frames); if (out_frames != new_frames) { fprintf(stderr, " Error writing wave file\n"); exit(2); } if (++count > 100) { if (rate > 0.5) { rate -= 0.1; if (rate >= 0.99 && rate <= 1.01) rate -= 0.1; printf("Rate is %f\n", rate); time_scale_init(&state, rate); } count = 0; } } if (afCloseFile(inhandle) != 0) { printf(" Cannot close wave file '%s'\n", IN_FILE_NAME); exit(2); } if (afCloseFile(outhandle) != 0) { printf(" Cannot close wave file '%s'\n", OUT_FILE_NAME); exit(2); } afFreeFileSetup(filesetup); return 0; }