Я использую Java Android AudioRecorder (код приведен ниже). Когда я начинаю запись, среднеквадратичное значение — это нормальное значение, превышающее 0. Через несколько секунд после записи среднеквадратичное значение получает только значение 0,0, что означает, что audioBuffer — это нулевой буфер. Как я могу это исправить? Спасибо.
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, channelConfig, audioFormat, bufferSize);
while (isRecording) {
int read = recorder.read(audioBuffer, 0, bufferSize);
if (read > 0) {
long currentTime = System.currentTimeMillis();
// Calculate the RMS (root mean square) value of the audio buffer
double sum = 0;
for (int i = 0; i < read; i += 2) {
short sample = (short) ((audioBuffer[i + 1] 0) {
long currentTime = System.currentTimeMillis();
// Calculate the RMS (root mean square) value of the audio buffer
double sum = 0;
for (int i = 0; i < read; i += 2) {
short sample = (short) ((audioBuffer[i + 1] SILENCE_THRESHOLD) {
lastVoiceTimestamp = currentTime;
Log.i(Config.TAG, "Loud.");
// If the output stream is null, create a new file
if (os == null) {
try {
os = new FileOutputStream(filePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
// Write audio data to file
try {
os.write(audioBuffer, 0, read);
} catch (IOException e) {
e.printStackTrace();
}
} else {
Log.i(Config.TAG, "Silent "+ String.valueOf(rms)+" "+String.valueOf(recorder.getState()));
// Check if silence duration has exceeded the threshold
if (currentTime - lastVoiceTimestamp > SILENCE_DURATION) {
Log.i(Config.TAG, "Silent Time Out "+ String.valueOf(rms)+" "+ String.valueOf(lastVoiceTimestamp));
// Close the current file and prepare for a new one
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
os = null;
// Encode the recorded PCM data to M4A
String m4aFilePath = filePath.replace(".pcm", ".m4a");
convertPcmToM4a(filePath, m4aFilePath);
filePath = audioDir + "/" + "record_" + System.currentTimeMillis() + ".pcm";
// Trigger transcription and translation
new Thread(() -> {
triggerTranscriptionAndTranslation(m4aFilePath);
}).start();
Log.i(Config.TAG, "New audio "+ filePath);
}
} else {
// If within silence duration, continue writing silence (optional)
if (os != null) {
try {
os.write(audioBuffer, 0, read);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
// Clean up when recording stops
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
os = null;
// Encode the last recorded PCM data to M4A
String m4aFilePath = filePath.replace(".pcm", ".m4a");
convertPcmToM4a(filePath, m4aFilePath);
// Trigger transcription and translation
new Thread(() -> {
triggerTranscriptionAndTranslation(m4aFilePath);
}).start();
}
}
Я зарегистрировал RMS. Вначале это обычное значение, превышающее 0. Потом внезапно оно становится 0.
Я использую Java Android AudioRecorder (код приведен ниже). Когда я начинаю запись, среднеквадратичное значение — это нормальное значение, превышающее 0. Через несколько секунд после записи среднеквадратичное значение получает только значение 0,0, что означает, что audioBuffer — это нулевой буфер. Как я могу это исправить? Спасибо. [code] recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, channelConfig, audioFormat, bufferSize); while (isRecording) { int read = recorder.read(audioBuffer, 0, bufferSize); if (read > 0) { long currentTime = System.currentTimeMillis();
// Calculate the RMS (root mean square) value of the audio buffer double sum = 0; for (int i = 0; i < read; i += 2) { short sample = (short) ((audioBuffer[i + 1] 0) { long currentTime = System.currentTimeMillis();
// Calculate the RMS (root mean square) value of the audio buffer double sum = 0; for (int i = 0; i < read; i += 2) { short sample = (short) ((audioBuffer[i + 1] SILENCE_THRESHOLD) { lastVoiceTimestamp = currentTime; Log.i(Config.TAG, "Loud."); // If the output stream is null, create a new file if (os == null) { try { os = new FileOutputStream(filePath); } catch (FileNotFoundException e) { e.printStackTrace(); } }
// Write audio data to file try { os.write(audioBuffer, 0, read); } catch (IOException e) { e.printStackTrace(); }
} else { Log.i(Config.TAG, "Silent "+ String.valueOf(rms)+" "+String.valueOf(recorder.getState())); // Check if silence duration has exceeded the threshold if (currentTime - lastVoiceTimestamp > SILENCE_DURATION) { Log.i(Config.TAG, "Silent Time Out "+ String.valueOf(rms)+" "+ String.valueOf(lastVoiceTimestamp)); // Close the current file and prepare for a new one if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } os = null;
// Encode the recorded PCM data to M4A String m4aFilePath = filePath.replace(".pcm", ".m4a"); convertPcmToM4a(filePath, m4aFilePath); filePath = audioDir + "/" + "record_" + System.currentTimeMillis() + ".pcm"; // Trigger transcription and translation new Thread(() -> { triggerTranscriptionAndTranslation(m4aFilePath); }).start(); Log.i(Config.TAG, "New audio "+ filePath); } } else { // If within silence duration, continue writing silence (optional) if (os != null) { try { os.write(audioBuffer, 0, read); } catch (IOException e) { e.printStackTrace(); } } } } } }
// Clean up when recording stops if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } os = null;
// Encode the last recorded PCM data to M4A String m4aFilePath = filePath.replace(".pcm", ".m4a"); convertPcmToM4a(filePath, m4aFilePath);
// Trigger transcription and translation new Thread(() -> { triggerTranscriptionAndTranslation(m4aFilePath); }).start(); } } [/code] Я зарегистрировал RMS. Вначале это обычное значение, превышающее 0. Потом внезапно оно становится 0.