-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
240 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Copyright 2008 Dolphin Emulator Project | ||
// Licensed under GPLv2+ | ||
// Refer to the license.txt file included. | ||
|
||
#include <string> | ||
|
||
#include "Core/WaveFile.h" | ||
#include "Common/CommonTypes.h" | ||
#include "Common/MsgHandler.h" | ||
#include "Core/Config.h" | ||
|
||
constexpr size_t WaveFileWriter::BUFFER_SIZE; | ||
|
||
WaveFileWriter::WaveFileWriter() | ||
{ | ||
} | ||
|
||
WaveFileWriter::~WaveFileWriter() | ||
{ | ||
Stop(); | ||
} | ||
|
||
bool WaveFileWriter::Start(const std::string& filename, unsigned int HLESampleRate) | ||
{ | ||
// Check if the file is already open | ||
if (file) | ||
{ | ||
PanicAlert("The file %s was already open, the file header will not be written.", | ||
filename.c_str()); | ||
return false; | ||
} | ||
|
||
file.Open(filename, "wb"); | ||
if (!file) | ||
{ | ||
PanicAlert("The file %s could not be opened for writing. Please check if it's already opened " | ||
"by another program.", | ||
filename.c_str()); | ||
return false; | ||
} | ||
|
||
audio_size = 0; | ||
|
||
// ----------------- | ||
// 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) | ||
PanicAlert("Wrong offset: %lld", (long long)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::AddStereoSamples(const short* sample_data, u32 count) | ||
{ | ||
if (!file) | ||
PanicAlert("WaveFileWriter - file not open."); | ||
|
||
if (count > BUFFER_SIZE * 2) | ||
PanicAlert("WaveFileWriter - buffer too small (count = %u).", 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; | ||
} | ||
|
||
file.WriteBytes(sample_data, count * 4); | ||
audio_size += count * 4; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2008 Dolphin Emulator Project | ||
// Licensed under GPLv2+ | ||
// Refer to the license.txt file included. | ||
|
||
// --------------------------------------------------------------------------------- | ||
// Class: WaveFileWriter | ||
// Description: Simple utility class to make it easy to write long 16-bit stereo | ||
// audio streams to disk. | ||
// Use Start() to start recording to a file, and AddStereoSamples to add wave data. | ||
// The float variant will convert from -1.0-1.0 range and clamp. | ||
// Alternatively, AddSamplesBE for big endian wave data. | ||
// If Stop is not called when it destructs, the destructor will call Stop(). | ||
// --------------------------------------------------------------------------------- | ||
|
||
#pragma once | ||
|
||
#include <array> | ||
#include <string> | ||
#include "Common/CommonTypes.h" | ||
#include "Common/FileUtil.h" | ||
|
||
class WaveFileWriter | ||
{ | ||
public: | ||
WaveFileWriter(); | ||
~WaveFileWriter(); | ||
|
||
bool Start(const std::string& filename, unsigned int HLESampleRate); | ||
void Stop(); | ||
|
||
void SetSkipSilence(bool skip) { skip_silence = skip; } | ||
void AddStereoSamples(const short* sample_data, u32 count); | ||
u32 GetAudioSize() const { return audio_size; } | ||
private: | ||
static constexpr size_t BUFFER_SIZE = 32 * 1024; | ||
|
||
File::IOFile file; | ||
bool skip_silence = false; | ||
u32 audio_size = 0; | ||
std::array<short, BUFFER_SIZE> conv_buffer{}; | ||
void Write(u32 value); | ||
void Write4(const char* ptr); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.