Skip to content

Commit

Permalink
Revert "refactor: Sonic flush stream"
Browse files Browse the repository at this point in the history
This reverts commit 7b589f4.
  • Loading branch information
jing332 committed Feb 11, 2024
1 parent 7b589f4 commit dd8ab0f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,40 @@
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
Expand Down Expand Up @@ -445,7 +479,6 @@ public int readUnsignedByteFromStream(
return numSamples;
}


public byte[] readBytesFromStream(int maxBytes) {
int maxSamples = maxBytes / (2 * numChannels);
int numSamples = numOutputSamples;
Expand All @@ -470,7 +503,6 @@ 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,14 +438,15 @@ class TextToSpeechManager(val context: Context) : ITextToSpeechSynthesizer<IText
else Sonic(txtTts.tts.audioFormat.sampleRate, 1)
txtTts.playAudio(
sysRate, sysPitch, data.audio,
onDone = { data.done.invoke() }) { pcmAudio ->
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()))
}
Expand Down

0 comments on commit dd8ab0f

Please sign in to comment.