ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
wav_writer.h File Reference

Simple WAV file writer for audio debugging. More...

Go to the source code of this file.

Data Structures

struct  wav_writer_t
 File handle and sample counters needed to finalize a WAV recording. More...

Functions

wav_writer_t * wav_writer_open (const char *filepath, int sample_rate, int channels)
 Open WAV file for writing.
int wav_writer_write (wav_writer_t *writer, const float *samples, int num_samples)
 Write audio samples to WAV file.
void wav_writer_close (wav_writer_t *writer)
 Close WAV file and finalize header.
bool wav_dump_enabled (void)
 Check if audio dumping is enabled via environment.

Detailed Description

Simple WAV file writer for audio debugging.

Provides utilities to dump audio buffers to WAV files for debugging. Enable with environment variable: ASCIICHAT_DUMP_AUDIO=1

Definition in file wav_writer.h.

Function Documentation

◆ wav_dump_enabled()

bool wav_dump_enabled ( void )

Check if audio dumping is enabled via environment.

Returns
true if ASCIICHAT_DUMP_AUDIO=1

Definition at line 153 of file wav_writer.c.

153 {
154 const char *env = SAFE_GETENV("ASCIICHAT_DUMP_AUDIO");
155 return env && (strcmp(env, "1") == 0 || strcmp(env, "true") == 0);
156}
#define SAFE_GETENV(name)

References SAFE_GETENV.

Referenced by audio_analysis_init(), audio_client_init(), and client_audio_pipeline_create().

◆ wav_writer_close()

void wav_writer_close ( wav_writer_t * writer)

Close WAV file and finalize header.

Parameters
writerWAV writer context

Definition at line 127 of file wav_writer.c.

127 {
128 if (!writer) {
129 return;
130 }
131
132 if (writer->file) {
133 // Update file size and data size in header
134 uint32_t data_size = (uint32_t)(writer->samples_written * sizeof(float));
135 uint32_t file_size = data_size + 36; // 36 = header size without RIFF chunk
136
137 // Seek to file size field (offset 4)
138 fseek(writer->file, 4, SEEK_SET);
139 fwrite(&file_size, sizeof(uint32_t), 1, writer->file);
140
141 // Seek to data size field (offset 40)
142 fseek(writer->file, 40, SEEK_SET);
143 fwrite(&data_size, sizeof(uint32_t), 1, writer->file);
144
145 // Flush all buffered data to ensure headers are written before closing
146 fflush(writer->file);
147 fclose(writer->file);
148 }
149
150 SAFE_FREE(writer);
151}
unsigned int uint32_t
#define SAFE_FREE(ptr)
FILE * file
Definition wav_writer.h:29
size_t samples_written
Definition wav_writer.h:30

References wav_writer_t::file, SAFE_FREE, and wav_writer_t::samples_written.

Referenced by audio_analysis_destroy(), audio_cleanup(), audio_client_init(), and client_audio_pipeline_destroy().

◆ wav_writer_open()

wav_writer_t * wav_writer_open ( const char * filepath,
int sample_rate,
int channels )

Open WAV file for writing.

Parameters
filepathPath to WAV file to create
sample_rateSample rate (e.g., 44100)
channelsNumber of channels (1=mono, 2=stereo)
Returns
WAV writer context or NULL on error

Definition at line 59 of file wav_writer.c.

60 {
61 if (!filepath || sample_rate <= 0 || channels <= 0 || channels > 2) {
62 return NULL;
63 }
64
66 if (!writer) {
67 return NULL;
68 }
69
70 writer->file = platform_fopen("file_stream", filepath, "wb");
71 if (!writer->file) {
72 SAFE_FREE(writer);
73 return NULL;
74 }
75
76 writer->samples_written = 0;
77 writer->sample_rate = sample_rate;
78 writer->channels = channels;
79
80 // Write WAV header (we'll update sizes when closing)
81 wav_riff_header_t riff = {.riff = {'R', 'I', 'F', 'F'},
82 .file_size = 0, // Will update on close
83 .wave = {'W', 'A', 'V', 'E'}};
84
85 wav_fmt_chunk_t fmt = {.fmt = {'f', 'm', 't', ' '},
86 .chunk_size = 16,
87 .audio_format = 3, // IEEE float
88 .num_channels = (uint16_t)channels,
89 .sample_rate = (uint32_t)sample_rate,
90 .byte_rate =
91 (uint32_t)(sample_rate * channels * sizeof(float)),
92 .block_align = (uint16_t)(channels * sizeof(float)),
93 .bits_per_sample = 32};
94
95 wav_data_header_t data = {
96 .data = {'d', 'a', 't', 'a'},
97 .data_size = 0 // Will update on close
98 };
99
100 fwrite(&riff, sizeof(riff), 1, writer->file);
101 fwrite(&fmt, sizeof(fmt), 1, writer->file);
102 fwrite(&data, sizeof(data), 1, writer->file);
103
104 return writer;
105}
FILE * platform_fopen(const char *name, const char *filename, const char *mode)
Definition filesystem.c:18
unsigned short uint16_t
#define SAFE_MALLOC(size, cast)
Packed data chunk header giving the byte count of the following samples.
Definition wav_writer.c:53
Packed fmt chunk describing PCM/float encoding, channels, rate, and sample width.
Definition wav_writer.c:37
Packed RIFF/WAVE container header written before the format and data chunks.
Definition wav_writer.c:27
File handle and sample counters needed to finalize a WAV recording.
Definition wav_writer.h:28

References wav_writer_t::channels, ERROR_NOT_SUPPORTED, wav_writer_t::file, platform_fopen(), SAFE_FREE, SAFE_MALLOC, wav_writer_t::sample_rate, wav_writer_t::samples_written, and SET_ERRNO.

Referenced by audio_analysis_init(), audio_client_init(), and client_audio_pipeline_create().

◆ wav_writer_write()

int wav_writer_write ( wav_writer_t * writer,
const float * samples,
int num_samples )

Write audio samples to WAV file.

Parameters
writerWAV writer context
samplesFloat samples in range [-1.0, 1.0]
num_samplesNumber of samples to write
Returns
0 on success, -1 on error

Definition at line 107 of file wav_writer.c.

108 {
109 if (!writer || !writer->file || !samples || num_samples <= 0) {
110 return -1;
111 }
112
113 size_t written = fwrite(samples, sizeof(float), num_samples, writer->file);
114 if (written != (size_t)num_samples) {
115 return -1;
116 }
117
118 writer->samples_written += num_samples;
119
120 // Flush immediately to ensure data is written to disk (critical for real-time
121 // analysis)
122 fflush(writer->file);
123
124 return 0;
125}

References ERROR_NOT_SUPPORTED, wav_writer_t::file, wav_writer_t::samples_written, and SET_ERRNO.

Referenced by audio_analysis_track_received_sample(), audio_analysis_track_sent_sample(), audio_process_received_samples(), and client_audio_pipeline_process_duplex().