Debian Codegraph @

Source/Core/AudioCommon/WaveFile.cpp

raw symbols SCIP
1 // Copyright 2008 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "AudioCommon/WaveFile.h"
5
6 #include <string>
7
8 #include <fmt/format.h>
9
10 #include "AudioCommon/Mixer.h"
11 #include "Common/CommonTypes.h"
12 #include "Common/FileUtil.h"
13 #include "Common/IOFile.h"
14 #include "Common/Logging/Log.h"
15 #include "Common/MsgHandler.h"
16 #include "Common/StringUtil.h"
17 #include "Common/Swap.h"
18 #include "Core/Config/MainSettings.h"
19
20 constexpr size_t WaveFileWriter::BUFFER_SIZE;
21
22 WaveFileWriter::WaveFileWriter()
23 {
24 }
25
26 WaveFileWriter::~WaveFileWriter()
27 {
28 Stop();
29 }
30
31 bool WaveFileWriter::Start(const std::string& filename, u32 sample_rate_divisor)
32 {
33 // Ask to delete file
34 if (File::Exists(filename))
35 {
36 if (Config::Get(Config::MAIN_DUMP_AUDIO_SILENT) ||
37 AskYesNoFmtT("Delete the existing file '{0}'?", filename))
38 {
39 File::Delete(filename);
40 }
41 else
42 {
43 // Stop and cancel dumping the audio
44 return false;
45 }
46 }
47
48 // Check if the file is already open
49 if (m_file)
50 {
51 PanicAlertFmtT("The file {0} was already open, the file header will not be written.", filename);
52 return false;
53 }
54
55 m_file.Open(filename, "wb");
56 if (!m_file)
57 {
58 PanicAlertFmtT(
59 "The file {0} could not be opened for writing. Please check if it's already opened "
60 "by another program.",
61 filename);
62 return false;
63 }
64
65 m_audio_size = 0;
66
67 if (m_basename.empty())
68 SplitPath(filename, nullptr, &m_basename, nullptr);
69
70 m_current_sample_rate_divisor = sample_rate_divisor;
71
72 // -----------------
73 // Write file header
74 // -----------------
75 Write4("RIFF");
76 Write(100 * 1000 * 1000); // write big value in case the file gets truncated
77 Write4("WAVE");
78 Write4("fmt ");
79
80 Write(16); // size of fmt block
81 Write(0x00020001); // two channels, uncompressed
82
83 const u32 sample_rate = Mixer::FIXED_SAMPLE_RATE_DIVIDEND / sample_rate_divisor;
84 Write(sample_rate);
85 Write(sample_rate * 2 * 2); // two channels, 16bit
86
87 Write(0x00100004);
88 Write4("data");
89 Write(100 * 1000 * 1000 - 32);
90
91 // We are now at offset 44
92 if (m_file.Tell() != 44)
93 PanicAlertFmt("Wrong offset: {}", m_file.Tell());
94
95 return true;
96 }
97
98 void WaveFileWriter::Stop()
99 {
100 m_file.Seek(4, File::SeekOrigin::Begin);
101 Write(m_audio_size + 36);
102
103 m_file.Seek(40, File::SeekOrigin::Begin);
104 Write(m_audio_size);
105
106 m_file.Close();
107 }
108
109 void WaveFileWriter::Write(u32 value)
110 {
111 m_file.WriteArray(&value, 1);
112 }
113
114 void WaveFileWriter::Write4(const char* ptr)
115 {
116 m_file.WriteBytes(ptr, 4);
117 }
118
119 void WaveFileWriter::AddStereoSamplesBE(const short* sample_data, u32 count,
120 u32 sample_rate_divisor, int l_volume, int r_volume)
121 {
122 if (!m_file)
123 {
124 ERROR_LOG_FMT(AUDIO, "WaveFileWriter - file not open.");
125 return;
126 }
127
128 if (count * 2 > BUFFER_SIZE)
129 {
130 ERROR_LOG_FMT(AUDIO, "WaveFileWriter - buffer too small (count = {}).", count);
131 return;
132 }
133
134 if (m_skip_silence)
135 {
136 bool all_zero = true;
137
138 for (u32 i = 0; i < count * 2; i++)
139 {
140 if (sample_data[i])
141 all_zero = false;
142 }
143
144 if (all_zero)
145 return;
146 }
147
148 for (u32 i = 0; i < count; i++)
149 {
150 // Flip the audio channels from RL to LR
151 m_conv_buffer[2 * i] = Common::swap16((u16)sample_data[2 * i + 1]);
152 m_conv_buffer[2 * i + 1] = Common::swap16((u16)sample_data[2 * i]);
153
154 // Apply volume (volume ranges from 0 to 256)
155 m_conv_buffer[2 * i] = m_conv_buffer[2 * i] * l_volume / 256;
156 m_conv_buffer[2 * i + 1] = m_conv_buffer[2 * i + 1] * r_volume / 256;
157 }
158
159 if (sample_rate_divisor != m_current_sample_rate_divisor)
160 {
161 Stop();
162 m_file_index++;
163 const std::string filename =
164 fmt::format("{}{}{}.wav", File::GetUserPath(D_DUMPAUDIO_IDX), m_basename, m_file_index);
165 Start(filename, sample_rate_divisor);
166 m_current_sample_rate_divisor = sample_rate_divisor;
167 }
168
169 m_file.WriteBytes(m_conv_buffer.data(), count * 4);
170 m_audio_size += count * 4;
171 }