void Resampler::initialize() { if(dstSamplesPerSec_ == srcSamplesPerSec_){ convertTypeFunc_ = getConvTypeFunc( dstNumChannels_, dstBytesPerSample_, srcNumChannels_, srcBytesPerSample_); }else{ if(resamplerChannels_ != srcNumChannels_){ destroyResampler(); } if(NULL == resampler_){ resampler_ = speex_resampler_init(srcNumChannels_, srcSamplesPerSec_, dstSamplesPerSec_, SPEEX_RESAMPLER_QUALITY_DEFAULT, NULL); }else{ speex_resampler_set_rate(resampler_, srcSamplesPerSec_, dstSamplesPerSec_); speex_resampler_reset_mem(resampler_); } resampleRate_ = static_cast<f32>(dstSamplesPerSec_)/srcSamplesPerSec_; convertTypeFunc_ = getConvTypeFunc( dstNumChannels_, dstBytesPerSample_, srcNumChannels_, dstBytesPerSample_); } }
void RtpAudioStream::reset(double start_rel_time) { global_start_rel_time_ = start_rel_time; stop_rel_time_ = start_rel_time_; audio_out_rate_ = 0; max_sample_val_ = 1; packet_timestamps_.clear(); visual_samples_.clear(); out_of_seq_timestamps_.clear(); jitter_drop_timestamps_.clear(); if (audio_resampler_) { speex_resampler_reset_mem(audio_resampler_); } if (visual_resampler_) { speex_resampler_reset_mem(visual_resampler_); } tempfile_->seek(0); }
static void resampler_reset(struct resampler_itfe *resampler) { struct resampler *rsmp = (struct resampler *)resampler; rsmp->frames_in = 0; rsmp->frames_rq = 0; if (rsmp != NULL && rsmp->speex_resampler != NULL) { speex_resampler_reset_mem(rsmp->speex_resampler); } }
bool AudioResampler::Seek(size_t offset, Origin origin) { if (wrapped_decoder->Seek(offset, origin)) { //reset conversion data conversion_data.input_frames = 0; conversion_data.input_frames_used = 0; finished = wrapped_decoder->IsFinished(); #if defined(HAVE_LIBSPEEXDSP) speex_resampler_reset_mem(conversion_state); #elif defined(HAVE_LIBSAMPLERATE) src_reset(conversion_state); #endif return true; } return false; }
int main(int argc, char **argv) { CALLGRIND_STOP_INSTRUMENTATION; CALLGRIND_ZERO_STATS; QCoreApplication a(argc, argv); QFile f((argc >= 2) ? argv[1] : "wb_male.wav"); if (! f.open(QIODevice::ReadOnly)) { qFatal("Failed to open file!"); } f.seek(36 + 8); QFile o("output.raw"); if (!(RUNNING_ON_VALGRIND)) if (! o.open(QIODevice::WriteOnly)) qFatal("Failed to open output!"); QFile vf((argc >= 3) ? argv[2] : "verify.raw"); if (! vf.open(QIODevice::ReadOnly)) { qWarning("Failed to open validate file!"); } QDataStream out(&o); QDataStream verify(&vf); const int iFrameSize = 320; QVector<QByteArray> v; while (1) { QByteArray qba = f.read(iFrameSize * 2); if (qba.size() != iFrameSize * 2) break; v.append(qba); } int nframes = v.size(); qWarning("Ready to process %d frames of %d samples", nframes, iFrameSize); QVector<short *> qvInShort; QVector<float *> qvIn; QVector<float *> qvDirect; QVector<float *> qvInterpolate; QVector<float *> qvInterpolateMC; QVector<short *> qvInterpolateShort; QVector<float *> qv8; QVector<float *> qv96; const float sfraq1 = tfreq1 / 16000.0f; float fOutSize1 = iFrameSize * sfraq1; int iOutSize1 = lroundf(fOutSize1); const float sfraq2 = tfreq2 / 16000.0f; float fOutSize2 = iFrameSize * sfraq2; int iOutSize2 = lroundf(fOutSize2); int iOutSize8 = iFrameSize / 2; int iOutSize96 = iFrameSize * 6; if (RUNNING_ON_VALGRIND) nframes = qMin(nframes, 10); QVector<float> fInput(nframes * iFrameSize); QVector<float> fDirect(nframes * iOutSize1); QVector<float> fInterpolate(nframes * iOutSize2); QVector<float> fInterpolateMC(nframes * iOutSize2); QVector<short> sInterpolate(nframes * iOutSize2); QVector<float> f96(nframes * iOutSize96); QVector<float> f8(nframes *iOutSize8); for (int i=0;i<nframes;i++) { short *s = reinterpret_cast<short *>(v[i].data()); float *f = fInput.data() + i * iFrameSize; for (int j=0;j<iFrameSize;j++) f[j]=s[j]+20; qvInShort.append(s); qvIn.append(f); qvDirect.append(fDirect.data() + i * iOutSize1); qvInterpolate.append(fInterpolate.data() + i * iOutSize2); qvInterpolateMC.append(fInterpolateMC.data() + i * iOutSize2); qvInterpolateShort.append(sInterpolate.data() + i * iOutSize2); qv8.append(f8.data() + i * iOutSize8); qv96.append(f96.data() + i * iOutSize96); } int err; SpeexResamplerState *srs1 = speex_resampler_init(1, 16000, lroundf(tfreq1), qual, &err); SpeexResamplerState *srs2 = speex_resampler_init(1, 16000, lroundf(tfreq2), qual, &err); SpeexResamplerState *srs2i = speex_resampler_init(1, 16000, lroundf(tfreq2), qual, &err); SpeexResamplerState *srss = speex_resampler_init(3, 16000, lroundf(tfreq2), qual, &err); SpeexResamplerState *srsto96 = speex_resampler_init(1, 16000, 96000, 5, &err); SpeexResamplerState *srs8to96 = speex_resampler_init(1, 8000, 96000, qual, &err); SpeexResamplerState *srs96to8 = speex_resampler_init(1, 96000, 8000, qual, &err); #ifdef Q_OS_WIN if (!SetPriorityClass(GetCurrentProcess(),REALTIME_PRIORITY_CLASS)) qWarning("Application: Failed to set priority!"); #endif int len; spx_uint32_t inlen; spx_uint32_t outlen; Timer t; quint64 e; if (! RUNNING_ON_VALGRIND) { #ifndef Q_OS_WIN struct sched_param sp; sp.sched_priority = sched_get_priority_max(SCHED_FIFO); cpu_set_t cpuset; CPU_ZERO(&cpuset); CPU_SET(1, &cpuset); if (sched_setscheduler(getpid(), SCHED_FIFO, &sp) != 0) qWarning("No realtime."); if (mlockall(MCL_CURRENT | MCL_FUTURE) != 0) qWarning("No mlock."); if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) qWarning("No affinity"); sched_yield(); #endif for (int i=0;i<nframes;++i) { inlen = iFrameSize; outlen = iOutSize96; speex_resampler_process_float(srsto96, 0, qvIn[i], &inlen, qv96[i], &outlen); } QVector<unsigned long long> qvTimes; QPair<unsigned long long, unsigned long long> ci; for (int j=0;j<loops;j++) { t.restart(); for (int i=0;i<nframes;i++) { inlen = iFrameSize; outlen = iOutSize1; speex_resampler_process_float(srs1, 0, qvIn[i], &inlen, qvDirect[i], &outlen); } e = t.elapsed(); qvTimes.append(e); } ci = confint(qvTimes); qWarning("Direct: %8llu +/- %3llu usec (%d)", ci.first, ci.second, qvTimes.count(), qvTimes.count()); qvTimes.clear(); for (int j=0;j<loops;j++) { t.restart(); for (int i=0;i<nframes;i++) { inlen = iFrameSize; outlen = iOutSize2; speex_resampler_process_float(srs2, 0, qvIn[i], &inlen, qvInterpolate[i], &outlen); } e = t.elapsed(); qvTimes.append(e); } ci = confint(qvTimes); qWarning("Interpolate: %8llu +/- %3llu usec (%d)", ci.first, ci.second, qvTimes.count()); qvTimes.clear(); for (int j=0;j<loops;j++) { t.restart(); for (int i=0;i<nframes;i++) { inlen = iOutSize96; outlen = iOutSize8; speex_resampler_process_float(srs96to8, 0, qv96[i], &inlen, qv8[i], &outlen); } e = t.elapsed(); qvTimes.append(e); } ci = confint(qvTimes); qWarning("96 => 8: %8llu +/- %3llu usec (%d)", ci.first, ci.second, qvTimes.count()); qvTimes.clear(); t.restart(); for (int j=0;j<loops;j++) { t.restart(); for (int i=0;i<nframes;i++) { inlen = iOutSize8; outlen = iOutSize96; speex_resampler_process_float(srs8to96, 0, qv8[i], &inlen, qv96[i], &outlen); } e = t.elapsed(); qvTimes.append(e); } ci = confint(qvTimes); qWarning("8 => 96: %8llu +/- %3llu usec (%d)", ci.first, ci.second, qvTimes.count()); speex_resampler_reset_mem(srs1); speex_resampler_reset_mem(srs2); } t.restart(); CALLGRIND_START_INSTRUMENTATION; for (int i=0;i<nframes;i++) { inlen = iFrameSize; outlen = iOutSize1; speex_resampler_process_float(srs1, 0, qvIn[i], &inlen, qvDirect[i], &outlen); inlen = iFrameSize; outlen = iOutSize2; speex_resampler_process_float(srs2, 0, qvIn[i], &inlen, qvInterpolate[i], &outlen); inlen = iFrameSize; outlen = iOutSize2; speex_resampler_process_int(srs2i, 0, qvInShort[i], &inlen, qvInterpolateShort[i], &outlen); inlen = iFrameSize / 4; outlen = iOutSize2 / 4; speex_resampler_process_interleaved_float(srss, qvIn[i], &inlen, qvInterpolateMC[i], &outlen); } e = t.elapsed(); #ifdef Q_OS_WIN if (!SetPriorityClass(GetCurrentProcess(),NORMAL_PRIORITY_CLASS)) qWarning("Application: Failed to reset priority!"); #endif const int freq[10] = { 22050, 32000, 11025, 16000, 48000, 41000, 8000, 96000, 11025, 1600 }; QVector<float> fMagic; for (int f=0;f<10;f++) { float fbuff[32767]; speex_resampler_set_rate(srs1, 16000, freq[f]); for (int q = 0;q < 10;q++) { speex_resampler_set_quality(srs1, (3*q) % 7); inlen = iFrameSize; outlen = 32767; speex_resampler_process_float(srs1, 0, qvIn[(f*10+q) % nframes], &inlen, fbuff, &outlen); for (int j=0;j<outlen;j++) fMagic.append(fbuff[j]); } inlen = iFrameSize; outlen = 32767; speex_resampler_process_float(srs1, 0, NULL, &inlen, fbuff, &outlen); for (int j=0;j<outlen;j++) fMagic.append(fbuff[j]); } // Cropped magic test for (int f=0;f<10;f++) { float fbuff[32767]; speex_resampler_set_rate(srs1, 16000, freq[f]); for (int q = 0;q < 10;q++) { speex_resampler_set_quality(srs1, (3*q) % 7); inlen = iFrameSize; outlen = 16; speex_resampler_process_float(srs1, 0, qvIn[(f*10+q) % nframes], &inlen, fbuff, &outlen); for (int j=0;j<outlen;j++) fMagic.append(fbuff[j]); } inlen = iFrameSize; outlen = 32767; speex_resampler_process_float(srs1, 0, NULL, &inlen, fbuff, &outlen); for (int j=0;j<outlen;j++) fMagic.append(fbuff[j]); } CALLGRIND_STOP_INSTRUMENTATION; qWarning("Used %llu usec", e); qWarning("%.2f times realtime", (20000ULL * nframes) / (e * 1.0)); if (! RUNNING_ON_VALGRIND) { QVector<float> vDirect; QVector<float> vInterpolate; QVector<short> vsInterpolate; QVector<float> vMagic; QVector<float> vInterpolateMC; out << fDirect << fInterpolate << sInterpolate << fMagic << fInterpolateMC; if (vf.isOpen()) { verify >> vDirect >> vInterpolate >> vsInterpolate >> vMagic >> vInterpolateMC; double rmsd = veccomp(vDirect, fDirect, "SRS1"); double rmsi = veccomp(vInterpolate, fInterpolate, "SRS2"); veccomp(vsInterpolate, sInterpolate, "SRS2i"); veccomp(vMagic, fMagic, "Magic"); veccomp(vInterpolateMC, fInterpolateMC, "MC"); qWarning("Direct: %g", rmsd); qWarning("Interp: %g", rmsi); } else {
void Resampler::reset() { if(NULL != resampler_){ speex_resampler_reset_mem(resampler_); } }