Пример #1
0
void StarSeemTest::star_external_ffem()
{
    QFETCH(QString, data);

    AudioBuffer c;

    QVERIFY2(c.read(data), ("Failed reading test wave; " + data).toUtf8().data());

    /*=== WORLD による分析 ===*/
    int timeLength = GetSamplesForDIO(c.format().sampleRate(), c.length(), msFramePeriod);
    int fftLength = GetFFTSizeForStar(c.format().sampleRate());
    double *f0 = new double[timeLength];
    double *t = new double[timeLength];
    double **specgram = new double*[timeLength];
    specgram[0] = new double[timeLength * (fftLength / 2 + 1)];
    for(int i = 1; i < timeLength; i++)
    {
        specgram[i] = specgram[0] + i * (fftLength / 2 + 1);
    }

    Dio(c.data()[0], c.length(), c.format().sampleRate(), msFramePeriod, t, f0);
    Star(c.data()[0], c.length(), c.format().sampleRate(), msFramePeriod, f0, timeLength, specgram);
    /*=== WORLD による分析ここまで ===*/

    /*=== StarSeem による分析 === */
    Envelope *e = new Envelope;
    QVERIFY2(DioFfem().estimate(e, c.data()[0], c.length(), c.format().sampleRate(), msFramePeriod), "Failed dio FFEM");
    // Envelope が正しいか確認する
    for(int i = 0; i < e->size(); i++)
    {
        QVERIFY2(e->value(i) == f0[i], "Error; invalid f0 envelope");
    }
    Specgram *testset = new Specgram;
    QVERIFY2(StarSeem(e).estimate(testset, c.data()[0], c.length(), fftLength, c.format().sampleRate(), msFramePeriod), "Failed STAR SEEM");
    /*=== StarSeem による分析ここまで === */

    // 比較開始
    for(int t = 0; t < timeLength; t++)
    {
        for(int f = 0; f <= fftLength / 2; f++)
        {
            if(testset->value(t, f) != specgram[t][f])
            {
                QString s;
                s = "t = " + QString::number(t) + ", f = " + QString::number(f);
                s += " actual :" + QString::number(testset->value(t, f)) + " , expceted :" + QString::number(specgram[t][f]);
                // WORLD 自体は同じだから値が全く同じでないとおかしい。
                QFAIL(("Error ;" + s).toUtf8().data());
            }
        }
    }

    delete[] specgram[0];
    delete[] specgram;
    delete[] t;
    delete[] f0;
}
Пример #2
0
void AudioBuffer::convert(const AudioSpec &_new_spec)
{
	if(_new_spec == m_spec) {
		return;
	}

	AudioSpec new_spec = _new_spec;
	AudioBuffer dest[2];
	unsigned bufidx = 0;
	AudioBuffer *source = this;

	if(source->rate() != new_spec.rate) {
#if HAVE_LIBSAMPLERATE
		if(source->format() != AUDIO_FORMAT_F32) {
			dest[1].set_spec({AUDIO_FORMAT_F32, source->channels(), source->rate()});
			source->convert_format(dest[1], source->frames());
			source = &dest[1];
		}
		dest[0].set_spec({source->format(), source->channels(), new_spec.rate});
		source->convert_rate(dest[0], source->frames(), nullptr);
		source = &dest[0];
		bufidx = 1;
#else
		new_spec.rate = source->rate();
#endif
	}
	if(source->channels() != new_spec.channels) {
		dest[bufidx].set_spec({source->format(),new_spec.channels,source->rate()});
		source->convert_channels(dest[bufidx], source->frames());
		source = &dest[bufidx];
		bufidx = (bufidx + 1) % 2;
	}
	if(source->format() != new_spec.format) {
		dest[bufidx].set_spec({new_spec.format,source->channels(),source->rate()});
		source->convert_format(dest[bufidx], source->frames());
		source = &dest[bufidx];
	}

	if(new_spec != m_spec) {
		m_data = source->m_data;
		m_spec = new_spec;
	}
}
Пример #3
0
void AudioBuffer::convert_format(AudioBuffer &_dest, unsigned _frames_count)
{
	AudioSpec destspec{_dest.format(), m_spec.channels, m_spec.rate};

	if(_dest.spec() != destspec) {
		throw std::logic_error("destination must have same channels and rate");
	}

	_frames_count = std::min(frames(),_frames_count);

	if(m_spec.format == destspec.format) {
		_dest.add_frames(*this,_frames_count);
		return;
	}

	const unsigned samples_count = m_spec.frames_to_samples(_frames_count);
	std::vector<uint8_t> buffer, *data=&buffer;
	switch(m_spec.format) {
		case AUDIO_FORMAT_U8:
			u8_to_f32(m_data, buffer, samples_count);
			break;
		case AUDIO_FORMAT_S16:
			s16_to_f32(m_data, buffer, samples_count);
			break;
		case AUDIO_FORMAT_F32:
			data = &m_data;
			break;
		default:
			throw std::logic_error("unsupported source format");
	}
	switch(destspec.format) {
		case AUDIO_FORMAT_S16:
			f32_to_s16(*data, _dest.m_data, samples_count);
			break;
		case AUDIO_FORMAT_F32:
			_dest.m_data.swap(buffer);
			break;
		default:
			throw std::logic_error("unsupported destination format");
	}
}