Skip to content

Commit

Permalink
修复Recorder的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
NGLSG committed May 13, 2023
1 parent 2be88e4 commit 22944a7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
4 changes: 2 additions & 2 deletions include/Recorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Recorder {
public:
const std::string filepath = "recorded.wav";

double silentTimer;
long long silentTimer;
const int SILENT_TIMEOUT = 1000;

Recorder(int sampleRate, int framesPerBuffer);
Expand All @@ -34,6 +34,7 @@ class Recorder {
static const int SAMPLE_RATE = 16000;
int framesPerBuffer;
PaStream *stream;

std::vector<float> recordedData;

};
Expand Down Expand Up @@ -75,7 +76,6 @@ class Listener {
void ResetRecorded() {
run = false;
isRecorded = false;
recorder.silentTimer = 0;
recorder.stopRecording(); // 停止录音
std::lock_guard<std::mutex> lock(recordedData_mutex); // 加锁
recordedData.clear(); // 清空录音数据
Expand Down
4 changes: 2 additions & 2 deletions sample/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,12 +546,12 @@ void Application::render_setting_box() {
if (showbaiduPassword || baiduclicked) {
if (ImGui::InputText("BaiDu API Key", Bapi_buffer,
sizeof(Bapi_buffer))) {
configure.baiDuTranslator.APIKey=Bapi_buffer;
configure.baiDuTranslator.APIKey = Bapi_buffer;
}
} else {
if (ImGui::InputText("BaiDu API Key", Bapi_buffer,
sizeof(Bapi_buffer), ImGuiInputTextFlags_Password)) {
configure.baiDuTranslator.APIKey=Bapi_buffer;
configure.baiDuTranslator.APIKey = Bapi_buffer;
showbaiduPassword = true;
baidulastInputTime = ImGui::GetTime();
}
Expand Down
16 changes: 11 additions & 5 deletions src/Recorder.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "Recorder.h"

extern bool NoRecord;
bool Iscount = false;

Recorder::Recorder(int sampleRate, int framesPerBuffer)
: sampleRate(sampleRate), framesPerBuffer(framesPerBuffer) {
Logger::Init();
Iscount = false;
PaError err = Pa_Initialize();
if (err != paNoError) {
LogError("PortAudio initialization failed: {0}", Pa_GetErrorText(err));
Expand All @@ -24,10 +26,11 @@ void Recorder::startRecording() {

void Recorder::stopRecording(bool del) {
LogInfo("Stopping recording...");
silentTimer = LLONG_MIN;
silentTimer = 0;
Iscount = false;
Pa_StopStream(stream);
Pa_CloseStream(stream);

stream = NULL;
}

std::vector<float> Recorder::getRecordedData() const {
Expand All @@ -37,7 +40,7 @@ std::vector<float> Recorder::getRecordedData() const {
void Recorder::saveToWav(const std::string &fileName) {
if (!getRecordedData().empty()) {
const std::vector<float> &pcmData = getRecordedData();
getRecordedData().clear();
recordedData.clear();
FILE *file = fopen(fileName.c_str(), "wb");
if (file) {
// Write WAV header
Expand Down Expand Up @@ -97,11 +100,14 @@ int Recorder::recordCallback(const void *inputBuffer, void *outputBuffer, unsign
if (std::abs(input[i]) > 0.001) {
silence = false;
break;
} else {
silence = true;
}
}

// Reset timer if not silent
if (!silence) {
Iscount = true;
recorder->silentTimer = 0.0;
}

Expand All @@ -114,9 +120,9 @@ int Recorder::recordCallback(const void *inputBuffer, void *outputBuffer, unsign
if (!silence) {
recorder->recordedData.insert(recorder->recordedData.end(), input, input + framesPerBuffer);
}

// Increment timer
recorder->silentTimer += static_cast<double>(framesPerBuffer * 1000.0 / recorder->sampleRate);
if (Iscount)
recorder->silentTimer += static_cast<double>(framesPerBuffer * 1000.0 / recorder->sampleRate);
return paContinue;
}

Expand Down

0 comments on commit 22944a7

Please sign in to comment.