2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2017-03-03 19:43:52 +00:00
|
|
|
#include "AudioCommon/WaveFile.h"
|
|
|
|
|
2014-03-12 19:33:41 +00:00
|
|
|
#include <string>
|
|
|
|
|
2023-06-18 00:23:49 +00:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2024-07-26 21:24:16 +00:00
|
|
|
#include "AudioCommon/Mixer.h"
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2017-01-15 20:46:32 +00:00
|
|
|
#include "Common/FileUtil.h"
|
2020-09-15 10:29:41 +00:00
|
|
|
#include "Common/IOFile.h"
|
2020-11-27 02:36:49 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2016-10-07 20:55:13 +00:00
|
|
|
#include "Common/MsgHandler.h"
|
2016-11-27 21:31:48 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2017-03-03 19:43:52 +00:00
|
|
|
#include "Common/Swap.h"
|
2021-10-14 00:29:04 +00:00
|
|
|
#include "Core/Config/MainSettings.h"
|
2017-01-08 18:51:00 +00:00
|
|
|
#include "Core/ConfigManager.h"
|
|
|
|
|
2015-12-03 01:00:48 +00:00
|
|
|
constexpr size_t WaveFileWriter::BUFFER_SIZE;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2015-12-03 01:00:48 +00:00
|
|
|
WaveFileWriter::WaveFileWriter()
|
2008-10-08 18:57:33 +00:00
|
|
|
{
|
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2008-10-08 18:57:33 +00:00
|
|
|
WaveFileWriter::~WaveFileWriter()
|
|
|
|
{
|
|
|
|
Stop();
|
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2022-07-03 22:07:06 +00:00
|
|
|
bool WaveFileWriter::Start(const std::string& filename, u32 sample_rate_divisor)
|
2008-10-08 18:57:33 +00:00
|
|
|
{
|
2016-12-18 03:35:27 +00:00
|
|
|
// Ask to delete file
|
|
|
|
if (File::Exists(filename))
|
|
|
|
{
|
2021-10-14 00:29:04 +00:00
|
|
|
if (Config::Get(Config::MAIN_DUMP_AUDIO_SILENT) ||
|
2020-11-27 02:36:49 +00:00
|
|
|
AskYesNoFmtT("Delete the existing file '{0}'?", filename))
|
2016-12-18 03:35:27 +00:00
|
|
|
{
|
|
|
|
File::Delete(filename);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Stop and cancel dumping the audio
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-14 01:07:20 +00:00
|
|
|
// Check if the file is already open
|
2008-10-08 18:57:33 +00:00
|
|
|
if (file)
|
2009-02-14 01:07:20 +00:00
|
|
|
{
|
2020-11-27 02:36:49 +00:00
|
|
|
PanicAlertFmtT("The file {0} was already open, the file header will not be written.", filename);
|
2008-10-08 18:57:33 +00:00
|
|
|
return false;
|
2009-02-14 01:07:20 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2011-03-11 10:21:46 +00:00
|
|
|
file.Open(filename, "wb");
|
2008-10-08 18:57:33 +00:00
|
|
|
if (!file)
|
2009-02-14 01:07:20 +00:00
|
|
|
{
|
2020-11-27 02:36:49 +00:00
|
|
|
PanicAlertFmtT(
|
|
|
|
"The file {0} could not be opened for writing. Please check if it's already opened "
|
|
|
|
"by another program.",
|
|
|
|
filename);
|
2008-10-08 18:57:33 +00:00
|
|
|
return false;
|
2009-02-14 01:07:20 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-06-21 16:27:47 +00:00
|
|
|
audio_size = 0;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-06-11 22:52:45 +00:00
|
|
|
if (basename.empty())
|
|
|
|
SplitPath(filename, nullptr, &basename, nullptr);
|
|
|
|
|
2022-07-03 22:07:06 +00:00
|
|
|
current_sample_rate_divisor = sample_rate_divisor;
|
2016-06-11 22:52:45 +00:00
|
|
|
|
2009-03-28 08:57:34 +00:00
|
|
|
// -----------------
|
2009-02-14 01:07:20 +00:00
|
|
|
// Write file header
|
2009-03-28 08:57:34 +00:00
|
|
|
// -----------------
|
2008-10-08 18:57:33 +00:00
|
|
|
Write4("RIFF");
|
|
|
|
Write(100 * 1000 * 1000); // write big value in case the file gets truncated
|
|
|
|
Write4("WAVE");
|
|
|
|
Write4("fmt ");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2008-10-08 18:57:33 +00:00
|
|
|
Write(16); // size of fmt block
|
|
|
|
Write(0x00020001); // two channels, uncompressed
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2022-07-03 22:07:06 +00:00
|
|
|
const u32 sample_rate = Mixer::FIXED_SAMPLE_RATE_DIVIDEND / sample_rate_divisor;
|
2008-10-08 18:57:33 +00:00
|
|
|
Write(sample_rate);
|
|
|
|
Write(sample_rate * 2 * 2); // two channels, 16bit
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2008-10-08 18:57:33 +00:00
|
|
|
Write(0x00100004);
|
|
|
|
Write4("data");
|
|
|
|
Write(100 * 1000 * 1000 - 32);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2008-10-08 18:57:33 +00:00
|
|
|
// We are now at offset 44
|
2011-03-11 10:21:46 +00:00
|
|
|
if (file.Tell() != 44)
|
2020-11-27 02:36:49 +00:00
|
|
|
PanicAlertFmt("Wrong offset: {}", file.Tell());
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2009-02-14 01:07:20 +00:00
|
|
|
return true;
|
2008-10-08 18:57:33 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2008-10-08 18:57:33 +00:00
|
|
|
void WaveFileWriter::Stop()
|
|
|
|
{
|
2022-01-29 04:58:31 +00:00
|
|
|
file.Seek(4, File::SeekOrigin::Begin);
|
2008-10-08 18:57:33 +00:00
|
|
|
Write(audio_size + 36);
|
2009-03-28 08:57:34 +00:00
|
|
|
|
2022-01-29 04:58:31 +00:00
|
|
|
file.Seek(40, File::SeekOrigin::Begin);
|
2008-10-08 18:57:33 +00:00
|
|
|
Write(audio_size);
|
2009-03-28 08:57:34 +00:00
|
|
|
|
2011-03-11 10:21:46 +00:00
|
|
|
file.Close();
|
2008-10-08 18:57:33 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2008-10-08 18:57:33 +00:00
|
|
|
void WaveFileWriter::Write(u32 value)
|
|
|
|
{
|
2011-03-11 10:21:46 +00:00
|
|
|
file.WriteArray(&value, 1);
|
2008-10-08 18:57:33 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2008-10-08 18:57:33 +00:00
|
|
|
void WaveFileWriter::Write4(const char* ptr)
|
|
|
|
{
|
2011-03-11 10:21:46 +00:00
|
|
|
file.WriteBytes(ptr, 4);
|
2008-10-08 18:57:33 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2022-07-03 22:07:06 +00:00
|
|
|
void WaveFileWriter::AddStereoSamplesBE(const short* sample_data, u32 count,
|
|
|
|
u32 sample_rate_divisor, int l_volume, int r_volume)
|
2008-10-08 18:57:33 +00:00
|
|
|
{
|
|
|
|
if (!file)
|
WaveFile: Fix size check
`count` is the number of stereo samples to write (where each stereo sample is two shorts), while `BUFFER_SIZE` is the size of the buffer in shorts. So `count` needs to be multiplied by `2`, not `BUFFER_SIZE`. Also, when this check was failed, the previous code just clobbered whatever was past the end of the buffer after logging the warning, which corrupted `basename`, eventually resulting in Dolphin crashing.
This affected Datel's Wii-compatible Action Replay, which uses a block size of 2298, or 18384 stereo samples, which is 36768 shorts, which is bigger than the buffer size of 32768. (However, the previous commit means that only one block is transfered at a time, eliminating this issue; fixing the bounds check is just a general safety thing instead of an actual bugfix now.)
2022-06-11 18:40:38 +00:00
|
|
|
{
|
2020-11-27 02:36:49 +00:00
|
|
|
ERROR_LOG_FMT(AUDIO, "WaveFileWriter - file not open.");
|
WaveFile: Fix size check
`count` is the number of stereo samples to write (where each stereo sample is two shorts), while `BUFFER_SIZE` is the size of the buffer in shorts. So `count` needs to be multiplied by `2`, not `BUFFER_SIZE`. Also, when this check was failed, the previous code just clobbered whatever was past the end of the buffer after logging the warning, which corrupted `basename`, eventually resulting in Dolphin crashing.
This affected Datel's Wii-compatible Action Replay, which uses a block size of 2298, or 18384 stereo samples, which is 36768 shorts, which is bigger than the buffer size of 32768. (However, the previous commit means that only one block is transfered at a time, eliminating this issue; fixing the bounds check is just a general safety thing instead of an actual bugfix now.)
2022-06-11 18:40:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
WaveFile: Fix size check
`count` is the number of stereo samples to write (where each stereo sample is two shorts), while `BUFFER_SIZE` is the size of the buffer in shorts. So `count` needs to be multiplied by `2`, not `BUFFER_SIZE`. Also, when this check was failed, the previous code just clobbered whatever was past the end of the buffer after logging the warning, which corrupted `basename`, eventually resulting in Dolphin crashing.
This affected Datel's Wii-compatible Action Replay, which uses a block size of 2298, or 18384 stereo samples, which is 36768 shorts, which is bigger than the buffer size of 32768. (However, the previous commit means that only one block is transfered at a time, eliminating this issue; fixing the bounds check is just a general safety thing instead of an actual bugfix now.)
2022-06-11 18:40:38 +00:00
|
|
|
if (count * 2 > BUFFER_SIZE)
|
|
|
|
{
|
2020-11-27 02:36:49 +00:00
|
|
|
ERROR_LOG_FMT(AUDIO, "WaveFileWriter - buffer too small (count = {}).", count);
|
WaveFile: Fix size check
`count` is the number of stereo samples to write (where each stereo sample is two shorts), while `BUFFER_SIZE` is the size of the buffer in shorts. So `count` needs to be multiplied by `2`, not `BUFFER_SIZE`. Also, when this check was failed, the previous code just clobbered whatever was past the end of the buffer after logging the warning, which corrupted `basename`, eventually resulting in Dolphin crashing.
This affected Datel's Wii-compatible Action Replay, which uses a block size of 2298, or 18384 stereo samples, which is 36768 shorts, which is bigger than the buffer size of 32768. (However, the previous commit means that only one block is transfered at a time, eliminating this issue; fixing the bounds check is just a general safety thing instead of an actual bugfix now.)
2022-06-11 18:40:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2009-03-28 08:57:34 +00:00
|
|
|
if (skip_silence)
|
|
|
|
{
|
2008-10-08 18:57:33 +00:00
|
|
|
bool all_zero = true;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-01-12 03:22:55 +00:00
|
|
|
for (u32 i = 0; i < count * 2; i++)
|
|
|
|
{
|
2008-10-08 18:57:33 +00:00
|
|
|
if (sample_data[i])
|
|
|
|
all_zero = false;
|
2013-01-12 03:22:55 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2008-10-08 18:57:33 +00:00
|
|
|
if (all_zero)
|
|
|
|
return;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-10-13 05:52:54 +00:00
|
|
|
for (u32 i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
// Flip the audio channels from RL to LR
|
|
|
|
conv_buffer[2 * i] = Common::swap16((u16)sample_data[2 * i + 1]);
|
|
|
|
conv_buffer[2 * i + 1] = Common::swap16((u16)sample_data[2 * i]);
|
2022-06-11 04:27:10 +00:00
|
|
|
|
|
|
|
// Apply volume (volume ranges from 0 to 256)
|
|
|
|
conv_buffer[2 * i] = conv_buffer[2 * i] * l_volume / 256;
|
|
|
|
conv_buffer[2 * i + 1] = conv_buffer[2 * i + 1] * r_volume / 256;
|
2014-10-13 05:52:54 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2022-07-03 22:07:06 +00:00
|
|
|
if (sample_rate_divisor != current_sample_rate_divisor)
|
2016-06-11 22:52:45 +00:00
|
|
|
{
|
|
|
|
Stop();
|
|
|
|
file_index++;
|
2023-06-18 00:23:49 +00:00
|
|
|
const std::string filename =
|
|
|
|
fmt::format("{}{}{}.wav", File::GetUserPath(D_DUMPAUDIO_IDX), basename, file_index);
|
|
|
|
Start(filename, sample_rate_divisor);
|
2022-07-03 22:07:06 +00:00
|
|
|
current_sample_rate_divisor = sample_rate_divisor;
|
2016-06-11 22:52:45 +00:00
|
|
|
}
|
2016-07-01 00:43:59 +00:00
|
|
|
|
|
|
|
file.WriteBytes(conv_buffer.data(), count * 4);
|
|
|
|
audio_size += count * 4;
|
2016-06-11 22:52:45 +00:00
|
|
|
}
|