395 lines
11 KiB
C++
Raw Normal View History

2008-01-11 09:32:22 -08:00
/**
* OpenAL cross platform audio library
* Copyright (C) 1999-2007 by authors.
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2008-01-11 09:32:22 -08:00
* Or go to http://www.gnu.org/copyleft/lgpl.html
*/
#include "config.h"
#include "backends/wave.h"
2008-01-11 09:32:22 -08:00
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
2013-10-28 11:26:26 -07:00
#include <errno.h>
2012-09-14 02:14:29 -07:00
#include <chrono>
#include <thread>
2018-11-11 03:29:43 -08:00
#include <vector>
2008-01-11 09:32:22 -08:00
#include "alMain.h"
2012-09-14 02:14:29 -07:00
#include "alu.h"
#include "alconfig.h"
2013-10-28 12:05:33 -07:00
#include "compat.h"
2008-01-11 09:32:22 -08:00
2018-11-09 01:55:54 -08:00
namespace {
2008-01-11 09:32:22 -08:00
using std::chrono::seconds;
using std::chrono::milliseconds;
using std::chrono::nanoseconds;
2018-11-09 01:55:54 -08:00
constexpr ALCchar waveDevice[] = "Wave File Writer";
constexpr ALubyte SUBTYPE_PCM[]{
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa,
0x00, 0x38, 0x9b, 0x71
};
2018-11-09 01:55:54 -08:00
constexpr ALubyte SUBTYPE_FLOAT[]{
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa,
0x00, 0x38, 0x9b, 0x71
};
2018-11-09 01:55:54 -08:00
constexpr ALubyte SUBTYPE_BFORMAT_PCM[]{
0x01, 0x00, 0x00, 0x00, 0x21, 0x07, 0xd3, 0x11, 0x86, 0x44, 0xc8, 0xc1,
0xca, 0x00, 0x00, 0x00
};
2018-11-09 01:55:54 -08:00
constexpr ALubyte SUBTYPE_BFORMAT_FLOAT[]{
0x03, 0x00, 0x00, 0x00, 0x21, 0x07, 0xd3, 0x11, 0x86, 0x44, 0xc8, 0xc1,
0xca, 0x00, 0x00, 0x00
};
2018-11-09 01:55:54 -08:00
void fwrite16le(ALushort val, FILE *f)
{
2018-11-09 01:55:54 -08:00
ALubyte data[2]{ static_cast<ALubyte>(val&0xff), static_cast<ALubyte>((val>>8)&0xff) };
fwrite(data, 1, 2, f);
}
2018-11-09 01:55:54 -08:00
void fwrite32le(ALuint val, FILE *f)
{
2018-11-09 01:55:54 -08:00
ALubyte data[4]{ static_cast<ALubyte>(val&0xff), static_cast<ALubyte>((val>>8)&0xff),
static_cast<ALubyte>((val>>16)&0xff), static_cast<ALubyte>((val>>24)&0xff) };
fwrite(data, 1, 4, f);
}
struct WaveBackend final : public BackendBase {
WaveBackend(ALCdevice *device) noexcept : BackendBase{device} { }
~WaveBackend() override;
int mixerProc();
ALCenum open(const ALCchar *name) override;
ALCboolean reset() override;
ALCboolean start() override;
void stop() override;
2018-11-26 14:31:54 -08:00
FILE *mFile{nullptr};
long mDataStart{-1};
al::vector<ALbyte> mBuffer;
2018-11-26 17:31:04 -08:00
std::atomic<ALenum> mKillNow{AL_TRUE};
std::thread mThread;
static constexpr inline const char *CurrentPrefix() noexcept { return "WaveBackend::"; }
DEF_NEWDEL(WaveBackend)
2018-11-09 01:55:54 -08:00
};
WaveBackend::~WaveBackend()
{
if(mFile)
fclose(mFile);
mFile = nullptr;
}
int WaveBackend::mixerProc()
2008-01-11 09:32:22 -08:00
{
const milliseconds restTime{mDevice->UpdateSize*1000/mDevice->Frequency / 2};
2008-01-11 09:32:22 -08:00
althrd_setname(MIXER_THREAD_NAME);
const ALsizei frameSize{mDevice->frameSizeFromFmt()};
2008-01-11 09:32:22 -08:00
ALint64 done{0};
auto start = std::chrono::steady_clock::now();
while(!mKillNow.load(std::memory_order_acquire) &&
mDevice->Connected.load(std::memory_order_acquire))
{
auto now = std::chrono::steady_clock::now();
/* This converts from nanoseconds to nanosamples, then to samples. */
ALint64 avail{std::chrono::duration_cast<seconds>((now-start) *
mDevice->Frequency).count()};
if(avail-done < mDevice->UpdateSize)
2008-01-11 09:32:22 -08:00
{
std::this_thread::sleep_for(restTime);
continue;
2008-01-11 09:32:22 -08:00
}
while(avail-done >= mDevice->UpdateSize)
2008-01-11 09:32:22 -08:00
{
lock();
aluMixData(mDevice, mBuffer.data(), mDevice->UpdateSize);
unlock();
done += mDevice->UpdateSize;
2008-01-11 09:32:22 -08:00
2012-02-15 21:47:35 -08:00
if(!IS_LITTLE_ENDIAN)
2008-01-11 09:32:22 -08:00
{
const ALsizei bytesize{mDevice->bytesFromFmt()};
2018-12-12 21:18:31 -08:00
ALsizei i;
2008-01-11 09:32:22 -08:00
if(bytesize == 2)
2010-03-24 16:58:58 -07:00
{
ALushort *samples = reinterpret_cast<ALushort*>(mBuffer.data());
const auto len = static_cast<ALsizei>(mBuffer.size() / 2);
for(i = 0;i < len;i++)
{
ALushort samp = samples[i];
samples[i] = (samp>>8) | (samp<<8);
}
}
else if(bytesize == 4)
{
ALuint *samples = reinterpret_cast<ALuint*>(mBuffer.data());
const auto len = static_cast<ALsizei>(mBuffer.size() / 4);
for(i = 0;i < len;i++)
{
ALuint samp = samples[i];
samples[i] = (samp>>24) | ((samp>>8)&0x0000ff00) |
((samp<<8)&0x00ff0000) | (samp<<24);
}
}
2008-01-11 09:32:22 -08:00
}
size_t fs{fwrite(mBuffer.data(), frameSize, mDevice->UpdateSize, mFile)};
(void)fs;
if(ferror(mFile))
2008-01-11 09:32:22 -08:00
{
2011-07-13 01:43:00 -07:00
ERR("Error writing to file\n");
lock();
aluHandleDisconnect(mDevice, "Failed to write playback samples");
unlock();
2008-01-11 09:32:22 -08:00
break;
}
}
/* For every completed second, increment the start time and reduce the
* samples done. This prevents the difference between the start time
* and current time from growing too large, while maintaining the
* correct number of samples to render.
*/
if(done >= mDevice->Frequency)
{
seconds s{done/mDevice->Frequency};
start += s;
done -= mDevice->Frequency*s.count();
}
2008-01-11 09:32:22 -08:00
}
return 0;
}
ALCenum WaveBackend::open(const ALCchar *name)
2008-01-11 09:32:22 -08:00
{
2018-11-10 04:27:10 -08:00
const char *fname{GetConfigValue(nullptr, "wave", "file", "")};
if(!fname[0]) return ALC_INVALID_VALUE;
2008-01-11 09:32:22 -08:00
if(!name)
name = waveDevice;
else if(strcmp(name, waveDevice) != 0)
return ALC_INVALID_VALUE;
2008-01-11 09:32:22 -08:00
2018-11-10 04:27:10 -08:00
#ifdef _WIN32
{
std::wstring wname = utf8_to_wstr(fname);
mFile = _wfopen(wname.c_str(), L"wb");
2018-11-10 04:27:10 -08:00
}
#else
mFile = fopen(fname, "wb");
2018-11-10 04:27:10 -08:00
#endif
if(!mFile)
2008-01-11 09:32:22 -08:00
{
2011-07-13 01:43:00 -07:00
ERR("Could not open file '%s': %s\n", fname, strerror(errno));
return ALC_INVALID_VALUE;
2008-01-11 09:32:22 -08:00
}
mDevice->DeviceName = name;
return ALC_NO_ERROR;
}
ALCboolean WaveBackend::reset()
{
ALuint channels=0, bits=0, chanmask=0;
int isbformat = 0;
size_t val;
fseek(mFile, 0, SEEK_SET);
clearerr(mFile);
2018-11-09 01:55:54 -08:00
if(GetConfigValueBool(nullptr, "wave", "bformat", 0))
{
mDevice->FmtChans = DevFmtAmbi3D;
mDevice->mAmbiOrder = 1;
}
switch(mDevice->FmtType)
{
case DevFmtByte:
mDevice->FmtType = DevFmtUByte;
break;
case DevFmtUShort:
mDevice->FmtType = DevFmtShort;
break;
case DevFmtUInt:
mDevice->FmtType = DevFmtInt;
break;
case DevFmtUByte:
case DevFmtShort:
case DevFmtInt:
case DevFmtFloat:
break;
2008-01-11 09:32:22 -08:00
}
switch(mDevice->FmtChans)
{
case DevFmtMono: chanmask = 0x04; break;
case DevFmtStereo: chanmask = 0x01 | 0x02; break;
case DevFmtQuad: chanmask = 0x01 | 0x02 | 0x10 | 0x20; break;
case DevFmtX51: chanmask = 0x01 | 0x02 | 0x04 | 0x08 | 0x200 | 0x400; break;
case DevFmtX51Rear: chanmask = 0x01 | 0x02 | 0x04 | 0x08 | 0x010 | 0x020; break;
case DevFmtX61: chanmask = 0x01 | 0x02 | 0x04 | 0x08 | 0x100 | 0x200 | 0x400; break;
case DevFmtX71: chanmask = 0x01 | 0x02 | 0x04 | 0x08 | 0x010 | 0x020 | 0x200 | 0x400; break;
case DevFmtAmbi3D:
/* .amb output requires FuMa */
mDevice->mAmbiOrder = mini(mDevice->mAmbiOrder, 3);
mDevice->mAmbiLayout = AmbiLayout::FuMa;
mDevice->mAmbiScale = AmbiNorm::FuMa;
isbformat = 1;
chanmask = 0;
break;
}
bits = mDevice->bytesFromFmt() * 8;
channels = mDevice->channelsFromFmt();
2008-01-11 09:32:22 -08:00
fputs("RIFF", mFile);
fwrite32le(0xFFFFFFFF, mFile); // 'RIFF' header len; filled in at close
2008-01-11 09:32:22 -08:00
fputs("WAVE", mFile);
2008-01-11 09:32:22 -08:00
fputs("fmt ", mFile);
fwrite32le(40, mFile); // 'fmt ' header len; 40 bytes for EXTENSIBLE
// 16-bit val, format type id (extensible: 0xFFFE)
fwrite16le(0xFFFE, mFile);
2008-01-11 09:32:22 -08:00
// 16-bit val, channel count
fwrite16le(channels, mFile);
2008-01-11 09:32:22 -08:00
// 32-bit val, frequency
fwrite32le(mDevice->Frequency, mFile);
2008-01-11 09:32:22 -08:00
// 32-bit val, bytes per second
fwrite32le(mDevice->Frequency * channels * bits / 8, mFile);
2008-01-11 09:32:22 -08:00
// 16-bit val, frame size
fwrite16le(channels * bits / 8, mFile);
2008-01-11 09:32:22 -08:00
// 16-bit val, bits per sample
fwrite16le(bits, mFile);
// 16-bit val, extra byte count
fwrite16le(22, mFile);
// 16-bit val, valid bits per sample
fwrite16le(bits, mFile);
// 32-bit val, channel mask
fwrite32le(chanmask, mFile);
// 16 byte GUID, sub-type format
val = fwrite((mDevice->FmtType == DevFmtFloat) ?
(isbformat ? SUBTYPE_BFORMAT_FLOAT : SUBTYPE_FLOAT) :
(isbformat ? SUBTYPE_BFORMAT_PCM : SUBTYPE_PCM), 1, 16, mFile);
2013-06-05 01:52:49 -07:00
(void)val;
2008-01-11 09:32:22 -08:00
fputs("data", mFile);
fwrite32le(0xFFFFFFFF, mFile); // 'data' header len; filled in at close
2008-01-11 09:32:22 -08:00
if(ferror(mFile))
2008-01-11 09:32:22 -08:00
{
2011-07-13 01:43:00 -07:00
ERR("Error writing header: %s\n", strerror(errno));
2008-01-11 09:32:22 -08:00
return ALC_FALSE;
}
mDataStart = ftell(mFile);
SetDefaultWFXChannelOrder(mDevice);
const ALuint bufsize{mDevice->frameSizeFromFmt() * mDevice->UpdateSize};
mBuffer.resize(bufsize);
2018-11-11 03:29:43 -08:00
return ALC_TRUE;
}
ALCboolean WaveBackend::start()
{
try {
mKillNow.store(AL_FALSE, std::memory_order_release);
mThread = std::thread{std::mem_fn(&WaveBackend::mixerProc), this};
return ALC_TRUE;
}
catch(std::exception& e) {
ERR("Failed to start mixing thread: %s\n", e.what());
}
catch(...) {
2008-01-11 09:32:22 -08:00
}
return ALC_FALSE;
2008-01-11 09:32:22 -08:00
}
void WaveBackend::stop()
2008-01-11 09:32:22 -08:00
{
if(mKillNow.exchange(AL_TRUE, std::memory_order_acq_rel) || !mThread.joinable())
return;
mThread.join();
long size{ftell(mFile)};
2008-01-11 09:32:22 -08:00
if(size > 0)
{
long dataLen{size - mDataStart};
if(fseek(mFile, mDataStart-4, SEEK_SET) == 0)
fwrite32le(dataLen, mFile); // 'data' header len
if(fseek(mFile, 4, SEEK_SET) == 0)
fwrite32le(size-8, mFile); // 'WAVE' header len
2008-01-11 09:32:22 -08:00
}
}
} // namespace
bool WaveBackendFactory::init()
{ return true; }
2009-08-26 23:45:00 -07:00
2018-12-29 01:38:26 -08:00
bool WaveBackendFactory::querySupport(BackendType type)
{ return type == BackendType::Playback; }
void WaveBackendFactory::probe(DevProbe type, std::string *outnames)
{
switch(type)
{
case ALL_DEVICE_PROBE:
/* Includes null char. */
outnames->append(waveDevice, sizeof(waveDevice));
break;
case CAPTURE_DEVICE_PROBE:
break;
}
}
2018-12-29 02:16:16 -08:00
BackendPtr WaveBackendFactory::createBackend(ALCdevice *device, BackendType type)
{
2018-12-29 01:38:26 -08:00
if(type == BackendType::Playback)
2018-12-29 02:16:16 -08:00
return BackendPtr{new WaveBackend{device}};
2018-11-09 01:55:54 -08:00
return nullptr;
}
BackendFactory &WaveBackendFactory::getFactory()
2018-11-09 01:55:54 -08:00
{
static WaveBackendFactory factory{};
return factory;
2009-08-26 23:45:00 -07:00
}