summaryrefslogtreecommitdiff
path: root/Source/Core/AudioCommon/WaveFile.h
blob: b62e93cbf9f7936a2d31ef4cd4878641490e7400 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright 2008 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

// ---------------------------------------------------------------------------------
// 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/IOFile.h"

class WaveFileWriter
{
public:
  WaveFileWriter();
  ~WaveFileWriter();

  WaveFileWriter(const WaveFileWriter&) = delete;
  WaveFileWriter& operator=(const WaveFileWriter&) = delete;
  WaveFileWriter(WaveFileWriter&&) = delete;
  WaveFileWriter& operator=(WaveFileWriter&&) = delete;

  bool Start(const std::string& filename, u32 sample_rate_divisor);
  void Stop();

  void SetSkipSilence(bool skip) { m_skip_silence = skip; }
  // big endian
  void AddStereoSamplesBE(const short* sample_data, u32 count, u32 sample_rate_divisor,
                          int l_volume, int r_volume);
  u32 GetAudioSize() const { return m_audio_size; }

private:
  static constexpr size_t BUFFER_SIZE = 32 * 1024;

  void Write(u32 value);
  void Write4(const char* ptr);

  File::IOFile m_file;
  std::string m_basename;
  u32 m_file_index = 0;
  u32 m_audio_size = 0;

  u32 m_current_sample_rate_divisor;
  std::array<short, BUFFER_SIZE> m_conv_buffer{};

  bool m_skip_silence = false;
};