2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-03-03 12:43:52 -07:00
|
|
|
#include "AudioCommon/WaveFile.h"
|
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
#include <string>
|
|
|
|
|
2014-09-07 19:06:58 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
2017-01-15 13:46:32 -07:00
|
|
|
#include "Common/FileUtil.h"
|
2020-09-15 04:29:41 -06:00
|
|
|
#include "Common/IOFile.h"
|
2020-11-26 19:36:49 -07:00
|
|
|
#include "Common/Logging/Log.h"
|
2016-10-07 14:55:13 -06:00
|
|
|
#include "Common/MsgHandler.h"
|
2016-11-27 14:31:48 -07:00
|
|
|
#include "Common/StringUtil.h"
|
2017-03-03 12:43:52 -07:00
|
|
|
#include "Common/Swap.h"
|
2017-01-08 11:51:00 -07:00
|
|
|
#include "Core/ConfigManager.h"
|
|
|
|
|
2015-12-02 18:00:48 -07:00
|
|
|
constexpr size_t WaveFileWriter::BUFFER_SIZE;
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2015-12-02 18:00:48 -07:00
|
|
|
WaveFileWriter::WaveFileWriter()
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
WaveFileWriter::~WaveFileWriter()
|
|
|
|
{
|
|
|
|
Stop();
|
|
|
|
}
|
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
bool WaveFileWriter::Start(const std::string& filename, unsigned int HLESampleRate)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2016-12-17 20:35:27 -07:00
|
|
|
// Ask to delete file
|
|
|
|
if (File::Exists(filename))
|
|
|
|
{
|
2017-01-08 11:51:00 -07:00
|
|
|
if (SConfig::GetInstance().m_DumpAudioSilent ||
|
2020-11-26 19:36:49 -07:00
|
|
|
AskYesNoFmtT("Delete the existing file '{0}'?", filename))
|
2016-12-17 20:35:27 -07:00
|
|
|
{
|
|
|
|
File::Delete(filename);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Stop and cancel dumping the audio
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-13 18:07:20 -07:00
|
|
|
// Check if the file is already open
|
2008-12-07 22:30:24 -07:00
|
|
|
if (file)
|
2009-02-13 18:07:20 -07:00
|
|
|
{
|
2020-11-26 19:36:49 -07:00
|
|
|
PanicAlertFmtT("The file {0} was already open, the file header will not be written.", filename);
|
2008-12-07 22:30:24 -07:00
|
|
|
return false;
|
2009-02-13 18:07:20 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2011-03-11 03:21:46 -07:00
|
|
|
file.Open(filename, "wb");
|
2008-12-07 22:30:24 -07:00
|
|
|
if (!file)
|
2009-02-13 18:07:20 -07:00
|
|
|
{
|
2020-11-26 19:36:49 -07:00
|
|
|
PanicAlertFmtT(
|
|
|
|
"The file {0} could not be opened for writing. Please check if it's already opened "
|
|
|
|
"by another program.",
|
|
|
|
filename);
|
2008-12-07 22:30:24 -07:00
|
|
|
return false;
|
2009-02-13 18:07:20 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-06-21 10:27:47 -06:00
|
|
|
audio_size = 0;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2016-06-11 16:52:45 -06:00
|
|
|
if (basename.empty())
|
|
|
|
SplitPath(filename, nullptr, &basename, nullptr);
|
|
|
|
|
|
|
|
current_sample_rate = HLESampleRate;
|
|
|
|
|
2009-03-28 02:57:34 -06:00
|
|
|
// -----------------
|
2009-02-13 18:07:20 -07:00
|
|
|
// Write file header
|
2009-03-28 02:57:34 -06:00
|
|
|
// -----------------
|
2008-12-07 22:30:24 -07:00
|
|
|
Write4("RIFF");
|
|
|
|
Write(100 * 1000 * 1000); // write big value in case the file gets truncated
|
|
|
|
Write4("WAVE");
|
|
|
|
Write4("fmt ");
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2008-12-07 22:30:24 -07:00
|
|
|
Write(16); // size of fmt block
|
|
|
|
Write(0x00020001); // two channels, uncompressed
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2011-05-28 17:32:11 -06:00
|
|
|
const u32 sample_rate = HLESampleRate;
|
2008-12-07 22:30:24 -07:00
|
|
|
Write(sample_rate);
|
|
|
|
Write(sample_rate * 2 * 2); // two channels, 16bit
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2008-12-07 22:30:24 -07:00
|
|
|
Write(0x00100004);
|
|
|
|
Write4("data");
|
|
|
|
Write(100 * 1000 * 1000 - 32);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2008-12-07 22:30:24 -07:00
|
|
|
// We are now at offset 44
|
2011-03-11 03:21:46 -07:00
|
|
|
if (file.Tell() != 44)
|
2020-11-26 19:36:49 -07:00
|
|
|
PanicAlertFmt("Wrong offset: {}", file.Tell());
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2009-02-13 18:07:20 -07:00
|
|
|
return true;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void WaveFileWriter::Stop()
|
|
|
|
{
|
2010-12-03 20:50:55 -07:00
|
|
|
// u32 file_size = (u32)ftello(file);
|
2011-03-11 03:21:46 -07:00
|
|
|
file.Seek(4, SEEK_SET);
|
2008-12-07 22:30:24 -07:00
|
|
|
Write(audio_size + 36);
|
2009-03-28 02:57:34 -06:00
|
|
|
|
2011-03-11 03:21:46 -07:00
|
|
|
file.Seek(40, SEEK_SET);
|
2008-12-07 22:30:24 -07:00
|
|
|
Write(audio_size);
|
2009-03-28 02:57:34 -06:00
|
|
|
|
2011-03-11 03:21:46 -07:00
|
|
|
file.Close();
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void WaveFileWriter::Write(u32 value)
|
|
|
|
{
|
2011-03-11 03:21:46 -07:00
|
|
|
file.WriteArray(&value, 1);
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void WaveFileWriter::Write4(const char* ptr)
|
|
|
|
{
|
2011-03-11 03:21:46 -07:00
|
|
|
file.WriteBytes(ptr, 4);
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2016-06-11 16:52:45 -06:00
|
|
|
void WaveFileWriter::AddStereoSamplesBE(const short* sample_data, u32 count, int sample_rate)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
|
|
|
if (!file)
|
2020-11-26 19:36:49 -07:00
|
|
|
ERROR_LOG_FMT(AUDIO, "WaveFileWriter - file not open.");
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-12-02 18:00:48 -07:00
|
|
|
if (count > BUFFER_SIZE * 2)
|
2020-11-26 19:36:49 -07:00
|
|
|
ERROR_LOG_FMT(AUDIO, "WaveFileWriter - buffer too small (count = {}).", count);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2009-03-28 02:57:34 -06:00
|
|
|
if (skip_silence)
|
|
|
|
{
|
2008-12-07 22:30:24 -07:00
|
|
|
bool all_zero = true;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-01-11 20:22:55 -07:00
|
|
|
for (u32 i = 0; i < count * 2; i++)
|
|
|
|
{
|
2008-12-07 22:30:24 -07:00
|
|
|
if (sample_data[i])
|
|
|
|
all_zero = false;
|
2013-01-11 20:22:55 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2008-12-07 22:30:24 -07:00
|
|
|
if (all_zero)
|
|
|
|
return;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-10-12 23:52:54 -06: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]);
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2016-06-11 16:52:45 -06:00
|
|
|
if (sample_rate != current_sample_rate)
|
|
|
|
{
|
|
|
|
Stop();
|
|
|
|
file_index++;
|
2019-09-14 14:40:34 -06:00
|
|
|
std::ostringstream filename;
|
2016-06-11 16:52:45 -06:00
|
|
|
filename << File::GetUserPath(D_DUMPAUDIO_IDX) << basename << file_index << ".wav";
|
|
|
|
Start(filename.str(), sample_rate);
|
|
|
|
current_sample_rate = sample_rate;
|
|
|
|
}
|
2016-06-30 18:43:59 -06:00
|
|
|
|
|
|
|
file.WriteBytes(conv_buffer.data(), count * 4);
|
|
|
|
audio_size += count * 4;
|
2016-06-11 16:52:45 -06:00
|
|
|
}
|