Skip to content

Commit

Permalink
Change app name
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj committed Dec 4, 2024
1 parent 945a42e commit 0cef3c6
Show file tree
Hide file tree
Showing 14 changed files with 717 additions and 625 deletions.
841 changes: 449 additions & 392 deletions harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/non-streaming-tts.cc

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <string>
#include <vector>

#include "macros.h"
#include "macros.h" // NOLINT
#include "napi.h" // NOLINT

static std::vector<std::string> GetFilenames(NativeResourceManager *mgr,
Expand Down
15 changes: 8 additions & 7 deletions harmony-os/SherpaOnnxTts/entry/oh-package-lock.json5

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions harmony-os/SherpaOnnxTts/entry/oh-package.json5
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"author": "",
"license": "",
"dependencies": {
// "sherpa_onnx": "1.10.32",
"sherpa_onnx": "file:./sherpa_onnx_9.har"
"sherpa_onnx": "1.10.32",
}
}

287 changes: 159 additions & 128 deletions harmony-os/SherpaOnnxTts/entry/src/main/ets/pages/Index.ets
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function toInt16Samples(samples: Float32Array): Int16Array {
@Entry
@Component
struct Index {
@State currentIndex: number = 0;
@State title: string = 'Next-gen Kaldi: Text-to-speech';
@State info: string = '';
@State btnStartCaption: string = 'Start';
Expand All @@ -61,12 +62,13 @@ struct Index {
@State progress: number = 0;
@State sid: string = '0';
@State speechSpeed: string = '1.0';
private cancelled: boolean = false;
@State isGenerating: boolean = false;
@State initTtsDone: boolean = false;
@State ttsGeneratedDone: boolean = true;
@State numSpeakers: number = 1;
@State initAudioDone: boolean = false;
private controller: TabsController = new TabsController();
private cancelled: boolean = false;
private sampleRate: number = 0;
private startTime: number = 0;
private stopTime: number = 0;
Expand Down Expand Up @@ -188,10 +190,8 @@ RTF = ${elapsedSeconds.toFixed(2)}/${audioDuration.toFixed(2)} = ${RTF.toFixed(3

this.ttsGeneratedDone = true;

console.log(`buffer size ${this.sampleBuffer.size()}`);

if (this.audioRenderer && this.audioRenderer?.state != audio.AudioState.STATE_RUNNING && this.sampleBuffer.size()==0) {
console.log(`here buffer size ${this.sampleBuffer.size()}`);
if (this.audioRenderer && this.audioRenderer?.state != audio.AudioState.STATE_RUNNING &&
this.sampleBuffer.size() == 0) {
this.sampleBuffer.push(samples);
this.progress = 1;
this.audioRenderer.start();
Expand All @@ -209,148 +209,179 @@ RTF = ${elapsedSeconds.toFixed(2)}/${audioDuration.toFixed(2)} = ${RTF.toFixed(3
this.workerInstance.postMessage({ msgType: 'init-tts', context: getContext() });
}

@Builder
TabBuilder(title: string, targetIndex: number, selectedImg: Resource, normalImg: Resource) {
Column() {
Image(this.currentIndex == targetIndex ? selectedImg : normalImg).size({ width: 25, height: 25 })
Text(title).fontColor(this.currentIndex == targetIndex ? '#28bff1' : '#8a8a8a')
}.width('100%').height(50).justifyContent(FlexAlign.Center).onClick(() => {
this.currentIndex = targetIndex;
this.controller.changeIndex(this.currentIndex);
})
}

build() {
Row() {
Column({ space: 10 }) {
Text(this.title).fontSize(20).fontWeight(FontWeight.Bold);
if (this.numSpeakers > 1) {
Row({ space: 10 }) {
Text(`Speaker ID (0-${this.numSpeakers-1})`)
.width('60%')

TextInput({ text: this.sid })
.onChange((text) => {
this.sid = text.trim();
Column() {
Tabs({ barPosition: BarPosition.End, controller: this.controller }) {
TabContent() {
Column({ space: 10 }) {
Text(this.title).fontSize(20).fontWeight(FontWeight.Bold);
if (this.numSpeakers > 1) {
Row({ space: 10 }) {
Text(`Speaker ID (0-${this.numSpeakers - 1})`).width('60%')

TextInput({ text: this.sid }).onChange((text) => {
this.sid = text.trim();
}).width('20%')
}.justifyContent(FlexAlign.Center)
}

Row() {
Text('Speech speed').width('60%');

TextInput({ text: this.speechSpeed }).onChange((text) => {
this.speechSpeed = text.trim();
}).width('20%')
}

Row({ space: 10 }) {
Button(this.btnStartCaption).enabled(this.btnStartEnabled).onClick(async () => {
let sid = parseInt(this.sid);
if (sid.toString() != this.sid) {
this.info = 'Please input a valid speaker ID';
return;
}

let speed = parseFloat(this.speechSpeed);
if (isNaN(speed)) {
this.info = 'Please enter a valid speech speed';
return;
}

if (speed <= 0) {
this.info = 'Please enter a positive speech speed';
return;
}

if (this.workerInstance && this.initTtsDone) {
this.info = 'Generating...';
this.cancelled = false;
this.finalSamples = null;
this.sampleBuffer.reset();
this.ttsGeneratedDone = false;
this.progress = 0;

this.btnStartEnabled = false;
this.btnStopEnabled = true;
this.btnSaveEnabled = false;
console.log(`sending ${this.inputText}`)
this.ttsGeneratedDone = false;
this.startTime = await systemTime.getRealTime();
this.workerInstance?.postMessage({
msgType: 'tts-generate',
text: this.inputText,
sid: sid,
speed: speed,
});
this.isGenerating = true;
this.info = '';
} else {
this.info = 'Failed to initialize tts model';
this.btnStartEnabled = false;
}
});

Button(this.btnStopCaption).enabled(this.btnStopEnabled).onClick(() => {
this.ttsGeneratedDone = true;
this.btnStartEnabled = true;
this.btnStopEnabled = false;
this.sampleBuffer.reset();
this.cancelled = true;
this.isGenerating = false;

if (this.workerInstance && this.initTtsDone) {
this.workerInstance.postMessage({ msgType: 'tts-generate-cancel' });
}
this.audioRenderer?.stop();
})
.width('20%')
}.justifyContent(FlexAlign.Center)
}

Row() {
Text('Speech speed')
.width('60%');
Button(this.btnSaveCaption).enabled(this.btnSaveEnabled).onClick(() => {
if (!this.finalSamples || this.finalSamples.length == 0) {

TextInput({ text: this.speechSpeed })
.onChange((text) => {
this.speechSpeed = text.trim();
})
.width('20%')
}
this.btnSaveEnabled = false;
return;
}

Row({ space: 10 }) {
Button(this.btnStartCaption).enabled(this.btnStartEnabled).onClick(async () => {
let sid = parseInt(this.sid);
if (sid.toString() != this.sid) {
this.info = 'Please input a valid speaker ID';
return;
}
let uri: string = '';

let speed = parseFloat(this.speechSpeed);
if (isNaN(speed)) {
this.info = 'Please enter a valid speech speed';
return;
}
const audioOptions = new picker.AudioSaveOptions(); // audioOptions.newFileNames = ['o.wav'];

if (speed <= 0) {
this.info = 'Please enter a positive speech speed';
return;
}
const audioViewPicker = new picker.AudioViewPicker();

if (this.workerInstance && this.initTtsDone) {
this.info = 'Generating...';
this.cancelled = false;
this.finalSamples = null;
this.sampleBuffer.reset();
this.ttsGeneratedDone = false;
this.progress = 0;

this.btnStartEnabled = false;
this.btnStopEnabled = true;
this.btnSaveEnabled = false;
console.log(`sending ${this.inputText}`)
this.ttsGeneratedDone = false;
this.startTime = await systemTime.getRealTime();
this.workerInstance?.postMessage({
msgType: 'tts-generate',
text: this.inputText,
sid: sid,
speed: speed,
audioViewPicker.save(audioOptions).then((audioSelectResult: Array<string>) => {
uri = audioSelectResult[0];
if (this.finalSamples) {
savePcmToWav(uri, toInt16Samples(this.finalSamples), this.sampleRate);
console.log(`Saved to ${uri}`);
this.info += `\nSaved to ${uri}`;
}
});
});
this.isGenerating = true;
this.info = '';
} else {
this.info = 'Failed to initialize tts model';
this.btnStartEnabled = false;
}
});

Button(this.btnStopCaption).enabled(this.btnStopEnabled).onClick(() => {
this.ttsGeneratedDone = true;
this.btnStartEnabled = true;
this.btnStopEnabled = false;
this.sampleBuffer.reset();
this.cancelled = true;
this.isGenerating = false;

if (this.workerInstance && this.initTtsDone) {
this.workerInstance.postMessage({ msgType: 'tts-generate-cancel'});
}
this.audioRenderer?.stop();
})

Button(this.btnSaveCaption).enabled(this.btnSaveEnabled).onClick(() => {
if (!this.finalSamples || this.finalSamples.length == 0) {

this.btnSaveEnabled = false;
return;
if (this.info != '') {
TextArea({ text: this.info }).focusable(false);
}
if (this.progress > 0) {
Row() {
Progress({ value: 0, total: 100, type: ProgressType.Capsule })
.width('80%')
.height(20)
.value(this.progress * 100);

Text(`${(this.progress * 100).toFixed(2)}%`).width('15%')
}.width('100%').justifyContent(FlexAlign.Center)
}

let uri: string = '';
TextArea({ placeholder: 'Input text for TTS and click the start button' })
.width('100%')
.height('100%')
.focusable(this.isGenerating == false && this.initTtsDone)
.onChange((text) => {
this.inputText = text;
if (text.trim() == '') {
this.btnStartEnabled = false;
return;
}
this.btnStartEnabled = true;
})
}.width('100%')

// see https://composeicons.com/
}.tabBar(this.TabBuilder('TTS', 0, $r('app.media.home'), $r('app.media.home')))

const audioOptions = new picker.AudioSaveOptions(); // audioOptions.newFileNames = ['o.wav'];
TabContent() {
Column({space: 10}) {
Text(this.title).fontSize(20).fontWeight(FontWeight.Bold);
TextArea({text: `
Everyting is open-sourced.

const audioViewPicker = new picker.AudioViewPicker();
It runs locally, without accessing the network

audioViewPicker.save(audioOptions).then((audioSelectResult: Array<string>) => {
uri = audioSelectResult[0];
if (this.finalSamples) {
savePcmToWav(uri, toInt16Samples(this.finalSamples), this.sampleRate);
console.log(`Saved to ${uri}}`);
this.info += `\nSaved to ${uri}`;
}
});
See also https://github.com/k2-fsa/sherpa-onnx

})
}
新一代 Kaldi QQ 和微信交流群: 请看

if (this.info != '') {
TextArea({ text: this.info }).focusable(false);
}
if (this.progress > 0) {
Row() {
Progress({ value: 0, total: 100, type: ProgressType.Capsule })
.width('80%')
.height(20)
.value(this.progress * 100);

Text(`${(this.progress * 100).toFixed(2)}%`).width('15%')
}.width('100%').justifyContent(FlexAlign.Center)
}
https://k2-fsa.github.io/sherpa/social-groups.html

TextArea({ placeholder: 'Input text for TTS and click the start button' })
.width('100%')
.height('100%')
.focusable(this.isGenerating == false && this.initTtsDone)
.onChange((text) => {
this.inputText = text;
if (text.trim() == '') {
this.btnStartEnabled = false;
return;
}
this.btnStartEnabled = true;
})
}.width('100%')
}.height('100%')
微信公众号: 新一代 Kaldi
`}).width('100%')
.height('100%')
.focusable(false)
}.justifyContent(FlexAlign.Start)
}.tabBar(this.TabBuilder('Help', 1, $r('app.media.info'), $r('app.media.info')))
}.scrollable(false)
}
}

private audioPlayCallback = (buffer: ArrayBuffer) => {
Expand Down
Loading

0 comments on commit 0cef3c6

Please sign in to comment.