2007-03-27 17:47:10 +00:00
|
|
|
/*
|
|
|
|
* CiderPress
|
|
|
|
* Copyright (C) 2007 by faddenSoft, LLC. All Rights Reserved.
|
|
|
|
* See the file LICENSE for distribution terms.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Read-only encapsulation of a sound file.
|
|
|
|
*
|
|
|
|
* Microsoft supplies a number of functions (e.g. mmioOpen) that do a lot of
|
|
|
|
* this, but it's built on top of fairly complex machinery designed to
|
|
|
|
* support AVI files and read/write WAV files. It actually requires about
|
|
|
|
* the same amount of code to parse a WAV file without resorting to MMIO.
|
|
|
|
*/
|
|
|
|
#include "StdAfx.h"
|
|
|
|
#include "SoundFile.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert four characters into a single 4-byte constant.
|
|
|
|
*
|
|
|
|
* The constant is expected to match 4-character values read from a binary
|
|
|
|
* file, so the value is endian-dependent.
|
|
|
|
*/
|
2014-11-25 22:34:14 +00:00
|
|
|
static inline uint32_t MakeFourCC(uint8_t c0, uint8_t c1, uint8_t c2,
|
|
|
|
uint8_t c3)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
/* little-endian */
|
2014-11-25 22:34:14 +00:00
|
|
|
return ((uint32_t)c0) | ((uint32_t)c1 << 8) |
|
|
|
|
((uint32_t)c2 << 16) | ((uint32_t)c3 << 24);
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the object from a file on disk.
|
|
|
|
*
|
|
|
|
* Returns 0 on success.
|
|
|
|
*/
|
2014-11-25 22:34:14 +00:00
|
|
|
int SoundFile::Create(const WCHAR* fileName, CString* pErrMsg)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-18 05:13:13 +00:00
|
|
|
FILE* fp = NULL;
|
2014-11-04 00:26:53 +00:00
|
|
|
long fileLen;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
Large set of changes to restore CiderPress build.
CiderPress and MDC now compile, and execute far enough to open
their respective "about" boxes, but I doubt they'll do much
more than that.
* Switch from MBCS to UNICODE APIs
Microsoft switched to UTF-16 (by way of UCS-2) a long time ago,
and the support for MBCS seems to be getting phased out. So it's
time to switch to wide strings.
This is a bit awkward for CiderPress because it works with disk
and file archives with 8-bit filenames, and I want NufxLib and
DiskImgLib to continue to work on Linux (which has largely taken
the UTF-8 approach to Unicode). The libraries will continue to
work with 8-bit filenames, with CiderPress/MDC doing the
conversion at the appropriate point.
There were a couple of places where strings from a structure
handed back by one of the libraries were used directly in the UI,
or vice-versa, which is a problem because we have nowhere to
store the result of the conversion. These currently have fixed
place-holder "xyzzy" strings.
All UI strings are now wide.
Various format strings now use "%ls" and "%hs" to explicitly
specify wide and narrow. This doesn't play well with gcc, so
only the Windows-specific parts use those.
* Various updates to vcxproj files
The project-file conversion had some cruft that is now largely
gone. The build now has a common output directory for the EXEs
and libraries, avoiding the old post-build copy steps.
* Added zlib 1.2.8 and nufxlib 2.2.2 source snapshots
The old "prebuilts" directory is now gone. The libraries are now
built as part of building the apps.
I added a minimal set of files for zlib, and a full set for nufxlib.
The Linux-specific nufxlib goodies are included for the benefit of
the Linux utilities, which are currently broken (don't build).
* Replace symbols used for include guards
Symbols with a leading "__" are reserved.
2014-11-10 23:32:55 +00:00
|
|
|
fp = _wfopen(fileName, L"rb");
|
2014-11-18 05:13:13 +00:00
|
|
|
if (fp == NULL) {
|
2014-11-04 00:26:53 +00:00
|
|
|
int err = errno;
|
Large set of changes to restore CiderPress build.
CiderPress and MDC now compile, and execute far enough to open
their respective "about" boxes, but I doubt they'll do much
more than that.
* Switch from MBCS to UNICODE APIs
Microsoft switched to UTF-16 (by way of UCS-2) a long time ago,
and the support for MBCS seems to be getting phased out. So it's
time to switch to wide strings.
This is a bit awkward for CiderPress because it works with disk
and file archives with 8-bit filenames, and I want NufxLib and
DiskImgLib to continue to work on Linux (which has largely taken
the UTF-8 approach to Unicode). The libraries will continue to
work with 8-bit filenames, with CiderPress/MDC doing the
conversion at the appropriate point.
There were a couple of places where strings from a structure
handed back by one of the libraries were used directly in the UI,
or vice-versa, which is a problem because we have nowhere to
store the result of the conversion. These currently have fixed
place-holder "xyzzy" strings.
All UI strings are now wide.
Various format strings now use "%ls" and "%hs" to explicitly
specify wide and narrow. This doesn't play well with gcc, so
only the Windows-specific parts use those.
* Various updates to vcxproj files
The project-file conversion had some cruft that is now largely
gone. The build now has a common output directory for the EXEs
and libraries, avoiding the old post-build copy steps.
* Added zlib 1.2.8 and nufxlib 2.2.2 source snapshots
The old "prebuilts" directory is now gone. The libraries are now
built as part of building the apps.
I added a minimal set of files for zlib, and a full set for nufxlib.
The Linux-specific nufxlib goodies are included for the benefit of
the Linux utilities, which are currently broken (don't build).
* Replace symbols used for include guards
Symbols with a leading "__" are reserved.
2014-11-10 23:32:55 +00:00
|
|
|
pErrMsg->Format(L"Unable to open '%ls'", fileName);
|
2014-11-04 00:26:53 +00:00
|
|
|
return err;
|
|
|
|
}
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
fseek(fp, 0, SEEK_END);
|
|
|
|
fileLen = ftell(fp);
|
|
|
|
rewind(fp);
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
return Create(fp, fileLen, true, pErrMsg);
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the object from an already-open file pointer. The file must be
|
|
|
|
* seeked to the start of the WAV file.
|
|
|
|
*
|
|
|
|
* Returns 0 on success.
|
|
|
|
*/
|
2014-11-25 22:34:14 +00:00
|
|
|
int SoundFile::Create(FILE* fp, long len, bool doClose, CString* pErrMsg)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
struct {
|
2014-11-25 22:34:14 +00:00
|
|
|
uint32_t riff;
|
|
|
|
uint32_t fileLen;
|
|
|
|
uint32_t wav;
|
2014-11-04 00:26:53 +00:00
|
|
|
} fileHeader;
|
2014-11-25 22:34:14 +00:00
|
|
|
uint32_t chunkLen;
|
2014-11-04 00:26:53 +00:00
|
|
|
int err = 0;
|
|
|
|
|
2014-11-18 05:13:13 +00:00
|
|
|
if (mFP != NULL) {
|
2014-11-25 22:34:14 +00:00
|
|
|
LOGW("SoundFile object already created");
|
2014-11-04 00:26:53 +00:00
|
|
|
assert(false);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-18 05:13:13 +00:00
|
|
|
if (fp == NULL)
|
2014-11-04 00:26:53 +00:00
|
|
|
return -1;
|
|
|
|
if (len < kWAVMinSize) {
|
Large set of changes to restore CiderPress build.
CiderPress and MDC now compile, and execute far enough to open
their respective "about" boxes, but I doubt they'll do much
more than that.
* Switch from MBCS to UNICODE APIs
Microsoft switched to UTF-16 (by way of UCS-2) a long time ago,
and the support for MBCS seems to be getting phased out. So it's
time to switch to wide strings.
This is a bit awkward for CiderPress because it works with disk
and file archives with 8-bit filenames, and I want NufxLib and
DiskImgLib to continue to work on Linux (which has largely taken
the UTF-8 approach to Unicode). The libraries will continue to
work with 8-bit filenames, with CiderPress/MDC doing the
conversion at the appropriate point.
There were a couple of places where strings from a structure
handed back by one of the libraries were used directly in the UI,
or vice-versa, which is a problem because we have nowhere to
store the result of the conversion. These currently have fixed
place-holder "xyzzy" strings.
All UI strings are now wide.
Various format strings now use "%ls" and "%hs" to explicitly
specify wide and narrow. This doesn't play well with gcc, so
only the Windows-specific parts use those.
* Various updates to vcxproj files
The project-file conversion had some cruft that is now largely
gone. The build now has a common output directory for the EXEs
and libraries, avoiding the old post-build copy steps.
* Added zlib 1.2.8 and nufxlib 2.2.2 source snapshots
The old "prebuilts" directory is now gone. The libraries are now
built as part of building the apps.
I added a minimal set of files for zlib, and a full set for nufxlib.
The Linux-specific nufxlib goodies are included for the benefit of
the Linux utilities, which are currently broken (don't build).
* Replace symbols used for include guards
Symbols with a leading "__" are reserved.
2014-11-10 23:32:55 +00:00
|
|
|
*pErrMsg = L"File is too short to be WAV";
|
2014-11-04 00:26:53 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* from here on out, we own fp if doClose==true */
|
|
|
|
mFP = fp;
|
|
|
|
mDoClose = doClose;
|
|
|
|
mFileStart = ftell(mFP); // so we can rewind if necessary
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the first header.
|
|
|
|
*/
|
|
|
|
if (fread(&fileHeader, sizeof(fileHeader), 1, mFP) != 1) {
|
|
|
|
err = errno ? errno : -1;
|
Large set of changes to restore CiderPress build.
CiderPress and MDC now compile, and execute far enough to open
their respective "about" boxes, but I doubt they'll do much
more than that.
* Switch from MBCS to UNICODE APIs
Microsoft switched to UTF-16 (by way of UCS-2) a long time ago,
and the support for MBCS seems to be getting phased out. So it's
time to switch to wide strings.
This is a bit awkward for CiderPress because it works with disk
and file archives with 8-bit filenames, and I want NufxLib and
DiskImgLib to continue to work on Linux (which has largely taken
the UTF-8 approach to Unicode). The libraries will continue to
work with 8-bit filenames, with CiderPress/MDC doing the
conversion at the appropriate point.
There were a couple of places where strings from a structure
handed back by one of the libraries were used directly in the UI,
or vice-versa, which is a problem because we have nowhere to
store the result of the conversion. These currently have fixed
place-holder "xyzzy" strings.
All UI strings are now wide.
Various format strings now use "%ls" and "%hs" to explicitly
specify wide and narrow. This doesn't play well with gcc, so
only the Windows-specific parts use those.
* Various updates to vcxproj files
The project-file conversion had some cruft that is now largely
gone. The build now has a common output directory for the EXEs
and libraries, avoiding the old post-build copy steps.
* Added zlib 1.2.8 and nufxlib 2.2.2 source snapshots
The old "prebuilts" directory is now gone. The libraries are now
built as part of building the apps.
I added a minimal set of files for zlib, and a full set for nufxlib.
The Linux-specific nufxlib goodies are included for the benefit of
the Linux utilities, which are currently broken (don't build).
* Replace symbols used for include guards
Symbols with a leading "__" are reserved.
2014-11-10 23:32:55 +00:00
|
|
|
*pErrMsg = L"Failed reading file header";
|
2014-11-04 00:26:53 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
if (fileHeader.riff != MakeFourCC('R','I','F','F') ||
|
|
|
|
fileHeader.wav != MakeFourCC('W','A','V','E') ||
|
2014-11-25 22:34:14 +00:00
|
|
|
fileHeader.fileLen > (uint32_t) len)
|
2014-11-04 00:26:53 +00:00
|
|
|
{
|
Large set of changes to restore CiderPress build.
CiderPress and MDC now compile, and execute far enough to open
their respective "about" boxes, but I doubt they'll do much
more than that.
* Switch from MBCS to UNICODE APIs
Microsoft switched to UTF-16 (by way of UCS-2) a long time ago,
and the support for MBCS seems to be getting phased out. So it's
time to switch to wide strings.
This is a bit awkward for CiderPress because it works with disk
and file archives with 8-bit filenames, and I want NufxLib and
DiskImgLib to continue to work on Linux (which has largely taken
the UTF-8 approach to Unicode). The libraries will continue to
work with 8-bit filenames, with CiderPress/MDC doing the
conversion at the appropriate point.
There were a couple of places where strings from a structure
handed back by one of the libraries were used directly in the UI,
or vice-versa, which is a problem because we have nowhere to
store the result of the conversion. These currently have fixed
place-holder "xyzzy" strings.
All UI strings are now wide.
Various format strings now use "%ls" and "%hs" to explicitly
specify wide and narrow. This doesn't play well with gcc, so
only the Windows-specific parts use those.
* Various updates to vcxproj files
The project-file conversion had some cruft that is now largely
gone. The build now has a common output directory for the EXEs
and libraries, avoiding the old post-build copy steps.
* Added zlib 1.2.8 and nufxlib 2.2.2 source snapshots
The old "prebuilts" directory is now gone. The libraries are now
built as part of building the apps.
I added a minimal set of files for zlib, and a full set for nufxlib.
The Linux-specific nufxlib goodies are included for the benefit of
the Linux utilities, which are currently broken (don't build).
* Replace symbols used for include guards
Symbols with a leading "__" are reserved.
2014-11-10 23:32:55 +00:00
|
|
|
*pErrMsg = L"File is not a WAV file";
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("Not a valid WAV header (0x%08lx %d 0x%08lx)",
|
2014-11-04 00:26:53 +00:00
|
|
|
fileHeader.riff, fileHeader.fileLen, fileHeader.wav);
|
|
|
|
err = -1;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the "fmt " chunk, which holds the WAV format data. This should
|
|
|
|
* immediately follow the file header, but I'm not sure that's mandatory.
|
|
|
|
* I *am* going to assume that it precedes the "data" chunk, so I don't
|
|
|
|
* have to rewind the file after finding "fmt ".
|
|
|
|
*/
|
|
|
|
err = SkipToHeader(MakeFourCC('f','m','t',' '), &chunkLen);
|
|
|
|
if (err != 0)
|
|
|
|
goto bail;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the WAVEFORMATEX structure directly from the file. This is useful
|
|
|
|
* as an argument to certain DirectX functions. The structure contains
|
|
|
|
* an extra field, so we need to zero it out and use chunkLen rather
|
|
|
|
* than the structure size.
|
|
|
|
*/
|
|
|
|
if (chunkLen > sizeof(WAVEFORMATEX)) {
|
Large set of changes to restore CiderPress build.
CiderPress and MDC now compile, and execute far enough to open
their respective "about" boxes, but I doubt they'll do much
more than that.
* Switch from MBCS to UNICODE APIs
Microsoft switched to UTF-16 (by way of UCS-2) a long time ago,
and the support for MBCS seems to be getting phased out. So it's
time to switch to wide strings.
This is a bit awkward for CiderPress because it works with disk
and file archives with 8-bit filenames, and I want NufxLib and
DiskImgLib to continue to work on Linux (which has largely taken
the UTF-8 approach to Unicode). The libraries will continue to
work with 8-bit filenames, with CiderPress/MDC doing the
conversion at the appropriate point.
There were a couple of places where strings from a structure
handed back by one of the libraries were used directly in the UI,
or vice-versa, which is a problem because we have nowhere to
store the result of the conversion. These currently have fixed
place-holder "xyzzy" strings.
All UI strings are now wide.
Various format strings now use "%ls" and "%hs" to explicitly
specify wide and narrow. This doesn't play well with gcc, so
only the Windows-specific parts use those.
* Various updates to vcxproj files
The project-file conversion had some cruft that is now largely
gone. The build now has a common output directory for the EXEs
and libraries, avoiding the old post-build copy steps.
* Added zlib 1.2.8 and nufxlib 2.2.2 source snapshots
The old "prebuilts" directory is now gone. The libraries are now
built as part of building the apps.
I added a minimal set of files for zlib, and a full set for nufxlib.
The Linux-specific nufxlib goodies are included for the benefit of
the Linux utilities, which are currently broken (don't build).
* Replace symbols used for include guards
Symbols with a leading "__" are reserved.
2014-11-10 23:32:55 +00:00
|
|
|
pErrMsg->Format(L"Bad WAV file: 'fmt ' size is %d, struct is %d",
|
2014-11-04 00:26:53 +00:00
|
|
|
chunkLen, sizeof(WAVEFORMATEX));
|
|
|
|
err = -1;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
//memset(&mFormat, 0, sizeof(mFormat)); // done in constructor
|
|
|
|
if (fread(&mFormat, chunkLen, 1, mFP) != 1) {
|
|
|
|
err = errno ? errno : -1;
|
Large set of changes to restore CiderPress build.
CiderPress and MDC now compile, and execute far enough to open
their respective "about" boxes, but I doubt they'll do much
more than that.
* Switch from MBCS to UNICODE APIs
Microsoft switched to UTF-16 (by way of UCS-2) a long time ago,
and the support for MBCS seems to be getting phased out. So it's
time to switch to wide strings.
This is a bit awkward for CiderPress because it works with disk
and file archives with 8-bit filenames, and I want NufxLib and
DiskImgLib to continue to work on Linux (which has largely taken
the UTF-8 approach to Unicode). The libraries will continue to
work with 8-bit filenames, with CiderPress/MDC doing the
conversion at the appropriate point.
There were a couple of places where strings from a structure
handed back by one of the libraries were used directly in the UI,
or vice-versa, which is a problem because we have nowhere to
store the result of the conversion. These currently have fixed
place-holder "xyzzy" strings.
All UI strings are now wide.
Various format strings now use "%ls" and "%hs" to explicitly
specify wide and narrow. This doesn't play well with gcc, so
only the Windows-specific parts use those.
* Various updates to vcxproj files
The project-file conversion had some cruft that is now largely
gone. The build now has a common output directory for the EXEs
and libraries, avoiding the old post-build copy steps.
* Added zlib 1.2.8 and nufxlib 2.2.2 source snapshots
The old "prebuilts" directory is now gone. The libraries are now
built as part of building the apps.
I added a minimal set of files for zlib, and a full set for nufxlib.
The Linux-specific nufxlib goodies are included for the benefit of
the Linux utilities, which are currently broken (don't build).
* Replace symbols used for include guards
Symbols with a leading "__" are reserved.
2014-11-10 23:32:55 +00:00
|
|
|
*pErrMsg = L"Failed reading WAVEFORMATEX";
|
2014-11-04 00:26:53 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check the format for compatibility */
|
|
|
|
if (mFormat.wFormatTag != WAVE_FORMAT_PCM) {
|
Large set of changes to restore CiderPress build.
CiderPress and MDC now compile, and execute far enough to open
their respective "about" boxes, but I doubt they'll do much
more than that.
* Switch from MBCS to UNICODE APIs
Microsoft switched to UTF-16 (by way of UCS-2) a long time ago,
and the support for MBCS seems to be getting phased out. So it's
time to switch to wide strings.
This is a bit awkward for CiderPress because it works with disk
and file archives with 8-bit filenames, and I want NufxLib and
DiskImgLib to continue to work on Linux (which has largely taken
the UTF-8 approach to Unicode). The libraries will continue to
work with 8-bit filenames, with CiderPress/MDC doing the
conversion at the appropriate point.
There were a couple of places where strings from a structure
handed back by one of the libraries were used directly in the UI,
or vice-versa, which is a problem because we have nowhere to
store the result of the conversion. These currently have fixed
place-holder "xyzzy" strings.
All UI strings are now wide.
Various format strings now use "%ls" and "%hs" to explicitly
specify wide and narrow. This doesn't play well with gcc, so
only the Windows-specific parts use those.
* Various updates to vcxproj files
The project-file conversion had some cruft that is now largely
gone. The build now has a common output directory for the EXEs
and libraries, avoiding the old post-build copy steps.
* Added zlib 1.2.8 and nufxlib 2.2.2 source snapshots
The old "prebuilts" directory is now gone. The libraries are now
built as part of building the apps.
I added a minimal set of files for zlib, and a full set for nufxlib.
The Linux-specific nufxlib goodies are included for the benefit of
the Linux utilities, which are currently broken (don't build).
* Replace symbols used for include guards
Symbols with a leading "__" are reserved.
2014-11-10 23:32:55 +00:00
|
|
|
*pErrMsg = L"WAV file is not PCM format";
|
2014-11-04 00:26:53 +00:00
|
|
|
err = -1;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the "data" chunk, which holds the actual sound samples.
|
|
|
|
*/
|
|
|
|
err = SkipToHeader(MakeFourCC('d','a','t','a'), &chunkLen);
|
|
|
|
if (err != 0)
|
|
|
|
goto bail;
|
|
|
|
|
|
|
|
mSampleStart = ftell(mFP);
|
|
|
|
mSampleLen = chunkLen;
|
|
|
|
|
2014-11-25 22:34:14 +00:00
|
|
|
LOGD("WAV: chan=%d samples/sec=%d avgBPS=%d block=%d",
|
2014-11-04 00:26:53 +00:00
|
|
|
mFormat.nChannels, mFormat.nSamplesPerSec, mFormat.nAvgBytesPerSec,
|
|
|
|
mFormat.nBlockAlign);
|
2014-11-25 22:34:14 +00:00
|
|
|
LOGD(" bits/sample=%d [start=%d len=%d]", mFormat.wBitsPerSample,
|
2014-11-04 00:26:53 +00:00
|
|
|
mSampleStart, mSampleLen);
|
2007-03-27 17:47:10 +00:00
|
|
|
|
|
|
|
bail:
|
2014-11-04 00:26:53 +00:00
|
|
|
return err;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Skip forward until we find the named chunk. The file should be
|
|
|
|
* positioned immediately before the first chunk.
|
|
|
|
*/
|
2014-11-25 22:34:14 +00:00
|
|
|
int SoundFile::SkipToHeader(uint32_t hdrID, uint32_t* pChunkLen)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
struct {
|
2014-11-25 22:34:14 +00:00
|
|
|
uint32_t fourcc;
|
|
|
|
uint32_t chunkLen;
|
2014-11-04 00:26:53 +00:00
|
|
|
} chunkHeader;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
/* loop until we find the right chunk or run out of file */
|
|
|
|
while (true) {
|
|
|
|
if (fread(&chunkHeader, sizeof(chunkHeader), 1, mFP) != 1) {
|
|
|
|
err = errno ? errno : -1;
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("Failed searching for chunk header 0x%08lx", hdrID);
|
2014-11-04 00:26:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chunkHeader.fourcc == hdrID) {
|
|
|
|
*pChunkLen = chunkHeader.chunkLen;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* didn't match, skip contents */
|
|
|
|
if (fseek(mFP, chunkHeader.chunkLen, SEEK_CUR) != 0) {
|
|
|
|
err = errno;
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("Failed seeking past contents of 0x%08lx",
|
2014-11-04 00:26:53 +00:00
|
|
|
chunkHeader.chunkLen);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a block of data from the specified offset.
|
|
|
|
*/
|
2014-11-25 22:34:14 +00:00
|
|
|
int SoundFile::ReadData(void* buf, long sampleOffset, long len) const
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-25 22:34:14 +00:00
|
|
|
if ((uint32_t)(sampleOffset+len) > mSampleLen) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("ERROR: invalid read request (%d bytes, %d into %d)",
|
2014-11-04 00:26:53 +00:00
|
|
|
len, sampleOffset, mSampleLen);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fseek(mFP, mSampleStart+sampleOffset, SEEK_SET) != 0)
|
|
|
|
return errno;
|
|
|
|
|
|
|
|
if (fread(buf, len, 1, mFP) != 1) {
|
|
|
|
int err = errno ? errno : -1;
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("Failed reading %d bytes from sound file", len);
|
2014-11-04 00:26:53 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|