Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134diff --git a/examples/common-sdl.cpp b/examples/common-sdl.cppindex c18a5f3..fa55dde 100644--- a/examples/common-sdl.cpp+++ b/examples/common-sdl.cpp@@ -48,12 +48,17 @@ bool audio_async::init(int capture_id, int sample_rate) { }; capture_spec_requested.userdata = this; + SDL_AudioSpec native_spec;+ if (capture_id >= 0 && SDL_GetAudioDeviceSpec(capture_id, SDL_TRUE, &native_spec) == 0 && native_spec.channels > 1) {+ capture_spec_requested.channels = native_spec.channels;+ }+ if (capture_id >= 0) { fprintf(stderr, "%s: attempt to open capture device %d : '%s' ...\n", __func__, capture_id, SDL_GetAudioDeviceName(capture_id, SDL_TRUE));- m_dev_id_in = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(capture_id, SDL_TRUE), SDL_TRUE, &capture_spec_requested, &capture_spec_obtained, 0);+ m_dev_id_in = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(capture_id, SDL_TRUE), SDL_TRUE, &capture_spec_requested, &capture_spec_obtained, SDL_AUDIO_ALLOW_CHANNELS_CHANGE); } else { fprintf(stderr, "%s: attempt to open default capture device ...\n", __func__);- m_dev_id_in = SDL_OpenAudioDevice(nullptr, SDL_TRUE, &capture_spec_requested, &capture_spec_obtained, 0);+ m_dev_id_in = SDL_OpenAudioDevice(nullptr, SDL_TRUE, &capture_spec_requested, &capture_spec_obtained, SDL_AUDIO_ALLOW_CHANNELS_CHANGE); } if (!m_dev_id_in) {@@ -72,6 +77,7 @@ bool audio_async::init(int capture_id, int sample_rate) { } m_sample_rate = capture_spec_obtained.freq;+ m_channels = capture_spec_obtained.channels; m_audio.resize((m_sample_rate*m_len_ms)/1000); @@ -141,6 +147,22 @@ void audio_async::callback(uint8_t * stream, int len) { return; } + if (m_channels > 1) {+ // multi-channel devices (e.g. mic + loopback aggregates) are averaged down to mono+ const float * in = (const float *) stream;+ const size_t n_frames = len / sizeof(float) / m_channels;+ m_mix.resize(n_frames);+ for (size_t f = 0; f < n_frames; ++f) {+ float sum = 0.0f;+ for (int ch = 0; ch < m_channels; ++ch) {+ sum += in[f * m_channels + ch];+ }+ m_mix[f] = sum / m_channels;+ }+ stream = (uint8_t *) m_mix.data();+ len = (int) (n_frames * sizeof(float));+ }+ size_t n_samples = len / sizeof(float); if (n_samples > m_audio.size()) {diff --git a/examples/common-sdl.h b/examples/common-sdl.hindex 9ee8a32..13fb994 100644--- a/examples/common-sdl.h+++ b/examples/common-sdl.h@@ -36,6 +36,8 @@ private: int m_len_ms = 0; int m_sample_rate = 0;+ int m_channels = 1;+ std::vector<float> m_mix; std::atomic_bool m_running; std::mutex m_mutex;diff --git a/examples/stream/stream.cpp b/examples/stream/stream.cppindex 94f9016..c15b7fc 100644--- a/examples/stream/stream.cpp+++ b/examples/stream/stream.cpp@@ -8,6 +8,7 @@ #include "whisper.h" #include <chrono>+#include <ctime> #include <cstdio> #include <fstream> #include <string>@@ -138,7 +139,7 @@ int main(int argc, char ** argv) { const int n_new_line = !use_vad ? std::max(1, params.length_ms / params.step_ms - 1) : 1; // number of steps to print new line - params.no_timestamps = !use_vad;+ params.no_timestamps = true; params.no_context |= use_vad; params.max_tokens = 0; @@ -293,16 +294,17 @@ int main(int argc, char ** argv) { const auto t_now = std::chrono::high_resolution_clock::now(); const auto t_diff = std::chrono::duration_cast<std::chrono::milliseconds>(t_now - t_last).count(); - if (t_diff < 2000) {+ if (t_diff < 3000) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); continue; } - audio.get(2000, pcmf32_new);+ audio.get(3000, pcmf32_new); - if (::vad_simple(pcmf32_new, WHISPER_SAMPLE_RATE, 1000, params.vad_thold, params.freq_thold, false)) {+ if (::vad_simple(pcmf32_new, WHISPER_SAMPLE_RATE, 1500, params.vad_thold, params.freq_thold, false)) { audio.get(params.length_ms, pcmf32);+ audio.clear(); } else { std::this_thread::sleep_for(std::chrono::milliseconds(100)); @@ -361,6 +363,13 @@ int main(int argc, char ** argv) { printf("\n"); } + if (use_vad && params.fname_out.length() > 0) {+ char stamp[16];+ const std::time_t now = std::time(nullptr);+ std::strftime(stamp, sizeof(stamp), "[%H:%M:%S]", std::localtime(&now));+ fout << stamp;+ }+ const int n_segments = whisper_full_n_segments(ctx); for (int i = 0; i < n_segments; ++i) { const char * text = whisper_full_get_segment_text(ctx, i);@@ -394,7 +403,7 @@ int main(int argc, char ** argv) { } if (params.fname_out.length() > 0) {- fout << std::endl;+ fout << "\n" << std::endl; } if (use_vad) {