summaryrefslogtreecommitdiff
path: root/src/audio/wav_dump_backend.h
blob: b037fd103baafc93072182a5331a140460d4362a (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
// This file is part of the 64k demo project.
// WAV dump backend for debugging audio output to file instead of device.
// Stripped in final build (STRIP_ALL).

#if !defined(DEMO_AUDIO_WAV_DUMP_BACKEND_H)
#define DEMO_AUDIO_WAV_DUMP_BACKEND_H

#include "audio_backend.h"
#include <stdio.h>
#include <vector>

#if !defined(STRIP_ALL)

// WAV file dump backend for debugging
// Captures audio from synth_render() and writes to .wav file
class WavDumpBackend : public AudioBackend {
 public:
  WavDumpBackend();
  ~WavDumpBackend();

  // AudioBackend interface
  void init() override;
  void start() override;
  void shutdown() override;

  // Set output filename (call before init())
  void set_output_file(const char* filename);

  // Get total samples written
  size_t get_samples_written() const { return samples_written_; }

 private:
  // Write WAV header with known sample count
  void write_wav_header(FILE* file, uint32_t num_samples);

  // Update WAV header with final sample count
  void update_wav_header();

  FILE* wav_file_;
  std::vector<float> sample_buffer_;
  size_t samples_written_;
  const char* output_filename_;
  bool is_active_;

  static const int kSampleRate = 32000;
  static const int kBufferSize = 1024;
};

#endif /* !defined(STRIP_ALL) */

#endif /* DEMO_AUDIO_WAV_DUMP_BACKEND_H */