Exemplo n.º 1
0
void ItemSound(int type, const char* filepath)
{
	int i = g_lastItem;
	CItemType* t = &g_itemType[i];
    
	if(type == DRYSHOT)
		t->dryShotSound.push_back(CSound(filepath));
	else if(type == SHOT)
		t->shotSound.push_back(CSound(filepath));
	else if(type == RELOAD)
		t->reloadSound.push_back(CSound(filepath));
	else if(type == C**K)
		t->cockSound.push_back(CSound(filepath));
	else if(type == DRYFIRE)
		t->dryFireSound.push_back(CSound(filepath));
	else if(type == HIT)
		t->hitSound.push_back(CSound(filepath));
    
    /*
    if(i == ITEM::M1911 && type == ITEMSOUND::RELOAD)
    {
        NSLog(@"Added size = %d", (int)t->reloadSound.size());
        NSLog(@"Added size = %d", (int)t->reloadSound.size());
        NSLog(@"Added size = %d", (int)t->reloadSound.size());
        NSLog(@"Added size = %d", (int)t->reloadSound.size());
        NSLog(@"Added size = %d", (int)t->reloadSound.size());
        NSLog(@"Added size = %d", (int)t->reloadSound.size());
        
        t->reloadSound[0].Play();
    }*/
}
Exemplo n.º 2
0
// must be 44.1k 16bit Stereo PCM Wave
CSound CDSound::CreateSound(char* wavFileName)
{
    int error;
    FILE* filePtr;
    unsigned int count;
    SWaveHeaderType waveFileHeader;
    WAVEFORMATEX waveFormat;
    DSBUFFERDESC bufferDesc;
    HRESULT result;
    IDirectSoundBuffer* tempBuffer;
    IDirectSoundBuffer8* pSecondaryBuffer;
    unsigned char* waveData;
    unsigned char* bufferPtr;
    unsigned long bufferSize;


    // Open the wave file in binary.
    error = fopen_s(&filePtr, wavFileName, "rb");
    assert(error == 0);

    // Read in the wave file header.
    count = fread(&waveFileHeader, sizeof(waveFileHeader), 1, filePtr);
    assert(count == 1);

    // Check that the chunk ID is the RIFF format.
    assert( (waveFileHeader.chunkId[0] == 'R') && 
            (waveFileHeader.chunkId[1] == 'I') && 
            (waveFileHeader.chunkId[2] == 'F') && 
            (waveFileHeader.chunkId[3] == 'F'));

    // Check that the file format is the WAVE format.
    assert( (waveFileHeader.format[0] == 'W') && 
            (waveFileHeader.format[1] == 'A') &&
            (waveFileHeader.format[2] == 'V') &&
            (waveFileHeader.format[3] == 'E'));

    // Check that the sub chunk ID is the fmt format.
    assert( (waveFileHeader.subChunkId[0] == 'f') && 
            (waveFileHeader.subChunkId[1] == 'm') &&
            (waveFileHeader.subChunkId[2] == 't') && 
            (waveFileHeader.subChunkId[3] == ' '));

    // Check that the audio format is WAVE_FORMAT_PCM.
    assert(waveFileHeader.audioFormat == WAVE_FORMAT_PCM);

    // Check that the wave file was recorded in stereo format.
    assert(waveFileHeader.numChannels == 2);

    // Check that the wave file was recorded at a sample rate of 44.1 KHz.
    assert(waveFileHeader.sampleRate == 44100);

    // Ensure that the wave file was recorded in 16 bit format.
    assert(waveFileHeader.bitsPerSample == 16);

    // Check for the data chunk header.
    assert( (waveFileHeader.dataChunkId[0] == 'd') && 
            (waveFileHeader.dataChunkId[1] == 'a') &&
            (waveFileHeader.dataChunkId[2] == 't') &&
            (waveFileHeader.dataChunkId[3] == 'a'));

    // Set the wave format of secondary buffer that this wave file will be loaded onto.
    waveFormat.wFormatTag = WAVE_FORMAT_PCM;
    waveFormat.nSamplesPerSec = 44100;
    waveFormat.wBitsPerSample = 16;
    waveFormat.nChannels = 2;
    waveFormat.nBlockAlign = (waveFormat.wBitsPerSample / 8) * waveFormat.nChannels;
    waveFormat.nAvgBytesPerSec = waveFormat.nSamplesPerSec * waveFormat.nBlockAlign;
    waveFormat.cbSize = 0;

    // Set the buffer description of the secondary sound buffer that the wave file will be loaded onto.
    bufferDesc.dwSize = sizeof(DSBUFFERDESC);
    bufferDesc.dwFlags = DSBCAPS_CTRLVOLUME;
    bufferDesc.dwBufferBytes = waveFileHeader.dataSize;
    bufferDesc.dwReserved = 0;
    bufferDesc.lpwfxFormat = &waveFormat;
    bufferDesc.guid3DAlgorithm = GUID_NULL;

    // Create a temporary sound buffer with the specific buffer settings.
    result = m_pDirectSound->CreateSoundBuffer(&bufferDesc, &tempBuffer, NULL);
    assert(!FAILED(result));

    // Test the buffer format against the direct sound 8 interface and create the secondary buffer.
    result = tempBuffer->QueryInterface(IID_IDirectSoundBuffer8, (void**)&pSecondaryBuffer);
    assert(!FAILED(result));

    // Release the temporary buffer.
    tempBuffer->Release();
    tempBuffer = 0;

    // Move to the beginning of the wave data which starts at the end of the data chunk header.
    fseek(filePtr, sizeof(SWaveHeaderType), SEEK_SET);

    // Create a temporary buffer to hold the wave file data.
    waveData = new unsigned char[waveFileHeader.dataSize];
    assert(waveData);

    // Read in the wave file data into the newly created buffer.
    count = fread(waveData, 1, waveFileHeader.dataSize, filePtr);
    assert(count == waveFileHeader.dataSize);

    // Close the file once done reading.
    error = fclose(filePtr);
    assert(error == 0);

    // Lock the secondary buffer to write wave data into it.
    result = pSecondaryBuffer->Lock(0, waveFileHeader.dataSize, (void**)&bufferPtr, (DWORD*)&bufferSize, NULL, 0, 0);
    assert(!FAILED(result));

    // Copy the wave data into the buffer.
    memcpy(bufferPtr, waveData, waveFileHeader.dataSize);

    // Unlock the secondary buffer after the data has been written to it.
    result = pSecondaryBuffer->Unlock((void*)bufferPtr, bufferSize, NULL, 0);
    assert(!FAILED(result));
    
    // Release the wave data since it was copied into the secondary buffer.
    delete [] waveData;

    return CSound(pSecondaryBuffer);
}