summaryrefslogtreecommitdiff
path: root/src/audio
diff options
context:
space:
mode:
Diffstat (limited to 'src/audio')
-rw-r--r--src/audio/wav_dump_backend.cc52
-rw-r--r--src/audio/wav_dump_backend.h8
2 files changed, 38 insertions, 22 deletions
diff --git a/src/audio/wav_dump_backend.cc b/src/audio/wav_dump_backend.cc
index d1acf66..9fa13f5 100644
--- a/src/audio/wav_dump_backend.cc
+++ b/src/audio/wav_dump_backend.cc
@@ -14,19 +14,24 @@
#include <string.h>
WavDumpBackend::WavDumpBackend()
- : wav_file_(nullptr),
- samples_written_(0),
- output_filename_("audio_dump.wav"),
- is_active_(false) {
+ : wav_file_(nullptr), samples_written_(0),
+ output_filename_("audio_dump.wav"), is_active_(false),
+ duration_sec_(60.0f) {
sample_buffer_.resize(kBufferSize);
}
-WavDumpBackend::~WavDumpBackend() { shutdown(); }
+WavDumpBackend::~WavDumpBackend() {
+ shutdown();
+}
void WavDumpBackend::set_output_file(const char* filename) {
output_filename_ = filename;
}
+void WavDumpBackend::set_duration(float seconds) {
+ duration_sec_ = seconds;
+}
+
void WavDumpBackend::init() {
// Open WAV file for writing
wav_file_ = fopen(output_filename_, "wb");
@@ -48,11 +53,11 @@ void WavDumpBackend::start() {
// Render audio in small chunks with tracker updates
// This matches the seek logic in main.cc
- const int max_duration_sec = 60;
- const float update_dt = 1.0f / 60.0f; // 60Hz update rate (matches main loop)
- const int frames_per_update = (int)(kSampleRate * update_dt); // ~533 frames
- const int samples_per_update = frames_per_update * 2; // Stereo: 2 samples per frame
- const int total_updates = (int)(max_duration_sec / update_dt);
+ const float update_dt = 1.0f / 60.0f; // 60Hz update rate (matches main loop)
+ const int frames_per_update = (int)(kSampleRate * update_dt); // ~533 frames
+ const int samples_per_update =
+ frames_per_update * 2; // Stereo: 2 samples per frame
+ const int total_updates = (int)(duration_sec_ / update_dt);
// Music time tracking
float music_time = 0.0f;
@@ -71,12 +76,12 @@ void WavDumpBackend::start() {
tempo_scale = 1.0f;
} else if (physical_time < 15.0f) {
const float progress = (physical_time - 10.0f) / 5.0f;
- tempo_scale = 1.0f + progress * 1.0f; // 1.0 → 2.0
+ tempo_scale = 1.0f + progress * 1.0f; // 1.0 → 2.0
} else if (physical_time < 20.0f) {
tempo_scale = 1.0f;
} else if (physical_time < 25.0f) {
const float progress = (physical_time - 20.0f) / 5.0f;
- tempo_scale = 1.0f - progress * 0.5f; // 1.0 → 0.5
+ tempo_scale = 1.0f - progress * 0.5f; // 1.0 → 0.5
} else {
tempo_scale = 1.0f;
}
@@ -99,8 +104,10 @@ void WavDumpBackend::start() {
// Convert float to int16 and write to WAV (stereo interleaved)
for (int i = 0; i < samples_per_update; ++i) {
float sample = chunk_buffer[i];
- if (sample > 1.0f) sample = 1.0f;
- if (sample < -1.0f) sample = -1.0f;
+ if (sample > 1.0f)
+ sample = 1.0f;
+ if (sample < -1.0f)
+ sample = -1.0f;
const int16_t sample_i16 = (int16_t)(sample * 32767.0f);
fwrite(&sample_i16, sizeof(int16_t), 1, wav_file_);
@@ -110,8 +117,8 @@ void WavDumpBackend::start() {
// Progress indicator
if (update_count % 60 == 0) {
- printf(" Rendering: %.1fs / %ds (music: %.1fs, tempo: %.2fx)\r",
- physical_time, max_duration_sec, music_time, tempo_scale);
+ printf(" Rendering: %.1fs / %.1fs (music: %.1fs, tempo: %.2fx)\r",
+ physical_time, duration_sec_, music_time, tempo_scale);
fflush(stdout);
}
@@ -120,8 +127,10 @@ void WavDumpBackend::start() {
}
printf(
- "\nWAV dump complete: %zu samples (%.2f seconds stereo, %.2f music time)\n",
- samples_written_, (float)samples_written_ / (kSampleRate * 2), music_time);
+ "\nWAV dump complete: %zu samples (%.2f seconds stereo, %.2f music "
+ "time)\n",
+ samples_written_, (float)samples_written_ / (kSampleRate * 2),
+ music_time);
is_active_ = false;
}
@@ -141,7 +150,7 @@ void WavDumpBackend::write_wav_header(FILE* file, uint32_t num_samples) {
// WAV file header structure
// Reference: http://soundfile.sapp.org/doc/WaveFormat/
- const uint32_t num_channels = 2; // Stereo (matches miniaudio config)
+ const uint32_t num_channels = 2; // Stereo (matches miniaudio config)
const uint32_t sample_rate = kSampleRate;
const uint32_t bits_per_sample = 16;
const uint32_t byte_rate = sample_rate * num_channels * bits_per_sample / 8;
@@ -158,7 +167,7 @@ void WavDumpBackend::write_wav_header(FILE* file, uint32_t num_samples) {
fwrite("fmt ", 1, 4, file);
const uint32_t subchunk1_size = 16;
fwrite(&subchunk1_size, 4, 1, file);
- const uint16_t audio_format = 1; // PCM
+ const uint16_t audio_format = 1; // PCM
fwrite(&audio_format, 2, 1, file);
fwrite(&num_channels, 2, 1, file);
fwrite(&sample_rate, 4, 1, file);
@@ -172,7 +181,8 @@ void WavDumpBackend::write_wav_header(FILE* file, uint32_t num_samples) {
}
void WavDumpBackend::update_wav_header() {
- if (wav_file_ == nullptr) return;
+ if (wav_file_ == nullptr)
+ return;
// Seek to beginning and rewrite header with actual sample count
fseek(wav_file_, 0, SEEK_SET);
diff --git a/src/audio/wav_dump_backend.h b/src/audio/wav_dump_backend.h
index b037fd1..eb6e011 100644
--- a/src/audio/wav_dump_backend.h
+++ b/src/audio/wav_dump_backend.h
@@ -26,8 +26,13 @@ class WavDumpBackend : public AudioBackend {
// Set output filename (call before init())
void set_output_file(const char* filename);
+ // Set duration in seconds (default: 60s, call before start())
+ void set_duration(float seconds);
+
// Get total samples written
- size_t get_samples_written() const { return samples_written_; }
+ size_t get_samples_written() const {
+ return samples_written_;
+ }
private:
// Write WAV header with known sample count
@@ -41,6 +46,7 @@ class WavDumpBackend : public AudioBackend {
size_t samples_written_;
const char* output_filename_;
bool is_active_;
+ float duration_sec_;
static const int kSampleRate = 32000;
static const int kBufferSize = 1024;