Skip to content

Commit

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


public byte[] readBytesFromStream(int maxBytes) {
int maxSamples = maxBytes / (2 * numChannels);
int numSamples = numOutputSamples;
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,15 +438,14 @@ 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 7b589f4

Please sign in to comment.