158 #if defined(_MSC_VER) && _MSC_VER < 1600
181 #define DRWAV_FALSE 0
188 #define DR_WAVE_FORMAT_PCM 0x1
189 #define DR_WAVE_FORMAT_ADPCM 0x2
190 #define DR_WAVE_FORMAT_IEEE_FLOAT 0x3
191 #define DR_WAVE_FORMAT_ALAW 0x6
192 #define DR_WAVE_FORMAT_MULAW 0x7
193 #define DR_WAVE_FORMAT_DVI_ADPCM 0x11
194 #define DR_WAVE_FORMAT_EXTENSIBLE 0xFFFE
197 #ifndef DRWAV_MAX_SMPL_LOOPS
198 #define DRWAV_MAX_SMPL_LOOPS 1
202 #define DRWAV_SEQUENTIAL 0x00000001
246 typedef size_t (*
drwav_read_proc)(
void* pUserData,
void* pBufferOut,
size_t bytesToRead);
259 typedef size_t (*
drwav_write_proc)(
void* pUserData,
const void* pData,
size_t bytesToWrite);
653 #ifndef DR_WAV_NO_CONVERSION_API
752 #ifndef DR_WAV_NO_STDIO
840 #ifndef DR_WAV_NO_CONVERSION_API
845 #ifndef DR_WAV_NO_STDIO
859 void drwav_free(
void* pDataReturnedByOpenAndRead);
869 #ifndef DR_WAV_NO_CONVERSION_API
873 #ifndef DR_WAV_NO_STDIO
897 #ifdef DR_WAV_IMPLEMENTATION
902 #ifndef DR_WAV_NO_STDIO
909 #define DRWAV_ASSERT(expression) assert(expression)
912 #define DRWAV_MALLOC(sz) malloc((sz))
914 #ifndef DRWAV_REALLOC
915 #define DRWAV_REALLOC(p, sz) realloc((p), (sz))
918 #define DRWAV_FREE(p) free((p))
920 #ifndef DRWAV_COPY_MEMORY
921 #define DRWAV_COPY_MEMORY(dst, src, sz) memcpy((dst), (src), (sz))
923 #ifndef DRWAV_ZERO_MEMORY
924 #define DRWAV_ZERO_MEMORY(p, sz) memset((p), 0, (sz))
927 #define drwav_countof(x) (sizeof(x) / sizeof(x[0]))
928 #define drwav_align(x, a) ((((x) + (a) - 1) / (a)) * (a))
929 #define drwav_min(a, b) (((a) < (b)) ? (a) : (b))
930 #define drwav_max(a, b) (((a) > (b)) ? (a) : (b))
931 #define drwav_clamp(x, lo, hi) (drwav_max((lo), drwav_min((hi), (x))))
933 #define drwav_assert DRWAV_ASSERT
934 #define drwav_copy_memory DRWAV_COPY_MEMORY
935 #define drwav_zero_memory DRWAV_ZERO_MEMORY
938 #define DRWAV_SUCCESS 0
939 #define DRWAV_ERROR -1
940 #define DRWAV_INVALID_ARGS -2
941 #define DRWAV_INVALID_OPERATION -3
942 #define DRWAV_INVALID_FILE -100
943 #define DRWAV_EOF -101
945 #define DRWAV_MAX_SIMD_VECTOR_SIZE 64
948 #define DRWAV_INLINE __forceinline
951 #define DRWAV_INLINE __inline__ __attribute__((always_inline))
957 #if defined(SIZE_MAX)
958 #define DRWAV_SIZE_MAX SIZE_MAX
960 #if defined(_WIN64) || defined(_LP64) || defined(__LP64__)
961 #define DRWAV_SIZE_MAX ((drwav_uint64)0xFFFFFFFFFFFFFFFF)
963 #define DRWAV_SIZE_MAX 0xFFFFFFFF
967 static const drwav_uint8 drwavGUID_W64_RIFF[16] = {0x72,0x69,0x66,0x66, 0x2E,0x91, 0xCF,0x11, 0xA5,0xD6, 0x28,0xDB,0x04,0xC1,0x00,0x00};
968 static const drwav_uint8 drwavGUID_W64_WAVE[16] = {0x77,0x61,0x76,0x65, 0xF3,0xAC, 0xD3,0x11, 0x8C,0xD1, 0x00,0xC0,0x4F,0x8E,0xDB,0x8A};
969 static const drwav_uint8 drwavGUID_W64_JUNK[16] = {0x6A,0x75,0x6E,0x6B, 0xF3,0xAC, 0xD3,0x11, 0x8C,0xD1, 0x00,0xC0,0x4F,0x8E,0xDB,0x8A};
970 static const drwav_uint8 drwavGUID_W64_FMT [16] = {0x66,0x6D,0x74,0x20, 0xF3,0xAC, 0xD3,0x11, 0x8C,0xD1, 0x00,0xC0,0x4F,0x8E,0xDB,0x8A};
971 static const drwav_uint8 drwavGUID_W64_FACT[16] = {0x66,0x61,0x63,0x74, 0xF3,0xAC, 0xD3,0x11, 0x8C,0xD1, 0x00,0xC0,0x4F,0x8E,0xDB,0x8A};
972 static const drwav_uint8 drwavGUID_W64_DATA[16] = {0x64,0x61,0x74,0x61, 0xF3,0xAC, 0xD3,0x11, 0x8C,0xD1, 0x00,0xC0,0x4F,0x8E,0xDB,0x8A};
973 static const drwav_uint8 drwavGUID_W64_SMPL[16] = {0x73,0x6D,0x70,0x6C, 0xF3,0xAC, 0xD3,0x11, 0x8C,0xD1, 0x00,0xC0,0x4F,0x8E,0xDB,0x8A};
987 static DRWAV_INLINE
drwav_bool32 drwav__fourcc_equal(
const unsigned char* a,
const char* b)
998 static DRWAV_INLINE
int drwav__is_little_endian()
1001 return (*(
char*)&n) == 1;
1004 static DRWAV_INLINE
unsigned short drwav__bytes_to_u16(
const unsigned char* data)
1006 return (data[0] << 0) | (data[1] << 8);
1009 static DRWAV_INLINE
short drwav__bytes_to_s16(
const unsigned char* data)
1011 return (
short)drwav__bytes_to_u16(data);
1014 static DRWAV_INLINE
unsigned int drwav__bytes_to_u32(
const unsigned char* data)
1016 return (data[0] << 0) | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
1019 static DRWAV_INLINE
drwav_uint64 drwav__bytes_to_u64(
const unsigned char* data)
1026 static DRWAV_INLINE
void drwav__bytes_to_guid(
const unsigned char* data,
drwav_uint8* guid)
1029 for (
i = 0;
i < 16; ++
i) {
1050 unsigned char sizeInBytes[4];
1052 if (onRead(pUserData, pHeaderOut->
id.
fourcc, 4) != 4) {
1056 if (onRead(pUserData, sizeInBytes, 4) != 4) {
1057 return DRWAV_INVALID_FILE;
1060 pHeaderOut->
sizeInBytes = drwav__bytes_to_u32(sizeInBytes);
1062 *pRunningBytesReadOut += 8;
1064 unsigned char sizeInBytes[8];
1066 if (onRead(pUserData, pHeaderOut->
id.
guid, 16) != 16) {
1070 if (onRead(pUserData, sizeInBytes, 8) != 8) {
1071 return DRWAV_INVALID_FILE;
1074 pHeaderOut->
sizeInBytes = drwav__bytes_to_u64(sizeInBytes) - 24;
1076 *pRunningBytesReadOut += 24;
1079 return DRWAV_SUCCESS;
1085 while (bytesRemainingToSeek > 0) {
1086 if (bytesRemainingToSeek > 0x7FFFFFFF) {
1090 bytesRemainingToSeek -= 0x7FFFFFFF;
1095 bytesRemainingToSeek = 0;
1104 if (offset <= 0x7FFFFFFF) {
1112 offset -= 0x7FFFFFFF;
1115 if (offset <= 0x7FFFFFFF) {
1122 offset -= 0x7FFFFFFF;
1133 unsigned char fmt[16];
1135 if (drwav__read_chunk_header(onRead, pUserData, container, pRunningBytesReadOut, &header) != DRWAV_SUCCESS) {
1148 if (drwav__read_chunk_header(onRead, pUserData, container, pRunningBytesReadOut, &header) != DRWAV_SUCCESS) {
1156 if (!drwav__fourcc_equal(header.
id.
fourcc,
"fmt ")) {
1160 if (!drwav__guid_equal(header.
id.
guid, drwavGUID_W64_FMT)) {
1166 if (onRead(pUserData, fmt,
sizeof(fmt)) !=
sizeof(fmt)) {
1169 *pRunningBytesReadOut +=
sizeof(fmt);
1171 fmtOut->
formatTag = drwav__bytes_to_u16(fmt + 0);
1172 fmtOut->
channels = drwav__bytes_to_u16(fmt + 2);
1173 fmtOut->
sampleRate = drwav__bytes_to_u32(fmt + 4);
1175 fmtOut->
blockAlign = drwav__bytes_to_u16(fmt + 12);
1184 unsigned char fmt_cbSize[2];
1185 int bytesReadSoFar = 0;
1187 if (onRead(pUserData, fmt_cbSize,
sizeof(fmt_cbSize)) !=
sizeof(fmt_cbSize)) {
1190 *pRunningBytesReadOut +=
sizeof(fmt_cbSize);
1192 bytesReadSoFar = 18;
1194 fmtOut->
extendedSize = drwav__bytes_to_u16(fmt_cbSize);
1204 unsigned char fmtext[22];
1210 fmtOut->
channelMask = drwav__bytes_to_u32(fmtext + 2);
1211 drwav__bytes_to_guid(fmtext + 6, fmtOut->
subFormat);
1226 *pRunningBytesReadOut += (header.
sizeInBytes - bytesReadSoFar);
1240 #ifndef DR_WAV_NO_STDIO
1241 FILE* drwav_fopen(
const char* filePath,
const char* openMode)
1244 #if defined(_MSC_VER) && _MSC_VER >= 1400
1245 if (fopen_s(&pFile, filePath, openMode) != 0) {
1249 pFile = fopen(filePath, openMode);
1250 if (pFile ==
NULL) {
1258 static size_t drwav__on_read_stdio(
void* pUserData,
void* pBufferOut,
size_t bytesToRead)
1260 return fread(pBufferOut, 1, bytesToRead, (FILE*)pUserData);
1263 static size_t drwav__on_write_stdio(
void* pUserData,
const void* pData,
size_t bytesToWrite)
1265 return fwrite(pData, 1, bytesToWrite, (FILE*)pUserData);
1280 FILE* pFile = drwav_fopen(filename,
"rb");
1281 if (pFile ==
NULL) {
1285 return drwav_init_ex(pWav, drwav__on_read_stdio, drwav__on_seek_stdio, onChunk, (
void*)pFile, pChunkUserData, flags);
1291 FILE* pFile = drwav_fopen(filename,
"wb");
1292 if (pFile ==
NULL) {
1296 return drwav_init_write__internal(pWav, pFormat, totalSampleCount, isSequential, drwav__on_write_stdio, drwav__on_seek_stdio, (
void*)pFile);
1301 return drwav_init_file_write__internal(pWav, filename, pFormat, 0,
DRWAV_FALSE);
1306 return drwav_init_file_write__internal(pWav, filename, pFormat, totalSampleCount,
DRWAV_TRUE);
1319 pFile = drwav_fopen(filename,
"rb");
1320 if (pFile ==
NULL) {
1324 pWav =
drwav_open_ex(drwav__on_read_stdio, drwav__on_seek_stdio, onChunk, (
void*)pFile, pChunkUserData, flags);
1339 pFile = drwav_fopen(filename,
"wb");
1340 if (pFile ==
NULL) {
1344 pWav = drwav_open_write__internal(pFormat, totalSampleCount, isSequential, drwav__on_write_stdio, drwav__on_seek_stdio, (
void*)pFile);
1355 return drwav_open_file_write__internal(filename, pFormat, 0,
DRWAV_FALSE);
1360 return drwav_open_file_write__internal(filename, pFormat, totalSampleCount,
DRWAV_TRUE);
1365 static size_t drwav__on_read_memory(
void* pUserData,
void* pBufferOut,
size_t bytesToRead)
1368 size_t bytesRemaining;
1370 drwav_assert(memory !=
NULL);
1374 if (bytesToRead > bytesRemaining) {
1375 bytesToRead = bytesRemaining;
1378 if (bytesToRead > 0) {
1389 drwav_assert(memory !=
NULL);
1415 static size_t drwav__on_write_memory(
void* pUserData,
const void* pDataIn,
size_t bytesToWrite)
1418 size_t bytesRemaining;
1420 drwav_assert(memory !=
NULL);
1424 if (bytesRemaining < bytesToWrite) {
1434 pNewData = DRWAV_REALLOC(*memory->
ppData, newDataCapacity);
1435 if (pNewData ==
NULL) {
1439 *memory->
ppData = pNewData;
1452 return bytesToWrite;
1458 drwav_assert(memory !=
NULL);
1493 if (data ==
NULL || dataSize == 0) {
1497 drwav_zero_memory(&memoryStream,
sizeof(memoryStream));
1498 memoryStream.
data = (
const unsigned char*)data;
1502 if (!
drwav_init_ex(pWav, drwav__on_read_memory, drwav__on_seek_memory, onChunk, (
void*)&memoryStream, pChunkUserData, flags)) {
1516 if (ppData ==
NULL) {
1523 drwav_zero_memory(&memoryStreamWrite,
sizeof(memoryStreamWrite));
1524 memoryStreamWrite.
ppData = ppData;
1525 memoryStreamWrite.
pDataSize = pDataSize;
1530 if (!drwav_init_write__internal(pWav, pFormat, totalSampleCount, isSequential, drwav__on_write_memory, drwav__on_seek_memory_write, (
void*)&memoryStreamWrite)) {
1541 return drwav_init_memory_write__internal(pWav, ppData, pDataSize, pFormat, 0,
DRWAV_FALSE);
1546 return drwav_init_memory_write__internal(pWav, ppData, pDataSize, pFormat, totalSampleCount,
DRWAV_TRUE);
1560 if (data ==
NULL || dataSize == 0) {
1564 drwav_zero_memory(&memoryStream,
sizeof(memoryStream));
1565 memoryStream.
data = (
const unsigned char*)data;
1569 pWav =
drwav_open_ex(drwav__on_read_memory, drwav__on_seek_memory, onChunk, (
void*)&memoryStream, pChunkUserData, flags);
1585 if (ppData ==
NULL) {
1592 drwav_zero_memory(&memoryStreamWrite,
sizeof(memoryStreamWrite));
1593 memoryStreamWrite.
ppData = ppData;
1594 memoryStreamWrite.
pDataSize = pDataSize;
1599 pWav = drwav_open_write__internal(pFormat, totalSampleCount, isSequential, drwav__on_write_memory, drwav__on_seek_memory_write, (
void*)&memoryStreamWrite);
1611 return drwav_open_memory_write__internal(ppData, pDataSize, pFormat, 0,
DRWAV_FALSE);
1616 return drwav_open_memory_write__internal(ppData, pDataSize, pFormat, totalSampleCount,
DRWAV_TRUE);
1624 drwav_assert(onRead !=
NULL);
1625 drwav_assert(pCursor !=
NULL);
1627 bytesRead = onRead(pUserData, pBufferOut, bytesToRead);
1628 *pCursor += bytesRead;
1634 drwav_assert(onSeek !=
NULL);
1635 drwav_assert(pCursor !=
NULL);
1637 if (!onSeek(pUserData, offset, origin)) {
1658 if (bytesPerSample == 0 || (pWav->
bitsPerSample & 0x7) != 0) {
1662 return bytesPerSample;
1673 if (bytesPerFrame == 0 || (bitsPerFrame & 0x7) != 0) {
1677 return bytesPerFrame;
1690 unsigned char riff[4];
1692 unsigned short translatedFormatTag;
1698 if (onRead ==
NULL || onSeek ==
NULL) {
1705 drwav_zero_memory(pWav,
sizeof(*pWav));
1711 if (drwav__on_read(onRead, pReadSeekUserData, riff,
sizeof(riff), &cursor) !=
sizeof(riff)) {
1719 if (drwav__fourcc_equal(riff,
"RIFF")) {
1721 }
else if (drwav__fourcc_equal(riff,
"riff")) {
1728 if (drwav__on_read(onRead, pReadSeekUserData, riff2,
sizeof(riff2), &cursor) !=
sizeof(riff2)) {
1732 for (
i = 0;
i < 12; ++
i) {
1733 if (riff2[
i] != drwavGUID_W64_RIFF[
i+4]) {
1743 unsigned char chunkSizeBytes[4];
1744 unsigned char wave[4];
1747 if (drwav__on_read(onRead, pReadSeekUserData, chunkSizeBytes,
sizeof(chunkSizeBytes), &cursor) !=
sizeof(chunkSizeBytes)) {
1751 if (drwav__bytes_to_u32(chunkSizeBytes) < 36) {
1755 if (drwav__on_read(onRead, pReadSeekUserData, wave,
sizeof(wave), &cursor) !=
sizeof(wave)) {
1759 if (!drwav__fourcc_equal(wave,
"WAVE")) {
1763 unsigned char chunkSizeBytes[8];
1767 if (drwav__on_read(onRead, pReadSeekUserData, chunkSizeBytes,
sizeof(chunkSizeBytes), &cursor) !=
sizeof(chunkSizeBytes)) {
1771 if (drwav__bytes_to_u64(chunkSizeBytes) < 80) {
1775 if (drwav__on_read(onRead, pReadSeekUserData, wave,
sizeof(wave), &cursor) !=
sizeof(wave)) {
1779 if (!drwav__guid_equal(wave, drwavGUID_W64_WAVE)) {
1786 if (!drwav__read_fmt(onRead, onSeek, pReadSeekUserData, pWav->
container, &cursor, &fmt)) {
1799 translatedFormatTag = drwav__bytes_to_u16(fmt.
subFormat + 0);
1804 sampleCountFromFactChunk = 0;
1821 drwav_result result = drwav__read_chunk_header(onRead, pReadSeekUserData, pWav->
container, &cursor, &header);
1822 if (result != DRWAV_SUCCESS) {
1823 if (!foundDataChunk) {
1831 if (!sequential && onChunk !=
NULL) {
1832 drwav_uint64 callbackBytesRead = onChunk(pChunkUserData, onRead, onSeek, pReadSeekUserData, &header);
1838 if (callbackBytesRead > 0) {
1839 if (!drwav__seek_from_start(onSeek, cursor, pReadSeekUserData)) {
1846 if (!foundDataChunk) {
1852 if (drwav__fourcc_equal(header.
id.
fourcc,
"data")) {
1854 dataChunkSize = chunkSize;
1857 if (drwav__guid_equal(header.
id.
guid, drwavGUID_W64_DATA)) {
1859 dataChunkSize = chunkSize;
1867 if (foundDataChunk && sequential) {
1873 if (drwav__fourcc_equal(header.
id.
fourcc,
"fact")) {
1875 if (drwav__on_read(onRead, pReadSeekUserData, &sampleCount, 4, &cursor) != 4) {
1880 if (!foundDataChunk) {
1889 sampleCountFromFactChunk = sampleCount;
1891 sampleCountFromFactChunk = 0;
1895 if (drwav__guid_equal(header.
id.
guid, drwavGUID_W64_FACT)) {
1896 if (drwav__on_read(onRead, pReadSeekUserData, &sampleCountFromFactChunk, 8, &cursor) != 8) {
1901 if (!foundDataChunk) {
1909 if (drwav__fourcc_equal(header.
id.
fourcc,
"smpl")) {
1910 unsigned char smplHeaderData[36];
1911 if (chunkSize >=
sizeof(smplHeaderData)) {
1912 drwav_uint64 bytesJustRead = drwav__on_read(onRead, pReadSeekUserData, smplHeaderData,
sizeof(smplHeaderData), &cursor);
1913 chunkSize -= bytesJustRead;
1915 if (bytesJustRead ==
sizeof(smplHeaderData)) {
1919 pWav->
smpl.
product = drwav__bytes_to_u32(smplHeaderData+4);
1929 unsigned char smplLoopData[24];
1930 bytesJustRead = drwav__on_read(onRead, pReadSeekUserData, smplLoopData,
sizeof(smplLoopData), &cursor);
1931 chunkSize -= bytesJustRead;
1933 if (bytesJustRead ==
sizeof(smplLoopData)) {
1935 pWav->
smpl.
loops[iLoop].
type = drwav__bytes_to_u32(smplLoopData+4);
1936 pWav->
smpl.
loops[iLoop].
start = drwav__bytes_to_u32(smplLoopData+8);
1937 pWav->
smpl.
loops[iLoop].
end = drwav__bytes_to_u32(smplLoopData+12);
1950 if (drwav__guid_equal(header.
id.
guid, drwavGUID_W64_SMPL)) {
1960 if (!drwav__seek_forward(onSeek, chunkSize, pReadSeekUserData)) {
1963 cursor += chunkSize;
1965 if (!foundDataChunk) {
1971 if (!foundDataChunk) {
1977 if (!drwav__seek_from_start(onSeek, pWav->
dataChunkDataPos, pReadSeekUserData)) {
1994 if (sampleCountFromFactChunk != 0) {
2016 #ifdef DR_WAV_LIBSNDFILE_COMPAT
2043 if (dataChunkSize <= (0xFFFFFFFFUL - 36)) {
2052 if (dataChunkSize <= 0xFFFFFFFFUL) {
2055 return 0xFFFFFFFFUL;
2061 return 80 + 24 + dataChunkSize;
2066 return 24 + dataChunkSize;
2072 size_t runningPos = 0;
2080 if (onWrite ==
NULL) {
2084 if (!isSequential && onSeek ==
NULL) {
2098 drwav_zero_memory(pWav,
sizeof(*pWav));
2124 if (initialDataChunkSize > (0xFFFFFFFFUL - 36)) {
2136 runningPos += pWav->
onWrite(pUserData,
"RIFF", 4);
2137 runningPos += pWav->
onWrite(pUserData, &chunkSizeRIFF, 4);
2138 runningPos += pWav->
onWrite(pUserData,
"WAVE", 4);
2140 drwav_uint64 chunkSizeRIFF = 80 + 24 + initialDataChunkSize;
2141 runningPos += pWav->
onWrite(pUserData, drwavGUID_W64_RIFF, 16);
2142 runningPos += pWav->
onWrite(pUserData, &chunkSizeRIFF, 8);
2143 runningPos += pWav->
onWrite(pUserData, drwavGUID_W64_WAVE, 16);
2149 runningPos += pWav->
onWrite(pUserData,
"fmt ", 4);
2150 runningPos += pWav->
onWrite(pUserData, &chunkSizeFMT, 4);
2153 runningPos += pWav->
onWrite(pUserData, drwavGUID_W64_FMT, 16);
2154 runningPos += pWav->
onWrite(pUserData, &chunkSizeFMT, 8);
2169 runningPos += pWav->
onWrite(pUserData,
"data", 4);
2170 runningPos += pWav->
onWrite(pUserData, &chunkSizeDATA, 4);
2172 drwav_uint64 chunkSizeDATA = 24 + initialDataChunkSize;
2173 runningPos += pWav->
onWrite(pUserData, drwavGUID_W64_DATA, 16);
2174 runningPos += pWav->
onWrite(pUserData, &chunkSizeDATA, 8);
2180 if (runningPos != 20 + chunkSizeFMT + 8) {
2184 if (runningPos != 40 + chunkSizeFMT + 24) {
2204 return drwav_init_write__internal(pWav, pFormat, 0,
DRWAV_FALSE, onWrite, onSeek, pUserData);
2209 return drwav_init_write__internal(pWav, pFormat, totalSampleCount,
DRWAV_TRUE, onWrite,
NULL, pUserData);
2238 if (paddingSize > 0) {
2276 #ifndef DR_WAV_NO_STDIO
2281 if (pWav->
onRead == drwav__on_read_stdio || pWav->
onWrite == drwav__on_write_stdio) {
2295 drwav* pWav = (
drwav*)DRWAV_MALLOC(
sizeof(*pWav));
2300 if (!
drwav_init_ex(pWav, onRead, onSeek, onChunk, pReadSeekUserData, pChunkUserData, flags)) {
2311 drwav* pWav = (
drwav*)DRWAV_MALLOC(
sizeof(*pWav));
2316 if (!drwav_init_write__internal(pWav, pFormat, totalSampleCount, isSequential, onWrite, onSeek, pUserData)) {
2326 return drwav_open_write__internal(pFormat, 0,
DRWAV_FALSE, onWrite, onSeek, pUserData);
2331 return drwav_open_write__internal(pFormat, totalSampleCount,
DRWAV_TRUE, onWrite,
NULL, pUserData);
2345 if (pWav ==
NULL || bytesToRead == 0 || pBufferOut ==
NULL) {
2364 if (pWav ==
NULL || samplesToRead == 0 || pBufferOut ==
NULL) {
2373 bytesPerSample = drwav_get_bytes_per_sample(pWav);
2374 if (bytesPerSample == 0) {
2379 if (samplesToRead * bytesPerSample > DRWAV_SIZE_MAX) {
2380 samplesToRead = DRWAV_SIZE_MAX / bytesPerSample;
2383 bytesRead =
drwav_read_raw(pWav, (
size_t)(samplesToRead * bytesPerSample), pBufferOut);
2384 return bytesRead / bytesPerSample;
2392 if (pWav ==
NULL || framesToRead == 0 || pBufferOut ==
NULL) {
2401 bytesPerFrame = drwav_get_bytes_per_pcm_frame(pWav);
2402 if (bytesPerFrame == 0) {
2407 if (framesToRead * bytesPerFrame > DRWAV_SIZE_MAX) {
2408 framesToRead = DRWAV_SIZE_MAX / bytesPerFrame;
2411 bytesRead =
drwav_read_raw(pWav, (
size_t)(framesToRead * bytesPerFrame), pBufferOut);
2412 return bytesRead / bytesPerFrame;
2467 if (!drwav_seek_to_first_pcm_frame(pWav)) {
2476 while (offset > 0) {
2479 if (samplesToRead > 2048) {
2480 samplesToRead = 2048;
2484 samplesRead = drwav_read_s16__msadpcm(pWav, samplesToRead, devnull);
2486 samplesRead = drwav_read_s16__ima(pWav, samplesToRead, devnull);
2491 if (samplesRead != samplesToRead) {
2495 offset -= samplesRead;
2508 targetBytePos =
sample * drwav_get_bytes_per_sample(pWav);
2510 if (currentBytePos < targetBytePos) {
2512 offset = (targetBytePos - currentBytePos);
2515 if (!drwav_seek_to_first_pcm_frame(pWav)) {
2518 offset = targetBytePos;
2521 while (offset > 0) {
2522 int offset32 = ((offset > INT_MAX) ? INT_MAX : (
int)offset);
2543 size_t bytesWritten;
2545 if (pWav ==
NULL || bytesToWrite == 0 || pData ==
NULL) {
2552 return bytesWritten;
2561 if (pWav ==
NULL || samplesToWrite == 0 || pData ==
NULL) {
2565 bytesToWrite = ((samplesToWrite * pWav->
bitsPerSample) / 8);
2566 if (bytesToWrite > DRWAV_SIZE_MAX) {
2572 while (bytesToWrite > 0) {
2573 size_t bytesJustWritten;
2575 if (bytesToWriteThisIteration > DRWAV_SIZE_MAX) {
2576 bytesToWriteThisIteration = DRWAV_SIZE_MAX;
2579 bytesJustWritten =
drwav_write_raw(pWav, (
size_t)bytesToWriteThisIteration, pRunningData);
2580 if (bytesJustWritten == 0) {
2584 bytesToWrite -= bytesJustWritten;
2585 bytesWritten += bytesJustWritten;
2586 pRunningData += bytesJustWritten;
2603 drwav_assert(pWav !=
NULL);
2604 drwav_assert(samplesToRead > 0);
2605 drwav_assert(pBufferOut !=
NULL);
2615 if (pWav->
onRead(pWav->
pUserData, header,
sizeof(header)) !=
sizeof(header)) {
2616 return totalSamplesRead;
2621 pWav->
msadpcm.
delta[0] = drwav__bytes_to_s16(header + 1);
2630 if (pWav->
onRead(pWav->
pUserData, header,
sizeof(header)) !=
sizeof(header)) {
2631 return totalSamplesRead;
2637 pWav->
msadpcm.
delta[0] = drwav__bytes_to_s16(header + 2);
2638 pWav->
msadpcm.
delta[1] = drwav__bytes_to_s16(header + 4);
2659 totalSamplesRead += 1;
2663 if (samplesToRead == 0) {
2664 return totalSamplesRead;
2677 230, 230, 230, 230, 307, 409, 512, 614,
2678 768, 614, 512, 409, 307, 230, 230, 230
2680 static drwav_int32 coeff1Table[] = { 256, 512, 0, 192, 240, 460, 392 };
2681 static drwav_int32 coeff2Table[] = { 0, -256, 0, 64, 0, -208, -232 };
2688 return totalSamplesRead;
2693 nibble0 = ((nibbles & 0xF0) >> 4);
if ((nibbles & 0x80)) { nibble0 |= 0xFFFFFFF0UL; }
2694 nibble1 = ((nibbles & 0x0F) >> 0);
if ((nibbles & 0x08)) { nibble1 |= 0xFFFFFFF0UL; }
2703 newSample0 = drwav_clamp(newSample0, -32768, 32767);
2716 newSample1 = drwav_clamp(newSample1, -32768, 32767);
2738 newSample0 = drwav_clamp(newSample0, -32768, 32767);
2752 newSample1 = drwav_clamp(newSample1, -32768, 32767);
2770 return totalSamplesRead;
2777 drwav_assert(pWav !=
NULL);
2778 drwav_assert(samplesToRead > 0);
2779 drwav_assert(pBufferOut !=
NULL);
2789 if (pWav->
onRead(pWav->
pUserData, header,
sizeof(header)) !=
sizeof(header)) {
2790 return totalSamplesRead;
2794 pWav->
ima.
predictor[0] = drwav__bytes_to_s16(header + 0);
2801 if (pWav->
onRead(pWav->
pUserData, header,
sizeof(header)) !=
sizeof(header)) {
2802 return totalSamplesRead;
2806 pWav->
ima.
predictor[0] = drwav__bytes_to_s16(header + 0);
2808 pWav->
ima.
predictor[1] = drwav__bytes_to_s16(header + 4);
2824 totalSamplesRead += 1;
2828 if (samplesToRead == 0) {
2829 return totalSamplesRead;
2841 -1, -1, -1, -1, 2, 4, 6, 8,
2842 -1, -1, -1, -1, 2, 4, 6, 8
2846 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
2847 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
2848 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
2849 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
2850 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
2851 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
2852 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
2853 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
2854 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
2864 for (iChannel = 0; iChannel < pWav->
channels; ++iChannel) {
2868 return totalSamplesRead;
2872 for (iByte = 0; iByte < 4; ++iByte) {
2873 drwav_uint8 nibble0 = ((nibbles[iByte] & 0x0F) >> 0);
2874 drwav_uint8 nibble1 = ((nibbles[iByte] & 0xF0) >> 4);
2880 if (nibble0 & 1) diff += step >> 2;
2881 if (nibble0 & 2) diff += step >> 1;
2882 if (nibble0 & 4) diff += step;
2883 if (nibble0 & 8) diff = -diff;
2885 predictor = drwav_clamp(predictor + diff, -32768, 32767);
2895 if (nibble1 & 1) diff += step >> 2;
2896 if (nibble1 & 2) diff += step >> 1;
2897 if (nibble1 & 4) diff += step;
2898 if (nibble1 & 8) diff = -diff;
2900 predictor = drwav_clamp(predictor + diff, -32768, 32767);
2910 return totalSamplesRead;
2914 #ifndef DR_WAV_NO_CONVERSION_API
2915 static unsigned short g_drwavAlawTable[256] = {
2916 0xEA80, 0xEB80, 0xE880, 0xE980, 0xEE80, 0xEF80, 0xEC80, 0xED80, 0xE280, 0xE380, 0xE080, 0xE180, 0xE680, 0xE780, 0xE480, 0xE580,
2917 0xF540, 0xF5C0, 0xF440, 0xF4C0, 0xF740, 0xF7C0, 0xF640, 0xF6C0, 0xF140, 0xF1C0, 0xF040, 0xF0C0, 0xF340, 0xF3C0, 0xF240, 0xF2C0,
2918 0xAA00, 0xAE00, 0xA200, 0xA600, 0xBA00, 0xBE00, 0xB200, 0xB600, 0x8A00, 0x8E00, 0x8200, 0x8600, 0x9A00, 0x9E00, 0x9200, 0x9600,
2919 0xD500, 0xD700, 0xD100, 0xD300, 0xDD00, 0xDF00, 0xD900, 0xDB00, 0xC500, 0xC700, 0xC100, 0xC300, 0xCD00, 0xCF00, 0xC900, 0xCB00,
2920 0xFEA8, 0xFEB8, 0xFE88, 0xFE98, 0xFEE8, 0xFEF8, 0xFEC8, 0xFED8, 0xFE28, 0xFE38, 0xFE08, 0xFE18, 0xFE68, 0xFE78, 0xFE48, 0xFE58,
2921 0xFFA8, 0xFFB8, 0xFF88, 0xFF98, 0xFFE8, 0xFFF8, 0xFFC8, 0xFFD8, 0xFF28, 0xFF38, 0xFF08, 0xFF18, 0xFF68, 0xFF78, 0xFF48, 0xFF58,
2922 0xFAA0, 0xFAE0, 0xFA20, 0xFA60, 0xFBA0, 0xFBE0, 0xFB20, 0xFB60, 0xF8A0, 0xF8E0, 0xF820, 0xF860, 0xF9A0, 0xF9E0, 0xF920, 0xF960,
2923 0xFD50, 0xFD70, 0xFD10, 0xFD30, 0xFDD0, 0xFDF0, 0xFD90, 0xFDB0, 0xFC50, 0xFC70, 0xFC10, 0xFC30, 0xFCD0, 0xFCF0, 0xFC90, 0xFCB0,
2924 0x1580, 0x1480, 0x1780, 0x1680, 0x1180, 0x1080, 0x1380, 0x1280, 0x1D80, 0x1C80, 0x1F80, 0x1E80, 0x1980, 0x1880, 0x1B80, 0x1A80,
2925 0x0AC0, 0x0A40, 0x0BC0, 0x0B40, 0x08C0, 0x0840, 0x09C0, 0x0940, 0x0EC0, 0x0E40, 0x0FC0, 0x0F40, 0x0CC0, 0x0C40, 0x0DC0, 0x0D40,
2926 0x5600, 0x5200, 0x5E00, 0x5A00, 0x4600, 0x4200, 0x4E00, 0x4A00, 0x7600, 0x7200, 0x7E00, 0x7A00, 0x6600, 0x6200, 0x6E00, 0x6A00,
2927 0x2B00, 0x2900, 0x2F00, 0x2D00, 0x2300, 0x2100, 0x2700, 0x2500, 0x3B00, 0x3900, 0x3F00, 0x3D00, 0x3300, 0x3100, 0x3700, 0x3500,
2928 0x0158, 0x0148, 0x0178, 0x0168, 0x0118, 0x0108, 0x0138, 0x0128, 0x01D8, 0x01C8, 0x01F8, 0x01E8, 0x0198, 0x0188, 0x01B8, 0x01A8,
2929 0x0058, 0x0048, 0x0078, 0x0068, 0x0018, 0x0008, 0x0038, 0x0028, 0x00D8, 0x00C8, 0x00F8, 0x00E8, 0x0098, 0x0088, 0x00B8, 0x00A8,
2930 0x0560, 0x0520, 0x05E0, 0x05A0, 0x0460, 0x0420, 0x04E0, 0x04A0, 0x0760, 0x0720, 0x07E0, 0x07A0, 0x0660, 0x0620, 0x06E0, 0x06A0,
2931 0x02B0, 0x0290, 0x02F0, 0x02D0, 0x0230, 0x0210, 0x0270, 0x0250, 0x03B0, 0x0390, 0x03F0, 0x03D0, 0x0330, 0x0310, 0x0370, 0x0350
2934 static unsigned short g_drwavMulawTable[256] = {
2935 0x8284, 0x8684, 0x8A84, 0x8E84, 0x9284, 0x9684, 0x9A84, 0x9E84, 0xA284, 0xA684, 0xAA84, 0xAE84, 0xB284, 0xB684, 0xBA84, 0xBE84,
2936 0xC184, 0xC384, 0xC584, 0xC784, 0xC984, 0xCB84, 0xCD84, 0xCF84, 0xD184, 0xD384, 0xD584, 0xD784, 0xD984, 0xDB84, 0xDD84, 0xDF84,
2937 0xE104, 0xE204, 0xE304, 0xE404, 0xE504, 0xE604, 0xE704, 0xE804, 0xE904, 0xEA04, 0xEB04, 0xEC04, 0xED04, 0xEE04, 0xEF04, 0xF004,
2938 0xF0C4, 0xF144, 0xF1C4, 0xF244, 0xF2C4, 0xF344, 0xF3C4, 0xF444, 0xF4C4, 0xF544, 0xF5C4, 0xF644, 0xF6C4, 0xF744, 0xF7C4, 0xF844,
2939 0xF8A4, 0xF8E4, 0xF924, 0xF964, 0xF9A4, 0xF9E4, 0xFA24, 0xFA64, 0xFAA4, 0xFAE4, 0xFB24, 0xFB64, 0xFBA4, 0xFBE4, 0xFC24, 0xFC64,
2940 0xFC94, 0xFCB4, 0xFCD4, 0xFCF4, 0xFD14, 0xFD34, 0xFD54, 0xFD74, 0xFD94, 0xFDB4, 0xFDD4, 0xFDF4, 0xFE14, 0xFE34, 0xFE54, 0xFE74,
2941 0xFE8C, 0xFE9C, 0xFEAC, 0xFEBC, 0xFECC, 0xFEDC, 0xFEEC, 0xFEFC, 0xFF0C, 0xFF1C, 0xFF2C, 0xFF3C, 0xFF4C, 0xFF5C, 0xFF6C, 0xFF7C,
2942 0xFF88, 0xFF90, 0xFF98, 0xFFA0, 0xFFA8, 0xFFB0, 0xFFB8, 0xFFC0, 0xFFC8, 0xFFD0, 0xFFD8, 0xFFE0, 0xFFE8, 0xFFF0, 0xFFF8, 0x0000,
2943 0x7D7C, 0x797C, 0x757C, 0x717C, 0x6D7C, 0x697C, 0x657C, 0x617C, 0x5D7C, 0x597C, 0x557C, 0x517C, 0x4D7C, 0x497C, 0x457C, 0x417C,
2944 0x3E7C, 0x3C7C, 0x3A7C, 0x387C, 0x367C, 0x347C, 0x327C, 0x307C, 0x2E7C, 0x2C7C, 0x2A7C, 0x287C, 0x267C, 0x247C, 0x227C, 0x207C,
2945 0x1EFC, 0x1DFC, 0x1CFC, 0x1BFC, 0x1AFC, 0x19FC, 0x18FC, 0x17FC, 0x16FC, 0x15FC, 0x14FC, 0x13FC, 0x12FC, 0x11FC, 0x10FC, 0x0FFC,
2946 0x0F3C, 0x0EBC, 0x0E3C, 0x0DBC, 0x0D3C, 0x0CBC, 0x0C3C, 0x0BBC, 0x0B3C, 0x0ABC, 0x0A3C, 0x09BC, 0x093C, 0x08BC, 0x083C, 0x07BC,
2947 0x075C, 0x071C, 0x06DC, 0x069C, 0x065C, 0x061C, 0x05DC, 0x059C, 0x055C, 0x051C, 0x04DC, 0x049C, 0x045C, 0x041C, 0x03DC, 0x039C,
2948 0x036C, 0x034C, 0x032C, 0x030C, 0x02EC, 0x02CC, 0x02AC, 0x028C, 0x026C, 0x024C, 0x022C, 0x020C, 0x01EC, 0x01CC, 0x01AC, 0x018C,
2949 0x0174, 0x0164, 0x0154, 0x0144, 0x0134, 0x0124, 0x0114, 0x0104, 0x00F4, 0x00E4, 0x00D4, 0x00C4, 0x00B4, 0x00A4, 0x0094, 0x0084,
2950 0x0078, 0x0070, 0x0068, 0x0060, 0x0058, 0x0050, 0x0048, 0x0040, 0x0038, 0x0030, 0x0028, 0x0020, 0x0018, 0x0010, 0x0008, 0x0000
2955 return (
short)g_drwavAlawTable[sampleIn];
2960 return (
short)g_drwavMulawTable[sampleIn];
2965 static void drwav__pcm_to_s16(
drwav_int16* pOut,
const unsigned char* pIn,
size_t totalSampleCount,
unsigned int bytesPerSample)
2970 if (bytesPerSample == 1) {
2977 if (bytesPerSample == 2) {
2978 for (
i = 0;
i < totalSampleCount; ++
i) {
2983 if (bytesPerSample == 3) {
2987 if (bytesPerSample == 4) {
2994 if (bytesPerSample > 8) {
2995 drwav_zero_memory(pOut, totalSampleCount *
sizeof(*pOut));
3001 for (
i = 0;
i < totalSampleCount; ++
i) {
3003 unsigned int shift = (8 - bytesPerSample) * 8;
3006 for (
j = 0;
j < bytesPerSample &&
j < 8;
j += 1) {
3016 static void drwav__ieee_to_s16(
drwav_int16* pOut,
const unsigned char* pIn,
size_t totalSampleCount,
unsigned int bytesPerSample)
3018 if (bytesPerSample == 4) {
3021 }
else if (bytesPerSample == 8) {
3026 drwav_zero_memory(pOut, totalSampleCount *
sizeof(*pOut));
3035 unsigned char sampleData[4096];
3039 return drwav_read(pWav, samplesToRead, pBufferOut);
3042 bytesPerSample = drwav_get_bytes_per_sample(pWav);
3043 if (bytesPerSample == 0) {
3047 totalSamplesRead = 0;
3049 while (samplesToRead > 0) {
3050 drwav_uint64 samplesRead =
drwav_read(pWav, drwav_min(samplesToRead,
sizeof(sampleData)/bytesPerSample), sampleData);
3051 if (samplesRead == 0) {
3055 drwav__pcm_to_s16(pBufferOut, sampleData, (
size_t)samplesRead, bytesPerSample);
3057 pBufferOut += samplesRead;
3058 samplesToRead -= samplesRead;
3059 totalSamplesRead += samplesRead;
3062 return totalSamplesRead;
3068 unsigned char sampleData[4096];
3070 drwav_uint32 bytesPerSample = drwav_get_bytes_per_sample(pWav);
3071 if (bytesPerSample == 0) {
3075 totalSamplesRead = 0;
3077 while (samplesToRead > 0) {
3078 drwav_uint64 samplesRead =
drwav_read(pWav, drwav_min(samplesToRead,
sizeof(sampleData)/bytesPerSample), sampleData);
3079 if (samplesRead == 0) {
3083 drwav__ieee_to_s16(pBufferOut, sampleData, (
size_t)samplesRead, bytesPerSample);
3085 pBufferOut += samplesRead;
3086 samplesToRead -= samplesRead;
3087 totalSamplesRead += samplesRead;
3090 return totalSamplesRead;
3096 unsigned char sampleData[4096];
3098 drwav_uint32 bytesPerSample = drwav_get_bytes_per_sample(pWav);
3099 if (bytesPerSample == 0) {
3103 totalSamplesRead = 0;
3105 while (samplesToRead > 0) {
3106 drwav_uint64 samplesRead =
drwav_read(pWav, drwav_min(samplesToRead,
sizeof(sampleData)/bytesPerSample), sampleData);
3107 if (samplesRead == 0) {
3113 pBufferOut += samplesRead;
3114 samplesToRead -= samplesRead;
3115 totalSamplesRead += samplesRead;
3118 return totalSamplesRead;
3124 unsigned char sampleData[4096];
3126 drwav_uint32 bytesPerSample = drwav_get_bytes_per_sample(pWav);
3127 if (bytesPerSample == 0) {
3131 totalSamplesRead = 0;
3133 while (samplesToRead > 0) {
3134 drwav_uint64 samplesRead =
drwav_read(pWav, drwav_min(samplesToRead,
sizeof(sampleData)/bytesPerSample), sampleData);
3135 if (samplesRead == 0) {
3141 pBufferOut += samplesRead;
3142 samplesToRead -= samplesRead;
3143 totalSamplesRead += samplesRead;
3146 return totalSamplesRead;
3151 if (pWav ==
NULL || samplesToRead == 0 || pBufferOut ==
NULL) {
3156 if (samplesToRead *
sizeof(
drwav_int16) > DRWAV_SIZE_MAX) {
3157 samplesToRead = DRWAV_SIZE_MAX /
sizeof(
drwav_int16);
3161 return drwav_read_s16__pcm(pWav, samplesToRead, pBufferOut);
3165 return drwav_read_s16__msadpcm(pWav, samplesToRead, pBufferOut);
3169 return drwav_read_s16__ieee(pWav, samplesToRead, pBufferOut);
3173 return drwav_read_s16__alaw(pWav, samplesToRead, pBufferOut);
3177 return drwav_read_s16__mulaw(pWav, samplesToRead, pBufferOut);
3181 return drwav_read_s16__ima(pWav, samplesToRead, pBufferOut);
3196 for (
i = 0;
i < sampleCount; ++
i) {
3208 for (
i = 0;
i < sampleCount; ++
i) {
3209 int x = ((
int)(((
unsigned int)(((
const unsigned char*)pIn)[
i*3+0]) << 8) | ((
unsigned int)(((
const unsigned char*)pIn)[
i*3+1]) << 16) | ((
unsigned int)(((
const unsigned char*)pIn)[
i*3+2])) << 24)) >> 8;
3219 for (
i = 0;
i < sampleCount; ++
i) {
3230 for (
i = 0;
i < sampleCount; ++
i) {
3233 c = ((
x < -1) ? -1 : ((
x > 1) ? 1 :
x));
3235 r = (
int)(c * 32767.5f);
3245 for (
i = 0;
i < sampleCount; ++
i) {
3248 c = ((
x < -1) ? -1 : ((
x > 1) ? 1 :
x));
3250 r = (
int)(c * 32767.5);
3259 for (
i = 0;
i < sampleCount; ++
i) {
3260 pOut[
i] = drwav__alaw_to_s16(pIn[
i]);
3267 for (
i = 0;
i < sampleCount; ++
i) {
3268 pOut[
i] = drwav__mulaw_to_s16(pIn[
i]);
3274 static void drwav__pcm_to_f32(
float* pOut,
const unsigned char* pIn,
size_t sampleCount,
unsigned int bytesPerSample)
3279 if (bytesPerSample == 1) {
3285 if (bytesPerSample == 2) {
3289 if (bytesPerSample == 3) {
3293 if (bytesPerSample == 4) {
3300 if (bytesPerSample > 8) {
3301 drwav_zero_memory(pOut, sampleCount *
sizeof(*pOut));
3307 for (
i = 0;
i < sampleCount; ++
i) {
3309 unsigned int shift = (8 - bytesPerSample) * 8;
3312 for (
j = 0;
j < bytesPerSample &&
j < 8;
j += 1) {
3322 static void drwav__ieee_to_f32(
float* pOut,
const unsigned char* pIn,
size_t sampleCount,
unsigned int bytesPerSample)
3324 if (bytesPerSample == 4) {
3326 for (
i = 0;
i < sampleCount; ++
i) {
3327 *pOut++ = ((
const float*)pIn)[
i];
3330 }
else if (bytesPerSample == 8) {
3335 drwav_zero_memory(pOut, sampleCount *
sizeof(*pOut));
3344 unsigned char sampleData[4096];
3346 drwav_uint32 bytesPerSample = drwav_get_bytes_per_sample(pWav);
3347 if (bytesPerSample == 0) {
3351 totalSamplesRead = 0;
3353 while (samplesToRead > 0) {
3354 drwav_uint64 samplesRead =
drwav_read(pWav, drwav_min(samplesToRead,
sizeof(sampleData)/bytesPerSample), sampleData);
3355 if (samplesRead == 0) {
3359 drwav__pcm_to_f32(pBufferOut, sampleData, (
size_t)samplesRead, bytesPerSample);
3360 pBufferOut += samplesRead;
3362 samplesToRead -= samplesRead;
3363 totalSamplesRead += samplesRead;
3366 return totalSamplesRead;
3377 while (samplesToRead > 0) {
3379 if (samplesRead == 0) {
3385 pBufferOut += samplesRead;
3386 samplesToRead -= samplesRead;
3387 totalSamplesRead += samplesRead;
3390 return totalSamplesRead;
3401 while (samplesToRead > 0) {
3403 if (samplesRead == 0) {
3409 pBufferOut += samplesRead;
3410 samplesToRead -= samplesRead;
3411 totalSamplesRead += samplesRead;
3414 return totalSamplesRead;
3420 unsigned char sampleData[4096];
3425 return drwav_read(pWav, samplesToRead, pBufferOut);
3428 bytesPerSample = drwav_get_bytes_per_sample(pWav);
3429 if (bytesPerSample == 0) {
3433 totalSamplesRead = 0;
3435 while (samplesToRead > 0) {
3436 drwav_uint64 samplesRead =
drwav_read(pWav, drwav_min(samplesToRead,
sizeof(sampleData)/bytesPerSample), sampleData);
3437 if (samplesRead == 0) {
3441 drwav__ieee_to_f32(pBufferOut, sampleData, (
size_t)samplesRead, bytesPerSample);
3443 pBufferOut += samplesRead;
3444 samplesToRead -= samplesRead;
3445 totalSamplesRead += samplesRead;
3448 return totalSamplesRead;
3454 unsigned char sampleData[4096];
3455 drwav_uint32 bytesPerSample = drwav_get_bytes_per_sample(pWav);
3456 if (bytesPerSample == 0) {
3460 totalSamplesRead = 0;
3462 while (samplesToRead > 0) {
3463 drwav_uint64 samplesRead =
drwav_read(pWav, drwav_min(samplesToRead,
sizeof(sampleData)/bytesPerSample), sampleData);
3464 if (samplesRead == 0) {
3470 pBufferOut += samplesRead;
3471 samplesToRead -= samplesRead;
3472 totalSamplesRead += samplesRead;
3475 return totalSamplesRead;
3481 unsigned char sampleData[4096];
3483 drwav_uint32 bytesPerSample = drwav_get_bytes_per_sample(pWav);
3484 if (bytesPerSample == 0) {
3488 totalSamplesRead = 0;
3490 while (samplesToRead > 0) {
3491 drwav_uint64 samplesRead =
drwav_read(pWav, drwav_min(samplesToRead,
sizeof(sampleData)/bytesPerSample), sampleData);
3492 if (samplesRead == 0) {
3498 pBufferOut += samplesRead;
3499 samplesToRead -= samplesRead;
3500 totalSamplesRead += samplesRead;
3503 return totalSamplesRead;
3508 if (pWav ==
NULL || samplesToRead == 0 || pBufferOut ==
NULL) {
3513 if (samplesToRead *
sizeof(
float) > DRWAV_SIZE_MAX) {
3514 samplesToRead = DRWAV_SIZE_MAX /
sizeof(float);
3518 return drwav_read_f32__pcm(pWav, samplesToRead, pBufferOut);
3522 return drwav_read_f32__msadpcm(pWav, samplesToRead, pBufferOut);
3526 return drwav_read_f32__ieee(pWav, samplesToRead, pBufferOut);
3530 return drwav_read_f32__alaw(pWav, samplesToRead, pBufferOut);
3534 return drwav_read_f32__mulaw(pWav, samplesToRead, pBufferOut);
3538 return drwav_read_f32__ima(pWav, samplesToRead, pBufferOut);
3557 #ifdef DR_WAV_LIBSNDFILE_COMPAT
3564 for (
i = 0;
i < sampleCount; ++
i) {
3565 *pOut++ = (pIn[
i] / 256.0f) * 2 - 1;
3568 for (
i = 0;
i < sampleCount; ++
i) {
3569 *pOut++ = (pIn[
i] / 255.0f) * 2 - 1;
3582 for (
i = 0;
i < sampleCount; ++
i) {
3583 *pOut++ = pIn[
i] / 32768.0f;
3595 for (
i = 0;
i < sampleCount; ++
i) {
3596 unsigned int s0 = pIn[
i*3 + 0];
3597 unsigned int s1 = pIn[
i*3 + 1];
3598 unsigned int s2 = pIn[
i*3 + 2];
3600 int sample32 = (
int)((s0 << 8) | (s1 << 16) | (s2 << 24));
3601 *pOut++ = (float)(sample32 / 2147483648.0);
3612 for (
i = 0;
i < sampleCount; ++
i) {
3613 *pOut++ = (float)(pIn[
i] / 2147483648.0);
3625 for (
i = 0;
i < sampleCount; ++
i) {
3626 *pOut++ = (float)pIn[
i];
3638 for (
i = 0;
i < sampleCount; ++
i) {
3639 *pOut++ = drwav__alaw_to_s16(pIn[
i]) / 32768.0f;
3651 for (
i = 0;
i < sampleCount; ++
i) {
3652 *pOut++ = drwav__mulaw_to_s16(pIn[
i]) / 32768.0f;
3658 static void drwav__pcm_to_s32(
drwav_int32* pOut,
const unsigned char* pIn,
size_t totalSampleCount,
unsigned int bytesPerSample)
3663 if (bytesPerSample == 1) {
3669 if (bytesPerSample == 2) {
3673 if (bytesPerSample == 3) {
3677 if (bytesPerSample == 4) {
3678 for (
i = 0;
i < totalSampleCount; ++
i) {
3686 if (bytesPerSample > 8) {
3687 drwav_zero_memory(pOut, totalSampleCount *
sizeof(*pOut));
3693 for (
i = 0;
i < totalSampleCount; ++
i) {
3695 unsigned int shift = (8 - bytesPerSample) * 8;
3698 for (
j = 0;
j < bytesPerSample &&
j < 8;
j += 1) {
3708 static void drwav__ieee_to_s32(
drwav_int32* pOut,
const unsigned char* pIn,
size_t totalSampleCount,
unsigned int bytesPerSample)
3710 if (bytesPerSample == 4) {
3713 }
else if (bytesPerSample == 8) {
3718 drwav_zero_memory(pOut, totalSampleCount *
sizeof(*pOut));
3727 unsigned char sampleData[4096];
3732 return drwav_read(pWav, samplesToRead, pBufferOut);
3735 bytesPerSample = drwav_get_bytes_per_sample(pWav);
3736 if (bytesPerSample == 0) {
3740 totalSamplesRead = 0;
3742 while (samplesToRead > 0) {
3743 drwav_uint64 samplesRead =
drwav_read(pWav, drwav_min(samplesToRead,
sizeof(sampleData)/bytesPerSample), sampleData);
3744 if (samplesRead == 0) {
3748 drwav__pcm_to_s32(pBufferOut, sampleData, (
size_t)samplesRead, bytesPerSample);
3750 pBufferOut += samplesRead;
3751 samplesToRead -= samplesRead;
3752 totalSamplesRead += samplesRead;
3755 return totalSamplesRead;
3766 while (samplesToRead > 0) {
3768 if (samplesRead == 0) {
3774 pBufferOut += samplesRead;
3775 samplesToRead -= samplesRead;
3776 totalSamplesRead += samplesRead;
3779 return totalSamplesRead;
3790 while (samplesToRead > 0) {
3792 if (samplesRead == 0) {
3798 pBufferOut += samplesRead;
3799 samplesToRead -= samplesRead;
3800 totalSamplesRead += samplesRead;
3803 return totalSamplesRead;
3809 unsigned char sampleData[4096];
3811 drwav_uint32 bytesPerSample = drwav_get_bytes_per_sample(pWav);
3812 if (bytesPerSample == 0) {
3816 totalSamplesRead = 0;
3818 while (samplesToRead > 0) {
3819 drwav_uint64 samplesRead =
drwav_read(pWav, drwav_min(samplesToRead,
sizeof(sampleData)/bytesPerSample), sampleData);
3820 if (samplesRead == 0) {
3824 drwav__ieee_to_s32(pBufferOut, sampleData, (
size_t)samplesRead, bytesPerSample);
3826 pBufferOut += samplesRead;
3827 samplesToRead -= samplesRead;
3828 totalSamplesRead += samplesRead;
3831 return totalSamplesRead;
3837 unsigned char sampleData[4096];
3839 drwav_uint32 bytesPerSample = drwav_get_bytes_per_sample(pWav);
3840 if (bytesPerSample == 0) {
3844 totalSamplesRead = 0;
3846 while (samplesToRead > 0) {
3847 drwav_uint64 samplesRead =
drwav_read(pWav, drwav_min(samplesToRead,
sizeof(sampleData)/bytesPerSample), sampleData);
3848 if (samplesRead == 0) {
3854 pBufferOut += samplesRead;
3855 samplesToRead -= samplesRead;
3856 totalSamplesRead += samplesRead;
3859 return totalSamplesRead;
3865 unsigned char sampleData[4096];
3867 drwav_uint32 bytesPerSample = drwav_get_bytes_per_sample(pWav);
3868 if (bytesPerSample == 0) {
3872 totalSamplesRead = 0;
3874 while (samplesToRead > 0) {
3875 drwav_uint64 samplesRead =
drwav_read(pWav, drwav_min(samplesToRead,
sizeof(sampleData)/bytesPerSample), sampleData);
3876 if (samplesRead == 0) {
3882 pBufferOut += samplesRead;
3883 samplesToRead -= samplesRead;
3884 totalSamplesRead += samplesRead;
3887 return totalSamplesRead;
3892 if (pWav ==
NULL || samplesToRead == 0 || pBufferOut ==
NULL) {
3897 if (samplesToRead *
sizeof(
drwav_int32) > DRWAV_SIZE_MAX) {
3898 samplesToRead = DRWAV_SIZE_MAX /
sizeof(
drwav_int32);
3903 return drwav_read_s32__pcm(pWav, samplesToRead, pBufferOut);
3907 return drwav_read_s32__msadpcm(pWav, samplesToRead, pBufferOut);
3911 return drwav_read_s32__ieee(pWav, samplesToRead, pBufferOut);
3915 return drwav_read_s32__alaw(pWav, samplesToRead, pBufferOut);
3919 return drwav_read_s32__mulaw(pWav, samplesToRead, pBufferOut);
3923 return drwav_read_s32__ima(pWav, samplesToRead, pBufferOut);
3942 for (
i = 0;
i < sampleCount; ++
i) {
3943 *pOut++ = ((
int)pIn[
i] - 128) << 24;
3955 for (
i = 0;
i < sampleCount; ++
i) {
3956 *pOut++ = pIn[
i] << 16;
3968 for (
i = 0;
i < sampleCount; ++
i) {
3969 unsigned int s0 = pIn[
i*3 + 0];
3970 unsigned int s1 = pIn[
i*3 + 1];
3971 unsigned int s2 = pIn[
i*3 + 2];
3986 for (
i = 0;
i < sampleCount; ++
i) {
3999 for (
i = 0;
i < sampleCount; ++
i) {
4012 for (
i = 0;
i < sampleCount; ++
i) {
4013 *pOut++ = ((
drwav_int32)drwav__alaw_to_s16(pIn[
i])) << 16;
4025 for (
i= 0;
i < sampleCount; ++
i) {
4026 *pOut++ = ((
drwav_int32)drwav__mulaw_to_s16(pIn[
i])) << 16;
4038 drwav_assert(pWav !=
NULL);
4041 if (sampleDataSize > DRWAV_SIZE_MAX) {
4046 pSampleData = (
drwav_int16*)DRWAV_MALLOC((
size_t)sampleDataSize);
4047 if (pSampleData ==
NULL) {
4054 DRWAV_FREE(pSampleData);
4067 if (totalSampleCount) {
4074 float* drwav__read_and_close_f32(
drwav* pWav,
unsigned int*
channels,
unsigned int* sampleRate,
drwav_uint64* totalSampleCount)
4080 drwav_assert(pWav !=
NULL);
4083 if (sampleDataSize > DRWAV_SIZE_MAX) {
4088 pSampleData = (
float*)DRWAV_MALLOC((
size_t)sampleDataSize);
4089 if (pSampleData ==
NULL) {
4096 DRWAV_FREE(pSampleData);
4109 if (totalSampleCount) {
4122 drwav_assert(pWav !=
NULL);
4125 if (sampleDataSize > DRWAV_SIZE_MAX) {
4130 pSampleData = (
drwav_int32*)DRWAV_MALLOC((
size_t)sampleDataSize);
4131 if (pSampleData ==
NULL) {
4138 DRWAV_FREE(pSampleData);
4151 if (totalSampleCount) {
4169 if (totalSampleCount) {
4170 *totalSampleCount = 0;
4173 if (!
drwav_init(&wav, onRead, onSeek, pUserData)) {
4177 return drwav__read_and_close_s16(&wav,
channels, sampleRate, totalSampleCount);
4183 unsigned int sampleRate;
4190 if (sampleRateOut) {
4193 if (totalFrameCountOut) {
4194 *totalFrameCountOut = 0;
4198 if (result ==
NULL) {
4205 if (sampleRateOut) {
4206 *sampleRateOut = sampleRate;
4208 if (totalFrameCountOut) {
4209 *totalFrameCountOut = totalSampleCount /
channels;
4225 if (totalSampleCount) {
4226 *totalSampleCount = 0;
4229 if (!
drwav_init(&wav, onRead, onSeek, pUserData)) {
4233 return drwav__read_and_close_f32(&wav,
channels, sampleRate, totalSampleCount);
4239 unsigned int sampleRate;
4246 if (sampleRateOut) {
4249 if (totalFrameCountOut) {
4250 *totalFrameCountOut = 0;
4254 if (result ==
NULL) {
4261 if (sampleRateOut) {
4262 *sampleRateOut = sampleRate;
4264 if (totalFrameCountOut) {
4265 *totalFrameCountOut = totalSampleCount /
channels;
4281 if (totalSampleCount) {
4282 *totalSampleCount = 0;
4285 if (!
drwav_init(&wav, onRead, onSeek, pUserData)) {
4289 return drwav__read_and_close_s32(&wav,
channels, sampleRate, totalSampleCount);
4295 unsigned int sampleRate;
4302 if (sampleRateOut) {
4305 if (totalFrameCountOut) {
4306 *totalFrameCountOut = 0;
4310 if (result ==
NULL) {
4317 if (sampleRateOut) {
4318 *sampleRateOut = sampleRate;
4320 if (totalFrameCountOut) {
4321 *totalFrameCountOut = totalSampleCount /
channels;
4327 #ifndef DR_WAV_NO_STDIO
4338 if (totalSampleCount) {
4339 *totalSampleCount = 0;
4346 return drwav__read_and_close_s16(&wav,
channels, sampleRate, totalSampleCount);
4352 unsigned int sampleRate;
4359 if (sampleRateOut) {
4362 if (totalFrameCountOut) {
4363 *totalFrameCountOut = 0;
4366 result = drwav_open_file_and_read_s16(filename, &
channels, &sampleRate, &totalSampleCount);
4367 if (result ==
NULL) {
4374 if (sampleRateOut) {
4375 *sampleRateOut = sampleRate;
4377 if (totalFrameCountOut) {
4378 *totalFrameCountOut = totalSampleCount /
channels;
4394 if (totalSampleCount) {
4395 *totalSampleCount = 0;
4402 return drwav__read_and_close_f32(&wav,
channels, sampleRate, totalSampleCount);
4408 unsigned int sampleRate;
4415 if (sampleRateOut) {
4418 if (totalFrameCountOut) {
4419 *totalFrameCountOut = 0;
4423 if (result ==
NULL) {
4430 if (sampleRateOut) {
4431 *sampleRateOut = sampleRate;
4433 if (totalFrameCountOut) {
4434 *totalFrameCountOut = totalSampleCount /
channels;
4450 if (totalSampleCount) {
4451 *totalSampleCount = 0;
4458 return drwav__read_and_close_s32(&wav,
channels, sampleRate, totalSampleCount);
4464 unsigned int sampleRate;
4471 if (sampleRateOut) {
4474 if (totalFrameCountOut) {
4475 *totalFrameCountOut = 0;
4479 if (result ==
NULL) {
4486 if (sampleRateOut) {
4487 *sampleRateOut = sampleRate;
4489 if (totalFrameCountOut) {
4490 *totalFrameCountOut = totalSampleCount /
channels;
4507 if (totalSampleCount) {
4508 *totalSampleCount = 0;
4515 return drwav__read_and_close_s16(&wav,
channels, sampleRate, totalSampleCount);
4521 unsigned int sampleRate;
4528 if (sampleRateOut) {
4531 if (totalFrameCountOut) {
4532 *totalFrameCountOut = 0;
4536 if (result ==
NULL) {
4543 if (sampleRateOut) {
4544 *sampleRateOut = sampleRate;
4546 if (totalFrameCountOut) {
4547 *totalFrameCountOut = totalSampleCount /
channels;
4563 if (totalSampleCount) {
4564 *totalSampleCount = 0;
4571 return drwav__read_and_close_f32(&wav,
channels, sampleRate, totalSampleCount);
4577 unsigned int sampleRate;
4584 if (sampleRateOut) {
4587 if (totalFrameCountOut) {
4588 *totalFrameCountOut = 0;
4592 if (result ==
NULL) {
4599 if (sampleRateOut) {
4600 *sampleRateOut = sampleRate;
4602 if (totalFrameCountOut) {
4603 *totalFrameCountOut = totalSampleCount /
channels;
4619 if (totalSampleCount) {
4620 *totalSampleCount = 0;
4627 return drwav__read_and_close_s32(&wav,
channels, sampleRate, totalSampleCount);
4633 unsigned int sampleRate;
4640 if (sampleRateOut) {
4643 if (totalFrameCountOut) {
4644 *totalFrameCountOut = 0;
4648 if (result ==
NULL) {
4655 if (sampleRateOut) {
4656 *sampleRateOut = sampleRate;
4658 if (totalFrameCountOut) {
4659 *totalFrameCountOut = totalSampleCount /
channels;
4667 void drwav_free(
void* pDataReturnedByOpenAndRead)
4669 DRWAV_FREE(pDataReturnedByOpenAndRead);