mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2024-11-15 13:57:57 -07:00
8ea6bef98f
While trying to work on adding audiodump support for CLI, I was alerted that it was important to first try moving the DSP configs to the new config before continuing, as that makes it substantially easier to write clean code to add such a feature. This commit aims to allow for Dolphin to only rely on the new config for DSP-related settings.
160 lines
3.4 KiB
C++
160 lines
3.4 KiB
C++
// Copyright 2008 Dolphin Emulator Project
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
#include "AudioCommon/WaveFile.h"
|
|
|
|
#include <string>
|
|
|
|
#include "Common/CommonTypes.h"
|
|
#include "Common/FileUtil.h"
|
|
#include "Common/IOFile.h"
|
|
#include "Common/Logging/Log.h"
|
|
#include "Common/MsgHandler.h"
|
|
#include "Common/StringUtil.h"
|
|
#include "Common/Swap.h"
|
|
#include "Core/Config/MainSettings.h"
|
|
#include "Core/ConfigManager.h"
|
|
|
|
constexpr size_t WaveFileWriter::BUFFER_SIZE;
|
|
|
|
WaveFileWriter::WaveFileWriter()
|
|
{
|
|
}
|
|
|
|
WaveFileWriter::~WaveFileWriter()
|
|
{
|
|
Stop();
|
|
}
|
|
|
|
bool WaveFileWriter::Start(const std::string& filename, unsigned int HLESampleRate)
|
|
{
|
|
// Ask to delete file
|
|
if (File::Exists(filename))
|
|
{
|
|
if (Config::Get(Config::MAIN_DUMP_AUDIO_SILENT) ||
|
|
AskYesNoFmtT("Delete the existing file '{0}'?", filename))
|
|
{
|
|
File::Delete(filename);
|
|
}
|
|
else
|
|
{
|
|
// Stop and cancel dumping the audio
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Check if the file is already open
|
|
if (file)
|
|
{
|
|
PanicAlertFmtT("The file {0} was already open, the file header will not be written.", filename);
|
|
return false;
|
|
}
|
|
|
|
file.Open(filename, "wb");
|
|
if (!file)
|
|
{
|
|
PanicAlertFmtT(
|
|
"The file {0} could not be opened for writing. Please check if it's already opened "
|
|
"by another program.",
|
|
filename);
|
|
return false;
|
|
}
|
|
|
|
audio_size = 0;
|
|
|
|
if (basename.empty())
|
|
SplitPath(filename, nullptr, &basename, nullptr);
|
|
|
|
current_sample_rate = HLESampleRate;
|
|
|
|
// -----------------
|
|
// Write file header
|
|
// -----------------
|
|
Write4("RIFF");
|
|
Write(100 * 1000 * 1000); // write big value in case the file gets truncated
|
|
Write4("WAVE");
|
|
Write4("fmt ");
|
|
|
|
Write(16); // size of fmt block
|
|
Write(0x00020001); // two channels, uncompressed
|
|
|
|
const u32 sample_rate = HLESampleRate;
|
|
Write(sample_rate);
|
|
Write(sample_rate * 2 * 2); // two channels, 16bit
|
|
|
|
Write(0x00100004);
|
|
Write4("data");
|
|
Write(100 * 1000 * 1000 - 32);
|
|
|
|
// We are now at offset 44
|
|
if (file.Tell() != 44)
|
|
PanicAlertFmt("Wrong offset: {}", file.Tell());
|
|
|
|
return true;
|
|
}
|
|
|
|
void WaveFileWriter::Stop()
|
|
{
|
|
// u32 file_size = (u32)ftello(file);
|
|
file.Seek(4, SEEK_SET);
|
|
Write(audio_size + 36);
|
|
|
|
file.Seek(40, SEEK_SET);
|
|
Write(audio_size);
|
|
|
|
file.Close();
|
|
}
|
|
|
|
void WaveFileWriter::Write(u32 value)
|
|
{
|
|
file.WriteArray(&value, 1);
|
|
}
|
|
|
|
void WaveFileWriter::Write4(const char* ptr)
|
|
{
|
|
file.WriteBytes(ptr, 4);
|
|
}
|
|
|
|
void WaveFileWriter::AddStereoSamplesBE(const short* sample_data, u32 count, int sample_rate)
|
|
{
|
|
if (!file)
|
|
ERROR_LOG_FMT(AUDIO, "WaveFileWriter - file not open.");
|
|
|
|
if (count > BUFFER_SIZE * 2)
|
|
ERROR_LOG_FMT(AUDIO, "WaveFileWriter - buffer too small (count = {}).", count);
|
|
|
|
if (skip_silence)
|
|
{
|
|
bool all_zero = true;
|
|
|
|
for (u32 i = 0; i < count * 2; i++)
|
|
{
|
|
if (sample_data[i])
|
|
all_zero = false;
|
|
}
|
|
|
|
if (all_zero)
|
|
return;
|
|
}
|
|
|
|
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]);
|
|
}
|
|
|
|
if (sample_rate != current_sample_rate)
|
|
{
|
|
Stop();
|
|
file_index++;
|
|
std::ostringstream filename;
|
|
filename << File::GetUserPath(D_DUMPAUDIO_IDX) << basename << file_index << ".wav";
|
|
Start(filename.str(), sample_rate);
|
|
current_sample_rate = sample_rate;
|
|
}
|
|
|
|
file.WriteBytes(conv_buffer.data(), count * 4);
|
|
audio_size += count * 4;
|
|
}
|