1

我想为我的项目实现一个特定的功能,包括sample-default.wav在后台连续播放一个sample-specific.wav文件,并在某些特定条件匹配时播放一个文件。我真正想要的是,当sample-specific.wav文件运行时,文件量sample-default.wav变为 0(或者简单地说,我想在运行sample-default.wav时静音文件sample-specific.wav)。

我正在尝试使用alsa-libraryfor c 来实现此功能。我已经尝试了几乎所有方法来实现这一点,但似乎没有什么对我有用。


我目前的做法

我正在尝试使用系统声音离开dmix插件添加两个虚拟声音设备。我搜索了谷歌,发现我需要编辑文件来创建不同的声音设备。alsadefault/etc/asound.conf

我添加了两个声音设备,分别命名为notificationsample-sound。我当前/etc/asound.conf的文件如下所示:-

pcm.notification {
    type dmix
    ipc_key 1024
    slave {
        pcm "hw:1,0"
    }
}

ctl.notification {
     type hw
     card 2
}

pcm.sample-sound {
     type dmix
     ipc_key 1025
    slave {
         pcm "hw:1,0"
    }
}

ctl.sample-sound {
     type hw
    card 3
}

aplay当我使用以下命令播放sample-default.wav文件时,这可以很好地工作:-aplay

aplay -D plug:notification sample-default.wav

它可以工作,但是当我尝试运行以下代码以在我的设备上sample-default.wav使用时:-alsa librarynotification

#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
#include <pthread.h>

void default_sound (char * str) {
    snd_pcm_t *handle;
    snd_pcm_hw_params_t *params;
    snd_pcm_uframes_t frames;

    /* Open PCM device for playback */
    int status = snd_pcm_open(&handle, "notification", SND_PCM_STREAM_PLAYBACK, 0);
    if (status < 0) {
        printf("Unable to open pcm deveice%s\n", snd_strerror(status));
        return;
    }

    /* Allocate a hardware parameter obejct */
    snd_pcm_hw_params_alloca(&params);

    /* Fill it with default values */
    snd_pcm_hw_params_any(handle, params);

    /* Set the desired hardware parameters */

    /* Interleaved mode */
    snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);

    /* Signed 16-bit little-endian format */
    snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);

    /* Single Channel (mono) */
    snd_pcm_hw_params_set_channels(handle, params, 1);

    /* sampling rate */
    unsigned int sample_rate = 16000;
    int dir;
    snd_pcm_hw_params_set_rate_near(handle, params, &sample_rate, &dir);

    /* set period size to 32 frames */
    frames = 32;
    snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir);

    /* Write the parameters to the driver */
    status = snd_pcm_hw_params(handle, params);
    if(status < 0) {
        printf("Unable to set hw params: %s\n", snd_strerror(status));
        return;
    }

    /* Use a buffer large enough to hold one period */
    snd_pcm_hw_params_get_period_size(params, &frames, &dir);

    int size = frames * 2;
    char *buffer = (char *)malloc(size);

    int readfd = open("sample-call.wav", O_RDONLY);
    if(readfd < 0) {
        printf("Error in opening wav file\n");
        exit(1);
    }


    int readval;
    while(readval = read(readfd, buffer, size) > 0) {
        status = snd_pcm_writei(handle, buffer, frames);
        if(status == -EPIPE) {
            printf("underrun occured\n");
            snd_pcm_prepare(handle);
        }
        else if(status < 0) {
            printf("Error from writei: %s\n", snd_strerror(status));
        }
    }

    snd_pcm_drain(handle);
    snd_pcm_close(handle);
    free(buffer);
}

int main() {
    pthread_t pthread_id;
    int status = pthread_create(&pthread_id, NULL, default_sound, "running");
    if (status != 0) {
        printf("Error in creating thread\n");
    }
    pthread_join(pthread_id, NULL);

    return 0;
}

它编译成功但抛出运行时错误:-

8211 segmentation fault (core dumped)  ./play-multiple-sound

我不知道上面的代码出了什么问题。

notification如果上面的代码工作正常,那么我将在两个设备(和)上播放两个声音文件,并使用我已经实现sample-sound的库控制这些设备的音量。alsa-mixer

我在过去几周试图实现这一目标,但似乎没有一个解决方案对我有用,所以请帮我解决这个问题,如果你知道更好的方法,请提出建议。


4

0 回答 0