ciderpress/util/SoundFile.cpp

223 lines
6.3 KiB
C++
Raw Normal View History

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.
*/
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
{
/* little-endian */
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.
*/
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;
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) {
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);
return err;
}
2007-03-27 17:47:10 +00:00
fseek(fp, 0, SEEK_END);
fileLen = ftell(fp);
rewind(fp);
2007-03-27 17:47:10 +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.
*/
int SoundFile::Create(FILE* fp, long len, bool doClose, CString* pErrMsg)
2007-03-27 17:47:10 +00:00
{
struct {
uint32_t riff;
uint32_t fileLen;
uint32_t wav;
} fileHeader;
uint32_t chunkLen;
int err = 0;
2014-11-18 05:13:13 +00:00
if (mFP != NULL) {
LOGW("SoundFile object already created");
assert(false);
return -1;
}
2014-11-18 05:13:13 +00:00
if (fp == NULL)
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";
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";
goto bail;
}
if (fileHeader.riff != MakeFourCC('R','I','F','F') ||
fileHeader.wav != MakeFourCC('W','A','V','E') ||
fileHeader.fileLen > (uint32_t) len)
{
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";
LOGI("Not a valid WAV header (0x%08lx %d 0x%08lx)",
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",
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";
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";
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;
LOGD("WAV: chan=%d samples/sec=%d avgBPS=%d block=%d",
mFormat.nChannels, mFormat.nSamplesPerSec, mFormat.nAvgBytesPerSec,
mFormat.nBlockAlign);
LOGD(" bits/sample=%d [start=%d len=%d]", mFormat.wBitsPerSample,
mSampleStart, mSampleLen);
2007-03-27 17:47:10 +00:00
bail:
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.
*/
int SoundFile::SkipToHeader(uint32_t hdrID, uint32_t* pChunkLen)
2007-03-27 17:47:10 +00:00
{
struct {
uint32_t fourcc;
uint32_t chunkLen;
} 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;
LOGI("Failed searching for chunk header 0x%08lx", hdrID);
break;
}
if (chunkHeader.fourcc == hdrID) {
*pChunkLen = chunkHeader.chunkLen;
break;
}
/* didn't match, skip contents */
if (fseek(mFP, chunkHeader.chunkLen, SEEK_CUR) != 0) {
err = errno;
LOGI("Failed seeking past contents of 0x%08lx",
chunkHeader.chunkLen);
break;
}
}
return err;
2007-03-27 17:47:10 +00:00
}
/*
* Read a block of data from the specified offset.
*/
int SoundFile::ReadData(void* buf, long sampleOffset, long len) const
2007-03-27 17:47:10 +00:00
{
if ((uint32_t)(sampleOffset+len) > mSampleLen) {
LOGI("ERROR: invalid read request (%d bytes, %d into %d)",
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;
LOGI("Failed reading %d bytes from sound file", len);
return err;
}
return 0;
2007-03-27 17:47:10 +00:00
}