Spaces:
Running
Running
bobqianic
commited on
examples : move wav_writer from stream.cpp to common.h (#1317)
Browse files* Allocate class on the stack instead of on the heap
* Add class wav_writer
* fix some minor issues
* fix some minor issues
* remove potential misleading API
- examples/common.h +100 -0
- examples/stream/stream.cpp +5 -57
examples/common.h
CHANGED
|
@@ -7,6 +7,8 @@
|
|
| 7 |
#include <vector>
|
| 8 |
#include <random>
|
| 9 |
#include <thread>
|
|
|
|
|
|
|
| 10 |
|
| 11 |
#define COMMON_SAMPLE_RATE 16000
|
| 12 |
|
|
@@ -139,6 +141,104 @@ bool read_wav(
|
|
| 139 |
std::vector<std::vector<float>> & pcmf32s,
|
| 140 |
bool stereo);
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
// Apply a high-pass frequency filter to PCM audio
|
| 143 |
// Suppresses frequencies below cutoff Hz
|
| 144 |
void high_pass_filter(
|
|
|
|
| 7 |
#include <vector>
|
| 8 |
#include <random>
|
| 9 |
#include <thread>
|
| 10 |
+
#include <ctime>
|
| 11 |
+
#include <fstream>
|
| 12 |
|
| 13 |
#define COMMON_SAMPLE_RATE 16000
|
| 14 |
|
|
|
|
| 141 |
std::vector<std::vector<float>> & pcmf32s,
|
| 142 |
bool stereo);
|
| 143 |
|
| 144 |
+
// Write PCM data into WAV audio file
|
| 145 |
+
class wav_writer {
|
| 146 |
+
private:
|
| 147 |
+
std::ofstream file;
|
| 148 |
+
uint32_t dataSize = 0;
|
| 149 |
+
std::string wav_filename;
|
| 150 |
+
|
| 151 |
+
bool write_header(const uint32_t sample_rate,
|
| 152 |
+
const uint16_t bits_per_sample,
|
| 153 |
+
const uint16_t channels) {
|
| 154 |
+
|
| 155 |
+
file.write("RIFF", 4);
|
| 156 |
+
file.write("\0\0\0\0", 4); // Placeholder for file size
|
| 157 |
+
file.write("WAVE", 4);
|
| 158 |
+
file.write("fmt ", 4);
|
| 159 |
+
|
| 160 |
+
const uint32_t sub_chunk_size = 16;
|
| 161 |
+
const uint16_t audio_format = 1; // PCM format
|
| 162 |
+
const uint32_t byte_rate = sample_rate * channels * bits_per_sample / 8;
|
| 163 |
+
const uint16_t block_align = channels * bits_per_sample / 8;
|
| 164 |
+
|
| 165 |
+
file.write(reinterpret_cast<const char *>(&sub_chunk_size), 4);
|
| 166 |
+
file.write(reinterpret_cast<const char *>(&audio_format), 2);
|
| 167 |
+
file.write(reinterpret_cast<const char *>(&channels), 2);
|
| 168 |
+
file.write(reinterpret_cast<const char *>(&sample_rate), 4);
|
| 169 |
+
file.write(reinterpret_cast<const char *>(&byte_rate), 4);
|
| 170 |
+
file.write(reinterpret_cast<const char *>(&block_align), 2);
|
| 171 |
+
file.write(reinterpret_cast<const char *>(&bits_per_sample), 2);
|
| 172 |
+
file.write("data", 4);
|
| 173 |
+
file.write("\0\0\0\0", 4); // Placeholder for data size
|
| 174 |
+
|
| 175 |
+
return true;
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
// It is assumed that PCM data is normalized to a range from -1 to 1
|
| 179 |
+
bool write_audio(const float * data, size_t length) {
|
| 180 |
+
for (size_t i = 0; i < length; ++i) {
|
| 181 |
+
const auto intSample = static_cast<const int16_t>(data[i] * 32767);
|
| 182 |
+
file.write(reinterpret_cast<const char *>(&intSample), sizeof(int16_t));
|
| 183 |
+
dataSize += sizeof(int16_t);
|
| 184 |
+
}
|
| 185 |
+
if (file.is_open()) {
|
| 186 |
+
file.seekp(4, std::ios::beg);
|
| 187 |
+
uint32_t fileSize = 36 + dataSize;
|
| 188 |
+
file.write(reinterpret_cast<char *>(&fileSize), 4);
|
| 189 |
+
file.seekp(40, std::ios::beg);
|
| 190 |
+
file.write(reinterpret_cast<char *>(&dataSize), 4);
|
| 191 |
+
file.seekp(0, std::ios::end);
|
| 192 |
+
}
|
| 193 |
+
return true;
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
bool open_wav(const std::string & filename) {
|
| 197 |
+
if (filename != wav_filename) {
|
| 198 |
+
if (file.is_open()) {
|
| 199 |
+
file.close();
|
| 200 |
+
}
|
| 201 |
+
}
|
| 202 |
+
if (!file.is_open()) {
|
| 203 |
+
file.open(filename, std::ios::binary);
|
| 204 |
+
wav_filename = filename;
|
| 205 |
+
dataSize = 0;
|
| 206 |
+
}
|
| 207 |
+
return file.is_open();
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
public:
|
| 211 |
+
bool open(const std::string & filename,
|
| 212 |
+
const uint32_t sample_rate,
|
| 213 |
+
const uint16_t bits_per_sample,
|
| 214 |
+
const uint16_t channels) {
|
| 215 |
+
|
| 216 |
+
if (open_wav(filename)) {
|
| 217 |
+
write_header(sample_rate, bits_per_sample, channels);
|
| 218 |
+
} else {
|
| 219 |
+
return false;
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
return true;
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
bool close() {
|
| 226 |
+
file.close();
|
| 227 |
+
return true;
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
bool write(const float * data, size_t length) {
|
| 231 |
+
return write_audio(data, length);
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
~wav_writer() {
|
| 235 |
+
if (file.is_open()) {
|
| 236 |
+
file.close();
|
| 237 |
+
}
|
| 238 |
+
}
|
| 239 |
+
};
|
| 240 |
+
|
| 241 |
+
|
| 242 |
// Apply a high-pass frequency filter to PCM audio
|
| 243 |
// Suppresses frequencies below cutoff Hz
|
| 244 |
void high_pass_filter(
|
examples/stream/stream.cpp
CHANGED
|
@@ -2,7 +2,6 @@
|
|
| 2 |
//
|
| 3 |
// A very quick-n-dirty implementation serving mainly as a proof of concept.
|
| 4 |
//
|
| 5 |
-
#include <fstream>
|
| 6 |
#include "common-sdl.h"
|
| 7 |
#include "common.h"
|
| 8 |
#include "whisper.h"
|
|
@@ -13,60 +12,8 @@
|
|
| 13 |
#include <thread>
|
| 14 |
#include <vector>
|
| 15 |
#include <fstream>
|
| 16 |
-
#include <ctime>
|
| 17 |
-
|
| 18 |
-
class SimpleWavWriter {
|
| 19 |
-
private:
|
| 20 |
-
std::ofstream file;
|
| 21 |
-
int32_t dataSize = 0;
|
| 22 |
-
|
| 23 |
-
public:
|
| 24 |
-
SimpleWavWriter(const std::string &filename, int sampleRate, int bitsPerSample, int channels) {
|
| 25 |
-
file.open(filename, std::ios::binary);
|
| 26 |
-
|
| 27 |
-
file.write("RIFF", 4);
|
| 28 |
-
file.write("\0\0\0\0", 4); // Placeholder for file size
|
| 29 |
-
file.write("WAVE", 4);
|
| 30 |
-
file.write("fmt ", 4);
|
| 31 |
-
|
| 32 |
-
int32_t subChunkSize = 16;
|
| 33 |
-
int16_t audioFormat = 1; // PCM format
|
| 34 |
-
int32_t byteRate = sampleRate * channels * bitsPerSample / 8;
|
| 35 |
-
int16_t blockAlign = channels * bitsPerSample / 8;
|
| 36 |
-
|
| 37 |
-
file.write(reinterpret_cast<char *>(&subChunkSize), 4);
|
| 38 |
-
file.write(reinterpret_cast<char *>(&audioFormat), 2);
|
| 39 |
-
file.write(reinterpret_cast<char *>(&channels), 2);
|
| 40 |
-
file.write(reinterpret_cast<char *>(&sampleRate), 4);
|
| 41 |
-
file.write(reinterpret_cast<char *>(&byteRate), 4);
|
| 42 |
-
file.write(reinterpret_cast<char *>(&blockAlign), 2);
|
| 43 |
-
file.write(reinterpret_cast<char *>(&bitsPerSample), 2);
|
| 44 |
-
file.write("data", 4);
|
| 45 |
-
file.write("\0\0\0\0", 4); // Placeholder for data size
|
| 46 |
-
}
|
| 47 |
|
| 48 |
-
void writeData(const float *data, size_t length) {
|
| 49 |
-
for (size_t i = 0; i < length; ++i) {
|
| 50 |
-
int16_t intSample = static_cast<int16_t>(data[i] * 32767);
|
| 51 |
-
file.write(reinterpret_cast<char *>(&intSample), sizeof(int16_t));
|
| 52 |
-
dataSize += sizeof(int16_t);
|
| 53 |
-
}
|
| 54 |
-
if (file.is_open()) {
|
| 55 |
-
file.seekp(4, std::ios::beg);
|
| 56 |
-
int32_t fileSize = 36 + dataSize;
|
| 57 |
-
file.write(reinterpret_cast<char *>(&fileSize), 4);
|
| 58 |
-
file.seekp(40, std::ios::beg);
|
| 59 |
-
file.write(reinterpret_cast<char *>(&dataSize), 4);
|
| 60 |
-
file.seekp(0, std::ios::end);
|
| 61 |
-
}
|
| 62 |
-
}
|
| 63 |
|
| 64 |
-
~SimpleWavWriter() {
|
| 65 |
-
if (file.is_open()) {
|
| 66 |
-
file.close();
|
| 67 |
-
}
|
| 68 |
-
}
|
| 69 |
-
};
|
| 70 |
// 500 -> 00:05.000
|
| 71 |
// 6000 -> 01:00.000
|
| 72 |
std::string to_timestamp(int64_t t) {
|
|
@@ -266,8 +213,9 @@ int main(int argc, char ** argv) {
|
|
| 266 |
return 1;
|
| 267 |
}
|
| 268 |
}
|
|
|
|
|
|
|
| 269 |
// save wav file
|
| 270 |
-
SimpleWavWriter *wavWriter = nullptr;
|
| 271 |
if (params.save_audio) {
|
| 272 |
// Get current date/time for filename
|
| 273 |
time_t now = time(0);
|
|
@@ -275,7 +223,7 @@ int main(int argc, char ** argv) {
|
|
| 275 |
strftime(buffer, sizeof(buffer), "%Y%m%d%H%M%S", localtime(&now));
|
| 276 |
std::string filename = std::string(buffer) + ".wav";
|
| 277 |
|
| 278 |
-
wavWriter
|
| 279 |
}
|
| 280 |
printf("[Start speaking]\n");
|
| 281 |
fflush(stdout);
|
|
@@ -285,8 +233,8 @@ int main(int argc, char ** argv) {
|
|
| 285 |
|
| 286 |
// main audio loop
|
| 287 |
while (is_running) {
|
| 288 |
-
if (params.save_audio
|
| 289 |
-
wavWriter
|
| 290 |
}
|
| 291 |
// handle Ctrl + C
|
| 292 |
is_running = sdl_poll_events();
|
|
|
|
| 2 |
//
|
| 3 |
// A very quick-n-dirty implementation serving mainly as a proof of concept.
|
| 4 |
//
|
|
|
|
| 5 |
#include "common-sdl.h"
|
| 6 |
#include "common.h"
|
| 7 |
#include "whisper.h"
|
|
|
|
| 12 |
#include <thread>
|
| 13 |
#include <vector>
|
| 14 |
#include <fstream>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
// 500 -> 00:05.000
|
| 18 |
// 6000 -> 01:00.000
|
| 19 |
std::string to_timestamp(int64_t t) {
|
|
|
|
| 213 |
return 1;
|
| 214 |
}
|
| 215 |
}
|
| 216 |
+
|
| 217 |
+
wav_writer wavWriter;
|
| 218 |
// save wav file
|
|
|
|
| 219 |
if (params.save_audio) {
|
| 220 |
// Get current date/time for filename
|
| 221 |
time_t now = time(0);
|
|
|
|
| 223 |
strftime(buffer, sizeof(buffer), "%Y%m%d%H%M%S", localtime(&now));
|
| 224 |
std::string filename = std::string(buffer) + ".wav";
|
| 225 |
|
| 226 |
+
wavWriter.open(filename, WHISPER_SAMPLE_RATE, 16, 1);
|
| 227 |
}
|
| 228 |
printf("[Start speaking]\n");
|
| 229 |
fflush(stdout);
|
|
|
|
| 233 |
|
| 234 |
// main audio loop
|
| 235 |
while (is_running) {
|
| 236 |
+
if (params.save_audio) {
|
| 237 |
+
wavWriter.write(pcmf32_new.data(), pcmf32_new.size());
|
| 238 |
}
|
| 239 |
// handle Ctrl + C
|
| 240 |
is_running = sdl_poll_events();
|