// Allocate sample based on a ModSample's properties. // Returns number of bytes allocated, 0 on failure. size_t ModSample::AllocateSample() //-------------------------------- { // Prevent overflows... if(nLength > SIZE_MAX - 6u || SIZE_MAX / GetBytesPerSample() < nLength + 6u) { return 0; } FreeSample(); size_t sampleSize = (nLength + 6u) * GetBytesPerSample(); if((pSample = CSoundFile::AllocateSample(sampleSize)) == nullptr) { return 0; } else { return sampleSize; } }
int aifFile::Read(ByteBuffer *buffer, int size) { int bufSize = 4096*GetBytesPerSample(); unsigned char buf[4096*4]; if (fd == NULL) return -1; int bytesRead = 0; while (size > 0) { // Are we inside a ssnd chunk? if (status == 2) { // Let's keep reading data! int bytes = size < bufSize ? size : bufSize; if (chunkSize < bytes) bytes = chunkSize; int ret = fcbs->read(buf, 1, bytes, fd); if (ret == 0) { status = 3; break; } else { chunkSize-= ret; if (bitsPerSample == 8) { char *cbuf = (char*)buf; for (int i=0; i<ret; i++) { cbuf[i] = cbuf[i]+128; } } else if (bitsPerSample == 16) { unsigned short *sbuf = (unsigned short *)buf; for (int i=0; i<ret; i+= 2) { sbuf[i>>1] = SWAP16(sbuf[i>>1]); } } else if (bitsPerSample == 24) { for (int i=0; i<ret; i+= 3) { unsigned char tmp; tmp = buf[i+0]; buf[i+0] = buf[i+2]; buf[i+2] = tmp; } } buffer->putBytes(buf, ret); bytesRead+= ret; size-= ret; }
bool cOAL_OggSample::CreateFromFile(const wstring &asFilename) { DEF_FUNC_NAME("cOAL_OggSample::Load()"); FUNC_USES_AL; if(mbStatus==false) return false; Reset(); char *pPCMBuffer; bool bEOF = false; int lOpenResult; int lCurrent_section; long lDataSize = 0; msFilename = asFilename; FILE *fileHandle = OpenFileW(asFilename, L"rb"); // If no file is present, set the error status and return if(!fileHandle) { mbStatus = false; return false; } // If not an Ogg file, set status and exit OggVorbis_File ovFileHandle; if((lOpenResult = ov_open_callbacks(fileHandle, &ovFileHandle, NULL, 0, gCallbacks))<0) { fclose ( fileHandle ); mbStatus = false; return false; } // Get file info vorbis_info *viFileInfo = ov_info ( &ovFileHandle, -1 ); mlChannels = viFileInfo->channels; mFormat = (mlChannels == 2)?AL_FORMAT_STEREO16:AL_FORMAT_MONO16; mlFrequency = viFileInfo->rate; mlSamples = (long) ov_pcm_total ( &ovFileHandle, -1 ); mfTotalTime = ov_time_total( &ovFileHandle, -1 ); // Reserve memory for 'mlChannels' channels of 'mlSamples' * 2 bytes of data each int lSizeInBytes = mlSamples * mlChannels * GetBytesPerSample(); pPCMBuffer = (char *) malloc (lSizeInBytes); memset (pPCMBuffer, 0, lSizeInBytes); // Loop which loads chunks of decoded data into a buffer while(!bEOF) { long lChunkSize = ov_read ( &ovFileHandle , pPCMBuffer + lDataSize , STREAMING_BLOCK_SIZE , SYS_ENDIANNESS , 2, 1, &lCurrent_section ); if(lChunkSize == 0) bEOF = true; // If we get a negative value, then something went wrong. Clean up and set error status. else if(lChunkSize < 0) { free(pPCMBuffer); ov_clear(&ovFileHandle); // ov_clear closes the file handle for us mbStatus = false; return false; } else lDataSize += lChunkSize; } cOAL_Buffer* pBuffer = mvBuffers[0]; if(lDataSize) { // If something went wrong, set error status. Clean up afterwards. mbStatus = pBuffer->Feed((ALvoid*)pPCMBuffer, lDataSize); } free(pPCMBuffer); ov_clear(&ovFileHandle); // ov_clear closes the file handle for us return true; }
uint32_t GetSampleSizeInBytes() const {return length * GetBytesPerSample();}