Exemplo n.º 1
0
    bool NSFSoundStream::open(const std::string& fileName) {
        sf::Lock lock(mutex);

        if(!FileSystem::exists(fileName)) {
            return false;
        }

        int length = 0;
        auto fs = FileSystem::openFileRead(fileName);

        fs->seekg (0, std::ios::end);
        length = static_cast<int>(fs->tellg());
        fs->seekg (0, std::ios::beg);

        std::unique_ptr<char[]> nsfFileBuffer(new char[length]);

        fs->read(nsfFileBuffer.get(), length);

        // To honor the contract of returning false on failure,
        // catch these exceptions and return false instead? Good/bad?
        try {
            gme_type_t file_type = gme_identify_extension(fileName.c_str());

            if(!file_type) {
                return false;
            }

            for(std::size_t i = 0; i < samplerCount; ++i) {
                auto sampleEmu = std::shared_ptr<Music_Emu>(file_type->new_emu());

                if(!sampleEmu) {
                    return false;
                }

                // Must set sample rate before loading data
                handleError(sampleEmu->set_sample_rate(SAMPLE_RATE));
                handleError(gme_load_data(sampleEmu.get(), nsfFileBuffer.get(), length));

                sampleEmu->start_track(-1);
                sampleEmu->ignore_silence(false);

                auto sampleBuffer = std::make_shared<std::vector<short>>(masterBufferSize);
                std::fill(std::begin(*sampleBuffer), std::end(*sampleBuffer), 0);
                availableSamplers.push(std::make_pair(sampleEmu, sampleBuffer));
                // sampleEmus.push_back(std::move(sampleEmu));
            }

            trackInfo.reset(new track_info_t());
        } catch(std::runtime_error& ex) {
            HIKARI_LOG(debug) << ex.what();
            return false;
        }

        initialize(2, SAMPLE_RATE);
        //setCurrentTrack(0);

        return true;
    }
Exemplo n.º 2
0
MusInfo *GME_OpenSong(FILE *file, BYTE *musiccache, int len, const char *fmt)
{
	gme_type_t type;
	gme_err_t err;
	BYTE *song;
	Music_Emu *emu;
	int sample_rate;
	
	type = gme_identify_extension(fmt);
	if (type == NULL)
	{
		return NULL;
	}
	sample_rate = (int)GSnd->GetOutputRate();
	emu = gme_new_emu(type, sample_rate);
	if (emu == NULL)
	{
		return NULL;
	}
	if (musiccache != NULL)
	{
		song = musiccache;
	}
	else
	{
		song = new BYTE[len];
		if (fread(song, 1, len, file) != (size_t)len)
		{
			delete[] song;
			gme_delete(emu);
			return NULL;
		}
	}
	err = gme_load_data(emu, song, len);
	if (song != musiccache)
	{
		delete[] song;
	}
	if (err != NULL)
	{
		Printf("Failed loading song: %s\n", err);
		gme_delete(emu);
		return NULL;
	}
	return new GMESong(emu, sample_rate);
}
Exemplo n.º 3
0
Arquivo: gme.cpp Projeto: kode54/Cog
gme_err_t gme_open_data( void const* data, long size, Music_Emu** out, int sample_rate )
{
	require( (data || !size) && out );
	*out = NULL;
	
	gme_type_t file_type = 0;
	if ( size >= 4 )
		file_type = gme_identify_extension( gme_identify_header( data ) );
	if ( !file_type )
		return blargg_err_file_type;
	
	Music_Emu* emu = gme_new_emu( file_type, sample_rate );
	CHECK_ALLOC( emu );
	
	gme_err_t err = gme_load_data( emu, data, size );
	
	if ( err )
		delete emu;
	else
		*out = emu;
	
	return err;
}