2021-01-26 19:01:35 +00: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.
|
|
|
|
|
* Or go to http://www.gnu.org/copyleft/lgpl.html
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
2022-05-30 20:32:45 +00:00
|
|
|
#include "wave.h"
|
2021-01-26 19:01:35 +00:00
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <atomic>
|
|
|
|
|
#include <cerrno>
|
|
|
|
|
#include <chrono>
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
#include <exception>
|
2024-06-30 19:35:57 +00:00
|
|
|
#include <system_error>
|
2021-01-26 19:01:35 +00:00
|
|
|
#include <thread>
|
2024-06-30 19:35:57 +00:00
|
|
|
#include <vector>
|
2021-01-26 19:01:35 +00:00
|
|
|
|
|
|
|
|
#include "albit.h"
|
2022-05-30 20:32:45 +00:00
|
|
|
#include "alc/alconfig.h"
|
2021-01-26 19:01:35 +00:00
|
|
|
#include "almalloc.h"
|
|
|
|
|
#include "alnumeric.h"
|
2024-06-30 19:35:57 +00:00
|
|
|
#include "althrd_setname.h"
|
2022-05-30 20:32:45 +00:00
|
|
|
#include "core/device.h"
|
2021-01-26 19:01:35 +00:00
|
|
|
#include "core/logging.h"
|
|
|
|
|
#include "strutils.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2024-06-30 19:35:57 +00:00
|
|
|
using namespace std::string_view_literals;
|
2021-01-26 19:01:35 +00:00
|
|
|
using std::chrono::seconds;
|
|
|
|
|
using std::chrono::milliseconds;
|
|
|
|
|
using std::chrono::nanoseconds;
|
|
|
|
|
|
|
|
|
|
using ubyte = unsigned char;
|
|
|
|
|
using ushort = unsigned short;
|
|
|
|
|
|
2024-06-30 19:35:57 +00:00
|
|
|
struct FileDeleter {
|
|
|
|
|
void operator()(gsl::owner<FILE*> f) { fclose(f); }
|
|
|
|
|
};
|
|
|
|
|
using FilePtr = std::unique_ptr<FILE,FileDeleter>;
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto GetDeviceName() noexcept { return "Wave File Writer"sv; }
|
2021-01-26 19:01:35 +00:00
|
|
|
|
2024-06-30 19:35:57 +00:00
|
|
|
constexpr std::array<ubyte,16> SUBTYPE_PCM{{
|
2021-01-26 19:01:35 +00:00
|
|
|
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa,
|
|
|
|
|
0x00, 0x38, 0x9b, 0x71
|
2024-06-30 19:35:57 +00:00
|
|
|
}};
|
|
|
|
|
constexpr std::array<ubyte,16> SUBTYPE_FLOAT{{
|
2021-01-26 19:01:35 +00:00
|
|
|
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa,
|
|
|
|
|
0x00, 0x38, 0x9b, 0x71
|
2024-06-30 19:35:57 +00:00
|
|
|
}};
|
2021-01-26 19:01:35 +00:00
|
|
|
|
2024-06-30 19:35:57 +00:00
|
|
|
constexpr std::array<ubyte,16> SUBTYPE_BFORMAT_PCM{{
|
2021-01-26 19:01:35 +00:00
|
|
|
0x01, 0x00, 0x00, 0x00, 0x21, 0x07, 0xd3, 0x11, 0x86, 0x44, 0xc8, 0xc1,
|
|
|
|
|
0xca, 0x00, 0x00, 0x00
|
2024-06-30 19:35:57 +00:00
|
|
|
}};
|
2021-01-26 19:01:35 +00:00
|
|
|
|
2024-06-30 19:35:57 +00:00
|
|
|
constexpr std::array<ubyte,16> SUBTYPE_BFORMAT_FLOAT{{
|
2021-01-26 19:01:35 +00:00
|
|
|
0x03, 0x00, 0x00, 0x00, 0x21, 0x07, 0xd3, 0x11, 0x86, 0x44, 0xc8, 0xc1,
|
|
|
|
|
0xca, 0x00, 0x00, 0x00
|
2024-06-30 19:35:57 +00:00
|
|
|
}};
|
2021-01-26 19:01:35 +00:00
|
|
|
|
|
|
|
|
void fwrite16le(ushort val, FILE *f)
|
|
|
|
|
{
|
2024-06-30 19:35:57 +00:00
|
|
|
std::array data{static_cast<ubyte>(val&0xff), static_cast<ubyte>((val>>8)&0xff)};
|
|
|
|
|
fwrite(data.data(), 1, data.size(), f);
|
2021-01-26 19:01:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void fwrite32le(uint val, FILE *f)
|
|
|
|
|
{
|
2024-06-30 19:35:57 +00:00
|
|
|
std::array data{static_cast<ubyte>(val&0xff), static_cast<ubyte>((val>>8)&0xff),
|
|
|
|
|
static_cast<ubyte>((val>>16)&0xff), static_cast<ubyte>((val>>24)&0xff)};
|
|
|
|
|
fwrite(data.data(), 1, data.size(), f);
|
2021-01-26 19:01:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct WaveBackend final : public BackendBase {
|
2025-09-03 16:09:27 +00:00
|
|
|
explicit WaveBackend(DeviceBase *device) noexcept : BackendBase{device} { }
|
2021-01-26 19:01:35 +00:00
|
|
|
~WaveBackend() override;
|
|
|
|
|
|
|
|
|
|
int mixerProc();
|
|
|
|
|
|
2024-06-30 19:35:57 +00:00
|
|
|
void open(std::string_view name) override;
|
2021-01-26 19:01:35 +00:00
|
|
|
bool reset() override;
|
|
|
|
|
void start() override;
|
|
|
|
|
void stop() override;
|
|
|
|
|
|
2024-06-30 19:35:57 +00:00
|
|
|
FilePtr mFile{nullptr};
|
2021-01-26 19:01:35 +00:00
|
|
|
long mDataStart{-1};
|
|
|
|
|
|
2024-06-30 19:35:57 +00:00
|
|
|
std::vector<std::byte> mBuffer;
|
2021-01-26 19:01:35 +00:00
|
|
|
|
|
|
|
|
std::atomic<bool> mKillNow{true};
|
|
|
|
|
std::thread mThread;
|
|
|
|
|
};
|
|
|
|
|
|
2024-06-30 19:35:57 +00:00
|
|
|
WaveBackend::~WaveBackend() = default;
|
2021-01-26 19:01:35 +00:00
|
|
|
|
|
|
|
|
int WaveBackend::mixerProc()
|
|
|
|
|
{
|
2025-09-03 16:09:27 +00:00
|
|
|
const milliseconds restTime{mDevice->mUpdateSize*1000/mDevice->mSampleRate / 2};
|
2021-01-26 19:01:35 +00:00
|
|
|
|
2024-06-30 19:35:57 +00:00
|
|
|
althrd_setname(GetMixerThreadName());
|
2021-01-26 19:01:35 +00:00
|
|
|
|
|
|
|
|
const size_t frameStep{mDevice->channelsFromFmt()};
|
|
|
|
|
const size_t frameSize{mDevice->frameSizeFromFmt()};
|
|
|
|
|
|
|
|
|
|
int64_t done{0};
|
|
|
|
|
auto start = std::chrono::steady_clock::now();
|
2022-05-30 20:32:45 +00:00
|
|
|
while(!mKillNow.load(std::memory_order_acquire)
|
|
|
|
|
&& mDevice->Connected.load(std::memory_order_acquire))
|
2021-01-26 19:01:35 +00:00
|
|
|
{
|
|
|
|
|
auto now = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
|
|
/* This converts from nanoseconds to nanosamples, then to samples. */
|
2025-09-03 16:09:27 +00:00
|
|
|
const auto avail = int64_t{std::chrono::duration_cast<seconds>((now-start) *
|
|
|
|
|
mDevice->mSampleRate).count()};
|
|
|
|
|
if(avail-done < mDevice->mUpdateSize)
|
2021-01-26 19:01:35 +00:00
|
|
|
{
|
|
|
|
|
std::this_thread::sleep_for(restTime);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2025-09-03 16:09:27 +00:00
|
|
|
while(avail-done >= mDevice->mUpdateSize)
|
2021-01-26 19:01:35 +00:00
|
|
|
{
|
2025-09-03 16:09:27 +00:00
|
|
|
mDevice->renderSamples(mBuffer.data(), mDevice->mUpdateSize, frameStep);
|
|
|
|
|
done += mDevice->mUpdateSize;
|
2021-01-26 19:01:35 +00:00
|
|
|
|
2022-05-30 20:32:45 +00:00
|
|
|
if(al::endian::native != al::endian::little)
|
2021-01-26 19:01:35 +00:00
|
|
|
{
|
|
|
|
|
const uint bytesize{mDevice->bytesFromFmt()};
|
|
|
|
|
|
|
|
|
|
if(bytesize == 2)
|
|
|
|
|
{
|
2024-06-30 19:35:57 +00:00
|
|
|
const size_t len{mBuffer.size() & ~1_uz};
|
2022-05-30 20:32:45 +00:00
|
|
|
for(size_t i{0};i < len;i+=2)
|
|
|
|
|
std::swap(mBuffer[i], mBuffer[i+1]);
|
2021-01-26 19:01:35 +00:00
|
|
|
}
|
|
|
|
|
else if(bytesize == 4)
|
|
|
|
|
{
|
2024-06-30 19:35:57 +00:00
|
|
|
const size_t len{mBuffer.size() & ~3_uz};
|
2022-05-30 20:32:45 +00:00
|
|
|
for(size_t i{0};i < len;i+=4)
|
2021-01-26 19:01:35 +00:00
|
|
|
{
|
2022-05-30 20:32:45 +00:00
|
|
|
std::swap(mBuffer[i ], mBuffer[i+3]);
|
|
|
|
|
std::swap(mBuffer[i+1], mBuffer[i+2]);
|
2021-01-26 19:01:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-03 16:09:27 +00:00
|
|
|
const size_t fs{fwrite(mBuffer.data(), frameSize, mDevice->mUpdateSize, mFile.get())};
|
|
|
|
|
if(fs < mDevice->mUpdateSize || ferror(mFile.get()))
|
2021-01-26 19:01:35 +00:00
|
|
|
{
|
2025-09-03 16:09:27 +00:00
|
|
|
ERR("Error writing to file");
|
2021-01-26 19:01:35 +00:00
|
|
|
mDevice->handleDisconnect("Failed to write playback samples");
|
|
|
|
|
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.
|
|
|
|
|
*/
|
2025-09-03 16:09:27 +00:00
|
|
|
if(done >= mDevice->mSampleRate)
|
2021-01-26 19:01:35 +00:00
|
|
|
{
|
2025-09-03 16:09:27 +00:00
|
|
|
seconds s{done/mDevice->mSampleRate};
|
|
|
|
|
done %= mDevice->mSampleRate;
|
2021-01-26 19:01:35 +00:00
|
|
|
start += s;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-30 19:35:57 +00:00
|
|
|
void WaveBackend::open(std::string_view name)
|
2021-01-26 19:01:35 +00:00
|
|
|
{
|
2024-06-30 19:35:57 +00:00
|
|
|
auto fname = ConfigValueStr({}, "wave", "file");
|
2022-05-30 20:32:45 +00:00
|
|
|
if(!fname) throw al::backend_exception{al::backend_error::NoDevice,
|
2021-01-26 19:01:35 +00:00
|
|
|
"No wave output filename"};
|
|
|
|
|
|
2024-06-30 19:35:57 +00:00
|
|
|
if(name.empty())
|
|
|
|
|
name = GetDeviceName();
|
|
|
|
|
else if(name != GetDeviceName())
|
2025-09-03 16:09:27 +00:00
|
|
|
throw al::backend_exception{al::backend_error::NoDevice, "Device name \"{}\" not found",
|
|
|
|
|
name};
|
2021-01-26 19:01:35 +00:00
|
|
|
|
2022-05-30 20:32:45 +00:00
|
|
|
/* There's only one "device", so if it's already open, we're done. */
|
|
|
|
|
if(mFile) return;
|
|
|
|
|
|
2021-01-26 19:01:35 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
{
|
2024-06-30 19:35:57 +00:00
|
|
|
std::wstring wname{utf8_to_wstr(fname.value())};
|
|
|
|
|
mFile = FilePtr{_wfopen(wname.c_str(), L"wb")};
|
2021-01-26 19:01:35 +00:00
|
|
|
}
|
|
|
|
|
#else
|
2024-06-30 19:35:57 +00:00
|
|
|
mFile = FilePtr{fopen(fname->c_str(), "wb")};
|
2021-01-26 19:01:35 +00:00
|
|
|
#endif
|
|
|
|
|
if(!mFile)
|
2025-09-03 16:09:27 +00:00
|
|
|
throw al::backend_exception{al::backend_error::DeviceError, "Could not open file '{}': {}",
|
|
|
|
|
*fname, std::generic_category().message(errno)};
|
2021-01-26 19:01:35 +00:00
|
|
|
|
2025-09-03 16:09:27 +00:00
|
|
|
mDeviceName = name;
|
2021-01-26 19:01:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool WaveBackend::reset()
|
|
|
|
|
{
|
2024-06-30 19:35:57 +00:00
|
|
|
if(GetConfigValueBool({}, "wave", "bformat", false))
|
2021-01-26 19:01:35 +00:00
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
2025-09-03 16:09:27 +00:00
|
|
|
auto chanmask = 0u;
|
|
|
|
|
auto isbformat = false;
|
2021-01-26 19:01:35 +00: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 DevFmtX61: chanmask = 0x01 | 0x02 | 0x04 | 0x08 | 0x100 | 0x200 | 0x400; break;
|
|
|
|
|
case DevFmtX71: chanmask = 0x01 | 0x02 | 0x04 | 0x08 | 0x010 | 0x020 | 0x200 | 0x400; break;
|
2024-06-30 19:35:57 +00:00
|
|
|
case DevFmtX7144:
|
|
|
|
|
mDevice->FmtChans = DevFmtX714;
|
|
|
|
|
[[fallthrough]];
|
2024-03-21 17:33:47 +00:00
|
|
|
case DevFmtX714:
|
|
|
|
|
chanmask = 0x01 | 0x02 | 0x04 | 0x08 | 0x010 | 0x020 | 0x200 | 0x400 | 0x1000 | 0x4000
|
|
|
|
|
| 0x8000 | 0x20000;
|
|
|
|
|
break;
|
|
|
|
|
/* NOTE: Same as 7.1. */
|
|
|
|
|
case DevFmtX3D71: chanmask = 0x01 | 0x02 | 0x04 | 0x08 | 0x010 | 0x020 | 0x200 | 0x400; break;
|
2021-01-26 19:01:35 +00:00
|
|
|
case DevFmtAmbi3D:
|
|
|
|
|
/* .amb output requires FuMa */
|
2024-06-30 19:35:57 +00:00
|
|
|
mDevice->mAmbiOrder = std::min(mDevice->mAmbiOrder, 3u);
|
2021-01-26 19:01:35 +00:00
|
|
|
mDevice->mAmbiLayout = DevAmbiLayout::FuMa;
|
|
|
|
|
mDevice->mAmbiScale = DevAmbiScaling::FuMa;
|
|
|
|
|
isbformat = true;
|
|
|
|
|
chanmask = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-09-03 16:09:27 +00:00
|
|
|
const auto bytes = mDevice->bytesFromFmt();
|
|
|
|
|
const auto channels = mDevice->channelsFromFmt();
|
2021-01-26 19:01:35 +00:00
|
|
|
|
2025-09-03 16:09:27 +00:00
|
|
|
if(fseek(mFile.get(), 0, SEEK_CUR) != 0)
|
|
|
|
|
{
|
|
|
|
|
/* ESPIPE means the underlying file isn't seekable, which is fine for
|
|
|
|
|
* piped output.
|
|
|
|
|
*/
|
|
|
|
|
if(auto errcode = errno; errcode != ESPIPE)
|
|
|
|
|
{
|
|
|
|
|
ERR("Failed to reset file offset: {} ({})", std::generic_category().message(errcode),
|
|
|
|
|
errcode);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
clearerr(mFile.get());
|
2021-01-26 19:01:35 +00:00
|
|
|
|
2024-06-30 19:35:57 +00:00
|
|
|
fputs("RIFF", mFile.get());
|
2025-09-03 16:09:27 +00:00
|
|
|
fwrite32le(0xFFFFFFFF, mFile.get()); // 'RIFF' header len; filled in at stop
|
2021-01-26 19:01:35 +00:00
|
|
|
|
2024-06-30 19:35:57 +00:00
|
|
|
fputs("WAVE", mFile.get());
|
2021-01-26 19:01:35 +00:00
|
|
|
|
2024-06-30 19:35:57 +00:00
|
|
|
fputs("fmt ", mFile.get());
|
|
|
|
|
fwrite32le(40, mFile.get()); // 'fmt ' header len; 40 bytes for EXTENSIBLE
|
2021-01-26 19:01:35 +00:00
|
|
|
|
|
|
|
|
// 16-bit val, format type id (extensible: 0xFFFE)
|
2024-06-30 19:35:57 +00:00
|
|
|
fwrite16le(0xFFFE, mFile.get());
|
2021-01-26 19:01:35 +00:00
|
|
|
// 16-bit val, channel count
|
2024-06-30 19:35:57 +00:00
|
|
|
fwrite16le(static_cast<ushort>(channels), mFile.get());
|
2021-01-26 19:01:35 +00:00
|
|
|
// 32-bit val, frequency
|
2025-09-03 16:09:27 +00:00
|
|
|
fwrite32le(mDevice->mSampleRate, mFile.get());
|
2021-01-26 19:01:35 +00:00
|
|
|
// 32-bit val, bytes per second
|
2025-09-03 16:09:27 +00:00
|
|
|
fwrite32le(mDevice->mSampleRate * channels * bytes, mFile.get());
|
2021-01-26 19:01:35 +00:00
|
|
|
// 16-bit val, frame size
|
2024-06-30 19:35:57 +00:00
|
|
|
fwrite16le(static_cast<ushort>(channels * bytes), mFile.get());
|
2021-01-26 19:01:35 +00:00
|
|
|
// 16-bit val, bits per sample
|
2024-06-30 19:35:57 +00:00
|
|
|
fwrite16le(static_cast<ushort>(bytes * 8), mFile.get());
|
2021-01-26 19:01:35 +00:00
|
|
|
// 16-bit val, extra byte count
|
2024-06-30 19:35:57 +00:00
|
|
|
fwrite16le(22, mFile.get());
|
2021-01-26 19:01:35 +00:00
|
|
|
// 16-bit val, valid bits per sample
|
2024-06-30 19:35:57 +00:00
|
|
|
fwrite16le(static_cast<ushort>(bytes * 8), mFile.get());
|
2021-01-26 19:01:35 +00:00
|
|
|
// 32-bit val, channel mask
|
2024-06-30 19:35:57 +00:00
|
|
|
fwrite32le(chanmask, mFile.get());
|
2021-01-26 19:01:35 +00:00
|
|
|
// 16 byte GUID, sub-type format
|
2024-06-30 19:35:57 +00:00
|
|
|
std::ignore = fwrite((mDevice->FmtType == DevFmtFloat) ?
|
|
|
|
|
(isbformat ? SUBTYPE_BFORMAT_FLOAT.data() : SUBTYPE_FLOAT.data()) :
|
|
|
|
|
(isbformat ? SUBTYPE_BFORMAT_PCM.data() : SUBTYPE_PCM.data()), 1, 16, mFile.get());
|
2021-01-26 19:01:35 +00:00
|
|
|
|
2024-06-30 19:35:57 +00:00
|
|
|
fputs("data", mFile.get());
|
2025-09-03 16:09:27 +00:00
|
|
|
fwrite32le(0xFFFFFFFF, mFile.get()); // 'data' header len; filled in at stop
|
2021-01-26 19:01:35 +00:00
|
|
|
|
2024-06-30 19:35:57 +00:00
|
|
|
if(ferror(mFile.get()))
|
2021-01-26 19:01:35 +00:00
|
|
|
{
|
2025-09-03 16:09:27 +00:00
|
|
|
ERR("Error writing header: {}", std::generic_category().message(errno));
|
2021-01-26 19:01:35 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2024-06-30 19:35:57 +00:00
|
|
|
mDataStart = ftell(mFile.get());
|
2021-01-26 19:01:35 +00:00
|
|
|
|
|
|
|
|
setDefaultWFXChannelOrder();
|
|
|
|
|
|
2025-09-03 16:09:27 +00:00
|
|
|
const uint bufsize{mDevice->frameSizeFromFmt() * mDevice->mUpdateSize};
|
2021-01-26 19:01:35 +00:00
|
|
|
mBuffer.resize(bufsize);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WaveBackend::start()
|
|
|
|
|
{
|
2024-06-30 19:35:57 +00:00
|
|
|
if(mDataStart > 0 && fseek(mFile.get(), 0, SEEK_END) != 0)
|
2025-09-03 16:09:27 +00:00
|
|
|
WARN("Failed to seek on output file");
|
2021-01-26 19:01:35 +00:00
|
|
|
try {
|
|
|
|
|
mKillNow.store(false, std::memory_order_release);
|
2025-09-03 16:09:27 +00:00
|
|
|
mThread = std::thread{&WaveBackend::mixerProc, this};
|
2021-01-26 19:01:35 +00:00
|
|
|
}
|
|
|
|
|
catch(std::exception& e) {
|
|
|
|
|
throw al::backend_exception{al::backend_error::DeviceError,
|
2025-09-03 16:09:27 +00:00
|
|
|
"Failed to start mixing thread: {}", e.what()};
|
2021-01-26 19:01:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WaveBackend::stop()
|
|
|
|
|
{
|
|
|
|
|
if(mKillNow.exchange(true, std::memory_order_acq_rel) || !mThread.joinable())
|
|
|
|
|
return;
|
|
|
|
|
mThread.join();
|
|
|
|
|
|
|
|
|
|
if(mDataStart > 0)
|
|
|
|
|
{
|
2024-06-30 19:35:57 +00:00
|
|
|
long size{ftell(mFile.get())};
|
2021-01-26 19:01:35 +00:00
|
|
|
if(size > 0)
|
|
|
|
|
{
|
|
|
|
|
long dataLen{size - mDataStart};
|
2024-06-30 19:35:57 +00:00
|
|
|
if(fseek(mFile.get(), 4, SEEK_SET) == 0)
|
|
|
|
|
fwrite32le(static_cast<uint>(size-8), mFile.get()); // 'WAVE' header len
|
|
|
|
|
if(fseek(mFile.get(), mDataStart-4, SEEK_SET) == 0)
|
|
|
|
|
fwrite32le(static_cast<uint>(dataLen), mFile.get()); // 'data' header len
|
2021-01-26 19:01:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool WaveBackendFactory::init()
|
|
|
|
|
{ return true; }
|
|
|
|
|
|
|
|
|
|
bool WaveBackendFactory::querySupport(BackendType type)
|
|
|
|
|
{ return type == BackendType::Playback; }
|
|
|
|
|
|
2024-06-30 19:35:57 +00:00
|
|
|
auto WaveBackendFactory::enumerate(BackendType type) -> std::vector<std::string>
|
2021-01-26 19:01:35 +00:00
|
|
|
{
|
|
|
|
|
switch(type)
|
|
|
|
|
{
|
|
|
|
|
case BackendType::Playback:
|
2024-06-30 19:35:57 +00:00
|
|
|
return std::vector{std::string{GetDeviceName()}};
|
2021-01-26 19:01:35 +00:00
|
|
|
case BackendType::Capture:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-06-30 19:35:57 +00:00
|
|
|
return {};
|
2021-01-26 19:01:35 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-30 20:32:45 +00:00
|
|
|
BackendPtr WaveBackendFactory::createBackend(DeviceBase *device, BackendType type)
|
2021-01-26 19:01:35 +00:00
|
|
|
{
|
|
|
|
|
if(type == BackendType::Playback)
|
|
|
|
|
return BackendPtr{new WaveBackend{device}};
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BackendFactory &WaveBackendFactory::getFactory()
|
|
|
|
|
{
|
|
|
|
|
static WaveBackendFactory factory{};
|
|
|
|
|
return factory;
|
|
|
|
|
}
|