Пример #1
0
int CacheList::reg(char const *filename, char const *name, int type,
		int rm_dups) {
	int fn = crc_manager.get_filenumber(filename);
	int offset = 0;

	if (type == SPEC_EXTERN_SFX) {
		// If an extern sound effect then just make sure it's there. If sound
		// is disabled, ignore the load error, just pretend it's all OK.
		bFILE *check = open_file(filename, "rb");
		if (!check->open_failure()) {
			char buf[4];
			check->read(buf, 4);
			if (memcmp(buf, "RIFF", 4)) {
				printf("File %s is not a WAV file\n", filename);
				exit(0);
			}
		} else if (sound_avail) {
			printf("Unable to open file '%s' for reading\n", filename);
			exit(0);
		}
		delete check;
	} else {
		// If a classic spec item, look for it in the archive.
		spec_directory *sd = sd_cache.get_spec_directory(filename);

		if (!sd) {
			printf("Unable to open file %s for item %s\n", filename, name);
			exit(0);
		}

		spec_entry *se = NULL;
		if (type != -1)
			se = sd->find(name, type);
		if (!se)
			se = sd->find(name);
		if (!se) {
			printf("No such item %s in file %s\n", name, filename);
			exit(0);
		}

		if (type >= 0 && type != se->type
				&& ((type != SPEC_CHARACTER2 && type != SPEC_CHARACTER)
						|| (se->type != SPEC_CHARACTER
								&& se->type != SPEC_CHARACTER2))) {
			printf("Item %s of file %s should be type %s\n", name, filename,
					spec_types[type]);
			exit(0);
		}

		type = se->type;
		offset = se->offset;
	}

	// Check whether there is another entry pointing to the same
	// file and offset, and return it as a shortcut.
	if (rm_dups) {
		for (int i = 0; i < total; i++)
			if (list[i].file_number == fn && list[i].offset == offset)
				return i;
	}

	int id = AllocId();

	CHECK(id < total && list[id].file_number < 0);

	list[id].file_number = fn;
	list[id].last_access = -1;
	list[id].data = NULL;
	list[id].offset = offset;
	list[id].type = type;

	return id;
}
Пример #2
0
int CacheList::reg(char const *filename, char const *name, int type, int rm_dups)
{
    int fn = crc_manager.get_filenumber(filename);
    int offset = 0;

    if (type == SPEC_EXTERN_SFX)
    {
    	// Missing sound effect is not a critical error, so we don't
    	// quit when error happens.
        bFILE *check = open_file(filename, "rb");
        if (!check->open_failure())
        {
        	printf("Unable to open sfx file '%s' for reading, ignoring.\n", filename);
        }
        delete check;
    }
    else
    {
        // If a classic spec item, look for it in the archive.
        spec_directory *sd = sd_cache.get_spec_directory(filename);

        if (!sd)
        {
            printf("Unable to open file %s for item %s\n", filename, name);
            assert(false);
            exit(0);
        }

        spec_entry *se = NULL;
        if (type != -1)
            se = sd->find(name, type);
        if (!se)
            se = sd->find(name);
        if (!se)
        {
            printf("No such item %s in file %s\n", name, filename);
            assert(false);
            exit(0);
        }

        if (type >= 0 && type != se->type &&
             ((type != SPEC_CHARACTER2 && type != SPEC_CHARACTER)
               || (se->type != SPEC_CHARACTER && se->type != SPEC_CHARACTER2)))
        {
            printf("Item %s of file %s should be type %s\n",
                   name, filename, spec_types[type]);
            assert(false);
            exit(0);
        }

        type = se->type;
        offset = se->offset;
    }

    // Check whether there is another entry pointing to the same
    // file and offset, and return it as a shortcut.
    if (rm_dups)
    {
        for (int i = 0; i < total; i++)
            if (list[i].file_number == fn && list[i].offset == offset)
                return i;
    }

    int id = AllocId();

    CHECK(id < total && list[id].file_number < 0);

    list[id].file_number = fn;
    list[id].last_access = -1;
    list[id].data = NULL;
    list[id].offset = offset;
    list[id].type = type;

    return id;
}
Пример #3
0
int CSound::LoadWV(const char *pFilename)
{
	CSample *pSample;
	int SampleId = -1;
	char aError[100];
	WavpackContext *pContext;
	
	// don't waste memory on sound when we are stress testing
	if(g_Config.m_DbgStress)
		return -1;
		
	// no need to load sound when we are running with no sound
	if(!m_SoundEnabled)
		return 1;
		
	if(!m_pStorage)
		return -1;

	ms_File = m_pStorage->OpenFile(pFilename, IOFLAG_READ); // TODO: use system.h stuff for this
	if(!ms_File)
	{
		dbg_msg("sound/wv", "failed to open %s", pFilename);
		return -1;
	}

	SampleId = AllocId();
	if(SampleId < 0)
		return -1;
	pSample = &m_aSamples[SampleId];

	pContext = WavpackOpenFileInput(ReadData, aError);
	if (pContext)
	{
		int m_aSamples = WavpackGetNumSamples(pContext);
		int BitsPerSample = WavpackGetBitsPerSample(pContext);
		unsigned int SampleRate = WavpackGetSampleRate(pContext);
		int m_aChannels = WavpackGetNumChannels(pContext);
		int *pData;
		int *pSrc;
		short *pDst;
		int i;

		pSample->m_Channels = m_aChannels;
		pSample->m_Rate = SampleRate;

		if(pSample->m_Channels > 2)
		{
			dbg_msg("sound/wv", "file is not mono or stereo. filename='%s'", pFilename);
			return -1;
		}

		/*
		if(snd->rate != 44100)
		{
			dbg_msg("sound/wv", "file is %d Hz, not 44100 Hz. filename='%s'", snd->rate, filename);
			return -1;
		}*/
		
		if(BitsPerSample != 16)
		{
			dbg_msg("sound/wv", "bps is %d, not 16, filname='%s'", BitsPerSample, pFilename);
			return -1;
		}

		pData = (int *)mem_alloc(4*m_aSamples*m_aChannels, 1);
		WavpackUnpackSamples(pContext, pData, m_aSamples); // TODO: check return value
		pSrc = pData;
		
		pSample->m_pData = (short *)mem_alloc(2*m_aSamples*m_aChannels, 1);
		pDst = pSample->m_pData;

		for (i = 0; i < m_aSamples*m_aChannels; i++)
			*pDst++ = (short)*pSrc++;

		mem_free(pData);

		pSample->m_NumFrames = m_aSamples;
		pSample->m_LoopStart = -1;
		pSample->m_LoopEnd = -1;
	}
	else
	{
		dbg_msg("sound/wv", "failed to open %s: %s", pFilename, aError);
	}

	io_close(ms_File);
	ms_File = NULL;

	if(g_Config.m_Debug)
		dbg_msg("sound/wv", "loaded %s", pFilename);

	RateConvert(SampleId);
	return SampleId;
}