我正在尝试使用nodejs库https://github.com/duobeiyun/node-sdl-speaker在特定声卡的特定音频通道上播放声音,但是我的问题并不特定于该库。这是一个SDL音频问题。
通过修改src/SDLSpeaker.cpp,我可以使用SDL_OpenAudioDevice
而不是SDL_OpenAudio
选择声音设备。我用以下代码替换第62行:
// Select second audio device
if (int errorCode = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(1, 0), 0, &wanted_spec, NULL, SDL_AUDIO_ALLOW_FORMAT_CHANGE) < 0) {
[…]
现在,我想仅将声音输出强制为“左”。我认为我应该通过更改fill_audio
来做到这一点,但是我不知道该怎么做...:(
答案 0 :(得分:0)
我找到了解决方案! SDL不适合我的需要,SDL_MIXER很适合!
我正在基于编写的C代码编写节点插件:
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int continuer = 1;
SDL_Init(SDL_INIT_AUDIO);
Mix_Init(MIX_INIT_MP3);
int i, count = SDL_GetNumAudioDevices(0);
printf("-----------------------------------------\n");
printf("Available audio devices:\n");
for (i = 0; i < count; ++i)
{
printf(" - Audio device %d: %s\n", i, SDL_GetAudioDeviceName(i, 0));
}
printf("\n");
if (argc <= 2)
{
return EXIT_FAILURE;
}
char *deviceName = argv[1];
if (Mix_OpenAudioDevice(44100, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 1024,
deviceName, SDL_AUDIO_ALLOW_ANY_CHANGE) == -1)
{
printf("[SDL ERROR] :: %s\n\n", Mix_GetError());
return EXIT_FAILURE;
}
printf("Audio device \"%s\" opened with success\n", deviceName);
printf("-----------------------------------------\n\n");
int MAX_SOUNDS = argc - 3;
int currentSound = 0;
char **files; // = {"file1.wav", "ding.wav"};
files = (char **)malloc(MAX_SOUNDS * sizeof(char *));
for (int i = 0; i < MAX_SOUNDS; ++i)
{
files[i] = argv[3 + i];
}
Mix_AllocateChannels(1);
if (!Mix_SetPanning(0, argv[2][0] == '1' ? 255 : 0,
argv[2][0] == '1' ? 0 : 255))
{
printf("[SDL ERROR] :: Mix_SetPanning: %s\n", Mix_GetError());
}
Mix_Chunk *sounds[MAX_SOUNDS];
for (int i = 0; i < MAX_SOUNDS; ++i)
{
sounds[i] = Mix_LoadWAV(files[i]);
Mix_VolumeChunk(sounds[i], MIX_MAX_VOLUME);
}
Mix_PlayChannel(0, sounds[0], 0);
bool playing = true;
while (playing)
{
SDL_WaitEvent(NULL);
playing = Mix_Playing(0) != 0;
if (!playing && currentSound < MAX_SOUNDS)
{
++currentSound;
playing = true;
if (!Mix_SetPanning(0, argv[2][0] == '1' ? 255 : 0,
argv[2][0] == '1' ? 0 : 255))
{
printf("[SDL ERROR] :: Mix_SetPanning: %s\n", Mix_GetError());
}
Mix_PlayChannel(0, sounds[currentSound], 0);
}
printf("Channel 0: %s\r", playing ? "PLAYING" : "STOPPED");
}
for (int i = 0; i < MAX_SOUNDS; ++i)
{
Mix_FreeChunk(sounds[i]);
}
Mix_CloseAudio(); // Fermeture de l'API
printf("\n");
return EXIT_SUCCESS;
}
在UNIX上,要进行编译(必须在系统上安装SDL2和SDL2_Mixer dev依赖项):
$ cc test-sdl-mixer.cc `sdl2-config --cflags --libs` -lsdl2_mixer
编译后,该应用将在特定声卡的特定通道(仅向左或向右)上顺序播放声音文件。
$ ./a.out "Built-in Output" 1 chime.wav advert.wav
效果很好,但是在我编写的节点模块中,我必须使用child_process
fork()
才能在2个不同的通道或设备上播放2种声音,因为SDL2和SDL2_Mixer都不会创建播放器/调音台。
节点模块即将上市。