示例#1
0
SINT SoundSourceOpus::readSampleFramesStereo(
        SINT numberOfFrames, CSAMPLE* sampleBuffer,
        SINT sampleBufferSize) {
    DEBUG_ASSERT(isValidFrameIndex(m_curFrameIndex));
    DEBUG_ASSERT(getSampleBufferSize(numberOfFrames, true) <= sampleBufferSize);

    const SINT numberOfFramesTotal = math_min(
            numberOfFrames, getMaxFrameIndex() - m_curFrameIndex);

    CSAMPLE* pSampleBuffer = sampleBuffer;
    SINT numberOfFramesRemaining = numberOfFramesTotal;
    while (0 < numberOfFramesRemaining) {
        int readResult = op_read_float_stereo(m_pOggOpusFile,
                pSampleBuffer,
                numberOfFramesRemaining * 2); // stereo
        if (0 < readResult) {
            m_curFrameIndex += readResult;
            pSampleBuffer += readResult * 2; // stereo
            numberOfFramesRemaining -= readResult;
        } else {
            qWarning() << "Failed to read sample data from OggOpus file:"
                    << readResult;
            break; // abort
        }
    }

    DEBUG_ASSERT(isValidFrameIndex(m_curFrameIndex));
    DEBUG_ASSERT(numberOfFramesTotal >= numberOfFramesRemaining);
    return numberOfFramesTotal - numberOfFramesRemaining;
}
示例#2
0
void cmdMusic(const std::string& args) {
	// terrible hack for testing audio
	// decode the entire file into a big buffer all at once

	OggOpusFile* opusFile;
	if (args.length() == 0) {
		opusFile = op_open_memory(
		    static_cast<const uint8_t*>(EMBED_DATA(Who_Likes_to_Party_Kevin_MacLeod_incompetech_opus)),
		    EMBED_SIZE(Who_Likes_to_Party_Kevin_MacLeod_incompetech_opus),
		    NULL);
	} else {
		opusFile = op_open_file(args.c_str(), NULL);
	}

	if (!opusFile) {
		narf::console->println("Failed to open music file " + args);
		return;
	}

	auto newMusicSize = op_pcm_total(opusFile, -1) * 2; // stereo
	auto newMusicSamples = new float[newMusicSize];

	size_t decoded = 0;
	while (decoded < newMusicSize) {
		auto rc = op_read_float_stereo(opusFile, newMusicSamples + decoded, newMusicSize - decoded);
		if (rc < 0) {
			narf::console->println("opusfile decode failed");
			decoded = 0;
			break;
		}

		decoded += rc * 2; // return code is number of samples per channel, and we are decoding in stereo
	}

	if (decoded != newMusicSize) {
		narf::console->println("opusfile decode returned wrong number of samples (got " + std::to_string(decoded) + ", expected " + std::to_string(newMusicSize) + ")");
		delete[] newMusicSamples;
		newMusicSamples = nullptr;
		newMusicSize = 0;
	}

	SDL_LockMutex(musicMutex);

	if (musicSamples) {
		delete[] musicSamples;
	}

	musicSamples = newMusicSamples;
	musicSize = newMusicSize;
	musicCursor = 0;

	SDL_UnlockMutex(musicMutex);

	op_free(opusFile);
}