Gtk音频播放器。如何在歌曲结束前停止声音

时间:2013-03-21 16:50:44

标签: gtk audio-player portaudio libsndfile

我正在开发一个非常简单的音频播放器。它使用Gtk和portaudio / libsndfile。我创建了一个简单的测试界面,只有几个按钮,如浏览,播放等。我的播放器正确选择文件名,按下播放后开始播放。但是一切都等待播放完成。没有什么是活跃的,我想知道如何在它完成之前停止它。

我的Gtk代码是:

#include <stdio.h>
#include <gtk\gtk.h>
static GtkWidget *window;
const char *filename;

static void play_file (GtkButton *button, gpointer data)
{
    sndFile_play(filename);
}
static gboolean delete_event( GtkWidget *widget,
                              GdkEvent  *event,
                              gpointer   data )
{
    return FALSE;
}

static void destroy( GtkWidget *widget,
                     gpointer   data )
{
    gtk_main_quit ();
}

int main( int   argc,
          char *argv[] )
{

    GtkWidget *button;
    GtkWidget *box1;

    gtk_init (&argc, &argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (window), "MyPlayer");

    g_signal_connect (window, "delete-event",
              G_CALLBACK (delete_event), NULL);

    g_signal_connect (window, "destroy",
              G_CALLBACK (destroy), NULL);

    gtk_container_set_border_width (GTK_CONTAINER (window), 20);

    box1 = gtk_hbox_new (FALSE, 0);
    gtk_container_add (GTK_CONTAINER (window), box1);

    button = gtk_button_new_with_label ("Browse...");
    g_signal_connect (button, "clicked",
        G_CALLBACK (browse_clicked), NULL); //not given here
    gtk_box_pack_start (GTK_BOX(box1), button, TRUE, TRUE, 0);
    gtk_widget_show (button);

    button = gtk_button_new_with_label ("Play");
    g_signal_connect (button, "clicked",
        G_CALLBACK (play_file), NULL);
    gtk_box_pack_start (GTK_BOX(box1), button, TRUE, TRUE, 0);
    gtk_widget_show (button);

    gtk_widget_show (box1);
    gtk_widget_show (window);

    gtk_main ();

    return 0;
}

如果有必要,我会提供播放声音的代码:

#include <stdio.h>
#include <stdlib.h>
#include <portaudio.h>
#include <sndfile.h>

#define FRAMES_PER_BUFFER (1024)
#define PA_SAMPLE_TYPE  paInt16
typedef short SAMPLE;
#define BUFFER_LEN  128
#define MAX_CHANNELS 2

int sndFile_play (const char *infilename){
    PaStreamParameters outputParameters;
    PaStream *stream;
    PaError err;
    static short data[BUFFER_LEN];
    SNDFILE      *infile;
    SF_INFO     sfinfo;
    sf_count_t  readcount;
    int channels;
    int srate;

    err = Pa_Initialize();

    if ( !( infile = sf_open( infilename, SFM_READ, &sfinfo ) ) ) {
        printf ("Not able to open input file %s.\n", infilename);
        puts (sf_strerror (NULL));
        return  1;
    }

    if ( sfinfo.channels > MAX_CHANNELS ){
        printf ("Not able to process more than %d channels\n", MAX_CHANNELS);
        return  1;
    }
    /* FILE INFO */

    channels = sfinfo.channels; // убрать channels
    printf("number of channels %d\n", sfinfo.channels);

    srate = sfinfo.samplerate; //убрать 
    printf("sample rate %d\n", sfinfo.samplerate);

    outputParameters.device = Pa_GetDefaultOutputDevice();
    outputParameters.channelCount = sfinfo.channels;
    outputParameters.sampleFormat =  PA_SAMPLE_TYPE;
    outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
    outputParameters.hostApiSpecificStreamInfo = NULL;

    printf("Begin playback.....\n"); fflush(stdout);
    err = Pa_OpenStream(
        &stream,
        NULL,
        &outputParameters,
        sfinfo.samplerate,
        FRAMES_PER_BUFFER,
        paClipOff,
        NULL,
        NULL );

    if( stream ) {

        err = Pa_StartStream( stream );
        printf("Waiting for playback to finish....\n"); fflush(stdout);

        while ( ( readcount = sf_read_short( infile, data,  BUFFER_LEN*sfinfo.channels ) ) ){
            err = Pa_WriteStream( stream, data, BUFFER_LEN );
        }

        err = Pa_CloseStream( stream );
        printf("Done.\n"); fflush(stdout);

    }

    sf_close( infile );

    Pa_Terminate();
    return 0;
}

1 个答案:

答案 0 :(得分:2)

您的代码

while ((readcount = sf_read_short( infile, data,  BUFFER_LEN*sfinfo.channels ) ) ){
    err = Pa_WriteStream( stream, data, BUFFER_LEN );
}

正在停止Gtk主循环的进展。对于GUI应用程序,您不能长时间运行,而回调中的循环或GUI将冻结。有一些解决方案

1)使用类似

之类的东西在while循环内驱动运行循环
...

while (gtk_events_pending ()) {
    gtk_main_iteration ();
}

...

这意味着每次执行音频循环时,都会处理已建立的所有gtk事件。

2)从空闲回调中驱动音频。这允许GUI像平常一样工作,然后当它不忙时,将调用你的音频回调,你可以播放下一个缓冲区。

3)在单独的线程中驱动音频循环。这将允许GUI在主线程上正常工作,音频将只播放。这带来了线程编程的所有标准危险

4)Portaudio有一个异步回调驱动模型。使用它,当Portaudio需要更多数据时,将调用您的音频回调。这也很难,因为你不应该在该回调中进行文件访问或内存分配。这里有更多详细信息:Portaudio callback documentation

老实说,这个基础级别的音频编程很复杂,我建议您使用像(例如)GStreamer这样的框架,它已经解决了所有这些复杂问题。