Dmitry Logov
Dmitry Logov

Reputation: 7

Decoded CELT audio to WAV

I need to extract voice data from a CS:GO game server. The engine gives me fragments of compressed audio stream, which I then decompress via celt library (IVoiceCodec::Decompress) the same way as it is done in this source code: https://github.com/perilouswithadollarsign/cstrike15_src/blob/f82112a2388b841d72cb62ca48ab1846dfcc11c8/engine/audio/private/voice.cpp

The interface doesn't provide any information about decoded data, but from the source code I can guess that it should be single channel 16bit/22050KHz PCM audio

static std::vector<char> buffer;

char decompressedData[MAX_UNCOMPRESSED_VOICE_BUFFER_SIZE];  
const int decompressedBytes = g_pCodec->Decompress(data.data(),
                                                   data.size(),
                                                   decompressedData,
                                                   MAX_UNCOMPRESSED_VOICE_BUFFER_SIZE);

if(decompressedBytes > 0)
    buffer.insert(buffer.end(),
                  decompressedData,
                  decompressedData + decompressedBytes);
        
if(buffer.size() > 50000)
{
    static int fileIndex = 0;
    const std::string fileName("wav/test_" + std::to_string(clientIndex) + "_" + std::to_string(fileIndex) + ".wav");
    const bool result = WriteWaveFile(fileName.c_str(),
                    buffer.data(),
                    buffer.size(),
                    16, // Bits per sample
                    1, // Channels
                    22050 // Samples per sec
                    );
                    
    buffer.clear();
        
    ++fileIndex;
}

The decoder works without errors and outputs as it seams to me correct data (I can make out voices after decoding). I then save the fragments without any changes as single channel 16bit/22050KHz PCM WAV files. Technically I prepend 42 bytes of WAV header to the data. And after that I merge the files. Here is an example of what I get: https://drive.google.com/file/d/16iSC-GXzzl5wwYsvAQN1Hkk93LuSyRYG/view?usp=share_link The pitch sounds correct, but the playback rate is too fast and I can hear noise.

Here is my code for writing wav files (I also got it from the engine source code):

void WriteDWord(FileHandle_t fp, unsigned long val) 
{
    g_pFileSystem->Write(&val, 4, fp);
}

void WriteWord(FileHandle_t fp, unsigned short val) 
{
    g_pFileSystem->Write(&val, 2, fp);
}

bool WriteWaveFile(
    const char *pFilename, 
    const char *pData, 
    int nBytes, 
    int wBitsPerSample, 
    int nChannels, 
    int nSamplesPerSec)
{
    FileHandle_t fp = g_pFileSystem->Open(pFilename, "wb");
    if(!fp)
        return false;

    // Write the RIFF chunk.
    g_pFileSystem->Write("RIFF", 4, fp);
    WriteDWord(fp, 0);
    g_pFileSystem->Write("WAVE", 4, fp);


    // Write the FORMAT chunk.
    g_pFileSystem->Write("fmt ", 4, fp);

    WriteDWord(fp, 0x10);
    WriteWord(fp, WAVE_FORMAT_PCM);
    WriteWord(fp, (unsigned short)nChannels);   
    WriteDWord(fp, (unsigned long)nSamplesPerSec);
    WriteDWord(fp, (unsigned long)((wBitsPerSample / 8) * nChannels * nSamplesPerSec));
    WriteWord(fp, (unsigned short)((wBitsPerSample / 8) * nChannels));
    WriteWord(fp, (unsigned long)wBitsPerSample);

    // Write the DATA chunk.
    g_pFileSystem->Write("data", 4, fp);
    WriteDWord(fp, (unsigned long)nBytes);
    g_pFileSystem->Write(pData, nBytes, fp);

    // Go back and write the length of the riff file.
    unsigned long dwVal = g_pFileSystem->Tell(fp) - 8;
    g_pFileSystem->Seek(fp, 4, FILESYSTEM_SEEK_HEAD);
    WriteDWord(fp, dwVal);

    g_pFileSystem->Close(fp);
    return true;
}

I tried changing the sample rate and the number of bits per sample but still get the wrong result. What can be wrong with the wav files?

Upvotes: 0

Views: 80

Answers (1)

Dmitry Logov
Dmitry Logov

Reputation: 7

It appears IVoiceCodec::Decompress returns number of samples rather then the number of written bytes.

Upvotes: 0

Related Questions