2013-04-18 03:09:55 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2014-03-12 19:33:41 +00:00
|
|
|
#include <string>
|
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "AudioCommon/WaveFile.h"
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Core/ConfigManager.h"
|
2008-12-08 05:30:24 +00:00
|
|
|
|
|
|
|
enum {BUF_SIZE = 32*1024};
|
|
|
|
|
2013-06-21 16:27:47 +00:00
|
|
|
WaveFileWriter::WaveFileWriter():
|
|
|
|
skip_silence(false),
|
|
|
|
audio_size(0),
|
2014-03-09 20:14:26 +00:00
|
|
|
conv_buffer(nullptr)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
WaveFileWriter::~WaveFileWriter()
|
|
|
|
{
|
|
|
|
delete [] conv_buffer;
|
|
|
|
Stop();
|
|
|
|
}
|
|
|
|
|
2014-03-12 19:33:41 +00:00
|
|
|
bool WaveFileWriter::Start(const std::string& filename, unsigned int HLESampleRate)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
|
|
|
if (!conv_buffer)
|
|
|
|
conv_buffer = new short[BUF_SIZE];
|
|
|
|
|
2009-02-14 01:07:20 +00:00
|
|
|
// Check if the file is already open
|
2008-12-08 05:30:24 +00:00
|
|
|
if (file)
|
2009-02-14 01:07:20 +00:00
|
|
|
{
|
2014-03-12 19:33:41 +00:00
|
|
|
PanicAlertT("The file %s was already open, the file header will not be written.", filename.c_str());
|
2008-12-08 05:30:24 +00:00
|
|
|
return false;
|
2009-02-14 01:07:20 +00:00
|
|
|
}
|
|
|
|
|
2011-03-11 10:21:46 +00:00
|
|
|
file.Open(filename, "wb");
|
2008-12-08 05:30:24 +00:00
|
|
|
if (!file)
|
2009-02-14 01:07:20 +00:00
|
|
|
{
|
2014-03-12 19:33:41 +00:00
|
|
|
PanicAlertT("The file %s could not be opened for writing. Please check if it's already opened by another program.", filename.c_str());
|
2008-12-08 05:30:24 +00:00
|
|
|
return false;
|
2009-02-14 01:07:20 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2013-06-21 16:27:47 +00:00
|
|
|
audio_size = 0;
|
|
|
|
|
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-12-08 05:30:24 +00:00
|
|
|
Write4("RIFF");
|
|
|
|
Write(100 * 1000 * 1000); // write big value in case the file gets truncated
|
|
|
|
Write4("WAVE");
|
|
|
|
Write4("fmt ");
|
2009-03-28 08:57:34 +00:00
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
Write(16); // size of fmt block
|
|
|
|
Write(0x00020001); //two channels, uncompressed
|
2009-03-28 08:57:34 +00:00
|
|
|
|
2011-05-28 23:32:11 +00:00
|
|
|
const u32 sample_rate = HLESampleRate;
|
2008-12-08 05:30:24 +00:00
|
|
|
Write(sample_rate);
|
|
|
|
Write(sample_rate * 2 * 2); //two channels, 16bit
|
2009-03-28 08:57:34 +00:00
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
Write(0x00100004);
|
|
|
|
Write4("data");
|
|
|
|
Write(100 * 1000 * 1000 - 32);
|
2009-03-28 08:57:34 +00:00
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
// We are now at offset 44
|
2011-03-11 10:21:46 +00:00
|
|
|
if (file.Tell() != 44)
|
2013-03-31 23:10:21 +00:00
|
|
|
PanicAlert("Wrong offset: %lld", (long long)file.Tell());
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2009-02-14 01:07:20 +00:00
|
|
|
return true;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WaveFileWriter::Stop()
|
|
|
|
{
|
2010-12-04 03:50:55 +00:00
|
|
|
// u32 file_size = (u32)ftello(file);
|
2011-03-11 10:21:46 +00:00
|
|
|
file.Seek(4, SEEK_SET);
|
2008-12-08 05:30:24 +00:00
|
|
|
Write(audio_size + 36);
|
2009-03-28 08:57:34 +00:00
|
|
|
|
2011-03-11 10:21:46 +00:00
|
|
|
file.Seek(40, SEEK_SET);
|
2008-12-08 05:30:24 +00:00
|
|
|
Write(audio_size);
|
2009-03-28 08:57:34 +00:00
|
|
|
|
2011-03-11 10:21:46 +00:00
|
|
|
file.Close();
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WaveFileWriter::Write(u32 value)
|
|
|
|
{
|
2011-03-11 10:21:46 +00:00
|
|
|
file.WriteArray(&value, 1);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WaveFileWriter::Write4(const char *ptr)
|
|
|
|
{
|
2011-03-11 10:21:46 +00:00
|
|
|
file.WriteBytes(ptr, 4);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-12 03:22:55 +00:00
|
|
|
void WaveFileWriter::AddStereoSamples(const short *sample_data, u32 count)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
|
|
|
if (!file)
|
2011-01-13 02:05:58 +00:00
|
|
|
PanicAlertT("WaveFileWriter - file not open.");
|
2013-03-20 01:51:12 +00:00
|
|
|
|
2013-01-12 03:22:55 +00:00
|
|
|
if (skip_silence)
|
|
|
|
{
|
2008-12-08 05:30:24 +00:00
|
|
|
bool all_zero = true;
|
2013-03-20 01:51:12 +00:00
|
|
|
|
2013-01-12 03:22:55 +00:00
|
|
|
for (u32 i = 0; i < count * 2; i++)
|
|
|
|
{
|
|
|
|
if (sample_data[i])
|
|
|
|
all_zero = false;
|
|
|
|
}
|
2013-03-20 01:51:12 +00:00
|
|
|
|
2013-01-12 03:22:55 +00:00
|
|
|
if (all_zero)
|
|
|
|
return;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2013-03-20 01:51:12 +00:00
|
|
|
|
2011-03-11 10:21:46 +00:00
|
|
|
file.WriteBytes(sample_data, count * 4);
|
2008-12-08 05:30:24 +00:00
|
|
|
audio_size += count * 4;
|
|
|
|
}
|
|
|
|
|
2013-01-12 03:22:55 +00:00
|
|
|
void WaveFileWriter::AddStereoSamplesBE(const short *sample_data, u32 count)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
|
|
|
if (!file)
|
2011-01-13 02:05:58 +00:00
|
|
|
PanicAlertT("WaveFileWriter - file not open.");
|
2009-03-28 08:57:34 +00:00
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
if (count > BUF_SIZE * 2)
|
2013-01-12 03:22:55 +00:00
|
|
|
PanicAlert("WaveFileWriter - buffer too small (count = %u).", count);
|
2009-03-28 08:57:34 +00:00
|
|
|
|
2013-10-29 05:23:17 +00:00
|
|
|
if (skip_silence)
|
2009-03-28 08:57:34 +00:00
|
|
|
{
|
2008-12-08 05:30:24 +00:00
|
|
|
bool all_zero = true;
|
2013-03-20 01:51:12 +00:00
|
|
|
|
2013-01-12 03:22:55 +00:00
|
|
|
for (u32 i = 0; i < count * 2; i++)
|
|
|
|
{
|
2008-12-08 05:30:24 +00:00
|
|
|
if (sample_data[i])
|
|
|
|
all_zero = false;
|
2013-01-12 03:22:55 +00:00
|
|
|
}
|
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
if (all_zero)
|
|
|
|
return;
|
|
|
|
}
|
2009-03-28 08:57:34 +00:00
|
|
|
|
2013-01-12 03:22:55 +00:00
|
|
|
for (u32 i = 0; i < count * 2; i++)
|
2008-12-08 05:30:24 +00:00
|
|
|
conv_buffer[i] = Common::swap16((u16)sample_data[i]);
|
2009-03-28 08:57:34 +00:00
|
|
|
|
2011-03-11 10:21:46 +00:00
|
|
|
file.WriteBytes(conv_buffer, count * 4);
|
2008-12-08 05:30:24 +00:00
|
|
|
audio_size += count * 4;
|
|
|
|
}
|