diff --git a/app/src/main/java/com/github/jing332/tts_server_android/help/audio/Sonic.java b/app/src/main/java/com/github/jing332/tts_server_android/help/audio/Sonic.java index 6e2c6865c..98e65cb7b 100644 --- a/app/src/main/java/com/github/jing332/tts_server_android/help/audio/Sonic.java +++ b/app/src/main/java/com/github/jing332/tts_server_android/help/audio/Sonic.java @@ -9,40 +9,6 @@ package com.github.jing332.tts_server_android.help.audio; public class Sonic { - - public static byte[] upSampling(byte[] data, int inFrequency, int outFrequency) { - if (data.length < 4) { - return data; - } - - int v1, v2; - short value; - double pos = 0; - int length = data.length; - double scale = (double) inFrequency / (double) outFrequency; - byte[] output = new byte[2 * (int) ((length / 2) / scale)]; - for (int i = 0; i < output.length / 2; i++) { - int inPos = (int) pos; - double proportion = pos - inPos; - - int inRealPos = inPos * 2; - if (inRealPos >= length - 3) { - inRealPos = length - 4; - proportion = 1; - } - v1 = ((data[inRealPos] & 255) | (data[inRealPos + 1] << 8)); - v2 = ((data[inRealPos + 2] & 255) | (data[inRealPos + 3] << 8)); - - value = (short) (v1 * (1 - proportion) + v2 * proportion); - - output[i * 2] = (byte) (value & 255); - output[i * 2 + 1] = (byte) ((value >> 8) & 255); - - pos += scale; - } - return output; - } - private static final int SONIC_MIN_PITCH = 65; private static final int SONIC_MAX_PITCH = 400; // This is used to down-sample some inputs to improve speed @@ -479,6 +445,7 @@ public int readUnsignedByteFromStream( return numSamples; } + public byte[] readBytesFromStream(int maxBytes) { int maxSamples = maxBytes / (2 * numChannels); int numSamples = numOutputSamples; @@ -503,6 +470,7 @@ public byte[] readBytesFromStream(int maxBytes) { return outBuffer; } + // Read unsigned byte data out of the stream. Sometimes no data will be available, and zero // is returned, which is not an error condition. public int readBytesFromStream( diff --git a/app/src/main/java/com/github/jing332/tts_server_android/service/systts/help/TextToSpeechManager.kt b/app/src/main/java/com/github/jing332/tts_server_android/service/systts/help/TextToSpeechManager.kt index 30a3e9717..715800912 100644 --- a/app/src/main/java/com/github/jing332/tts_server_android/service/systts/help/TextToSpeechManager.kt +++ b/app/src/main/java/com/github/jing332/tts_server_android/service/systts/help/TextToSpeechManager.kt @@ -438,15 +438,14 @@ class TextToSpeechManager(val context: Context) : ITextToSpeechSynthesizer + onDone = { data.done.invoke() }) { pcmAudio -> if (sonic == null) onPcmAudio.invoke(pcmAudio) else { sonic.volume = audioParams.volume sonic.speed = audioParams.speed sonic.pitch = audioParams.pitch sonic.rate = srcSampleRate.toFloat() / targetSampleRate.toFloat() - + sonic.flushStream() sonic.writeBytesToStream(pcmAudio, pcmAudio.size) onPcmAudio.invoke(sonic.readBytesFromStream(sonic.samplesAvailable())) }