Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Not-for-merge]Support playing in offline-tts-c-api #387

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion c-api-examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ add_executable(decode-file-c-api decode-file-c-api.c)
target_link_libraries(decode-file-c-api sherpa-onnx-c-api cargs)

add_executable(offline-tts-c-api offline-tts-c-api.c)
target_link_libraries(offline-tts-c-api sherpa-onnx-c-api cargs)
target_link_libraries(offline-tts-c-api sherpa-onnx-c-api cargs portaudio)
96 changes: 96 additions & 0 deletions c-api-examples/offline-tts-c-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <string.h>

#include "cargs.h"
#include "portaudio.h"
#include "sherpa-onnx/c-api/c-api.h"

static struct cag_option options[] = {
Expand Down Expand Up @@ -111,6 +112,100 @@ static void ShowUsage() {
exit(0);
}

int32_t g_i = 0;
static int patestCallback(const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo *timeInfo,
PaStreamCallbackFlags statusFlags, void *userData) {
SherpaOnnxGeneratedAudio *data = (SherpaOnnxGeneratedAudio *)userData;
float *out = (float *)outputBuffer;

(void)timeInfo; /* Prevent unused variable warnings. */
(void)statusFlags;
(void)inputBuffer;

for (int32_t i = 0; i < framesPerBuffer; i++) {
if (g_i >= data->n) {
return paComplete;
}

*out++ = data->samples[g_i + i]; /* left */
}
g_i += framesPerBuffer;

return paContinue;
}

static void StreamFinished(void *userData) {
SherpaOnnxGeneratedAudio *data = (SherpaOnnxGeneratedAudio *)userData;
printf("Stream Completed. Number of samples %d\n", data->n);
}

#define FRAMES_PER_BUFFER (64)

PaError Play(const SherpaOnnxGeneratedAudio *audio) {
printf("n: %d, sample_rate: %d\n", audio->n, audio->sample_rate);
PaStreamParameters outputParameters;
PaStream *stream;
PaError err;

err = Pa_Initialize();
if (err != paNoError) goto error;

outputParameters.device =
Pa_GetDefaultOutputDevice(); /* default output device */

if (outputParameters.device == paNoDevice) {
fprintf(stderr, "Error: No default output device.\n");
goto error;
}

const PaDeviceInfo *info = Pa_GetDeviceInfo(outputParameters.device);
fprintf(stderr, " Name: %s\n", info->name);
fprintf(stderr, " Max output channels: %d\n", info->maxOutputChannels);

outputParameters.channelCount = 1; /* Mono output */
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
outputParameters.suggestedLatency =
Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;

err = Pa_OpenStream(&stream, NULL, /* no input */
&outputParameters, audio->sample_rate, FRAMES_PER_BUFFER,
paClipOff, /* we won't output out of range samples so
don't bother clipping them */
patestCallback, (void *)audio);
if (err != paNoError) goto error;

err = Pa_SetStreamFinishedCallback(stream, &StreamFinished);
if (err != paNoError) goto error;

err = Pa_StartStream(stream);
if (err != paNoError) goto error;

float num_seconds = audio->n / (float)audio->sample_rate;
printf("Play for %.3f seconds.\n", num_seconds);
while (Pa_IsStreamActive(stream) == 1) {
Pa_Sleep(0.1 * 1000); // 100ms
}

err = Pa_StopStream(stream);
if (err != paNoError) goto error;
err = Pa_CloseStream(stream);
if (err != paNoError) goto error;

Pa_Terminate();
printf("Test finished.\n");

return err;
error:
Pa_Terminate();
fprintf(stderr, "An error occurred while using the portaudio stream\n");
fprintf(stderr, "Error number: %d\n", err);
fprintf(stderr, "Error message: %s\n", Pa_GetErrorText(err));
return err;
}

int32_t main(int32_t argc, char *argv[]) {
cag_option_context context;
char identifier;
Expand Down Expand Up @@ -190,6 +285,7 @@ int32_t main(int32_t argc, char *argv[]) {

SherpaOnnxWriteWave(audio->samples, audio->n, audio->sample_rate, filename);

Play(audio);
SherpaOnnxDestroyOfflineTtsGeneratedAudio(audio);
SherpaOnnxDestroyOfflineTts(tts);

Expand Down
Loading