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

Audio input device selection #30

Closed
revmischa opened this issue Feb 13, 2018 · 13 comments · Fixed by #515
Closed

Audio input device selection #30

revmischa opened this issue Feb 13, 2018 · 13 comments · Fixed by #515

Comments

@revmischa
Copy link
Collaborator

revmischa commented Feb 13, 2018

In projectM-sdl there needs to be a way to select which input device you want to use.

@spyderboy92
Copy link

Hey, am a new guy, never made any PR, can you assist me.

@spyderboy92
Copy link

Or provide me direction. Thanks

@revmischa
Copy link
Collaborator Author

Sure... you can come chat in #projectM on freenode IRC if you want
There is some code already for selecting the input device, it needs to be improved.
Read the SDL2 docs for audio input capture

SDL_AudioDeviceID projectMSDL::selectAudioInput(int _count) {
    // ask the user which capture device to use
    // printf("Please select which audio input to use:\n");
    printf("Detected devices:\n");
    for (int i = 0; i < _count; i++) {
        printf("  %i: 🎤%s\n", i, SDL_GetAudioDeviceName(i, true));
    }

    return 0;
}

int projectMSDL::openAudioInput() {
    // get audio driver name (static)
    const char* driver_name = SDL_GetCurrentAudioDriver();
    SDL_Log("Using audio driver: %s\n", driver_name);

    // get audio input device
    unsigned int i, count2 = SDL_GetNumAudioDevices(true);  // capture, please
    if (count2 == 0) {
        SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "No audio capture devices found");
        SDL_Quit();
    }
    for (i = 0; i < count2; i++) {
        SDL_Log("Found audio capture device %d: %s", i, SDL_GetAudioDeviceName(i, true));
    }

    SDL_AudioDeviceID selectedAudioDevice = 0;  // device to open
    if (count2 > 1) {
        // need to choose which input device to use
	selectedAudioDevice = selectAudioInput(count2);
	if (selectedAudioDevice > count2) {
            SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "No audio input device specified.");
            SDL_Quit();
        }
    }

    // params for audio input
    SDL_AudioSpec want, have;

    // requested format
    SDL_zero(want);
    want.freq = 48000;
    want.format = AUDIO_F32;  // float
    want.channels = 2;
    want.samples = 512;
    want.callback = projectMSDL::audioInputCallbackF32;
    want.userdata = this;

    audioDeviceID = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(selectedAudioDevice, true), true, &want, &have, 0);

    if (audioDeviceID == 0) {
        SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "Failed to open audio capture device: %s", SDL_GetError());
        SDL_Quit();
    }

    // read characteristics of opened capture device
    SDL_Log("Opened audio capture device %i: %s", audioDeviceID, SDL_GetAudioDeviceName(selectedAudioDevice, true));
    SDL_Log("Sample rate: %i, frequency: %i, channels: %i, format: %i", have.samples, have.freq, have.channels, have.format);
    audioChannelsCount = have.channels;
    audioSampleRate = have.freq;
    audioSampleCount = have.samples;
    audioFormat = have.format;
    audioInputDevice = audioDeviceID;
    return 1;
}

is the code in pmSDL.cpp to find the available devices and select them
something more user-friendly would be nice. not sure what.

@spyderboy92
Copy link

Lots of thanks for assisting me.

@revmischa
Copy link
Collaborator Author

SDL_AudioDeviceID projectMSDL::selectAudioInput(int _count) {
    // ask the user which capture device to use
    // printf("Please select which audio input to use:\n");
    printf("Detected devices:\n");
    for (int i = 0; i < _count; i++) {
        printf("  %i: 🎤%s\n", i, SDL_GetAudioDeviceName(i, true));
    }

    return 0;
}

This should return the index of the desired device

@ICShelly76
Copy link

ProjectX should allow u to lock in the songs with the effects that u choose when swiped left or right etc.

@dreamlayers
Copy link

This is very annoying in Linux. If I'm running a music visualization program I'll probably want to run it on the sound being played by the computer, not line in. But SDL makes that impossible without editing and recompiling!

First, SDL will ignore PulseAudio sources which are monitors: https://github.com/spurious/SDL-mirror/blob/cc7a10af3465de5d452a7f3d263ee1f36eeaed71/src/audio/pulseaudio/SDL_pulseaudio.c#L680

Second, SDL will set the DONT_MOVE flag if an audio source name is specified, so you can't move the input to a different source: https://github.com/spurious/SDL-mirror/blob/cc7a10af3465de5d452a7f3d263ee1f36eeaed71/src/audio/pulseaudio/SDL_pulseaudio.c#L632
(pavucontrol will offer you GUI elements for doing the move but not actually move. pactl will unhelpfully say "Failure: Invalid argument".)

Right now I edited the SDL_OpenAudioDevice call to make the audio source name NULL, allowing the source to be moved to the monitor.

Switching to an ALSA driver via SDL_AUDIODRIVER=alsa in the environment doesn't help either. For some reason the pulse_monitor source defined in ~/.asoundrc isn't seen by SDL.

@labkey-matthewb
Copy link
Contributor

Up vote this bug

https://bugzilla.libsdl.org/show_bug.cgi?id=4187

@ghost
Copy link

ghost commented Sep 12, 2020

Hi !
I am a beginner and i would like to contribute , can you please guide me ?

@kblaschke
Copy link
Member

SDL moved to GitHub, issue as well: libsdl-org/SDL#2917

@icculus
Copy link

icculus commented Jul 27, 2021

SDL moved to GitHub, issue as well: libsdl-org/SDL#2917

We just fixed this issue in SDL, which will be in the 2.0.16 release, that we're working on wrapping up right now. If you get a chance, please test the latest in revision control and make sure it meets your needs!

@kblaschke
Copy link
Member

SDL moved to GitHub, issue as well: libsdl-org/SDL#2917

We just fixed this issue in SDL, which will be in the 2.0.16 release, that we're working on wrapping up right now. If you get a chance, please test the latest in revision control and make sure it meets your needs!

Big thanks for fixing it!

Will test it tomorrow or the day after when I'm back home and give feedback whether it worked or not.

@kblaschke
Copy link
Member

kblaschke commented Jul 28, 2021

Setting the newly added SDL_HINT_AUDIO_INCLUDE_MONITORS hint works perfectly, PR is open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants