| 1 |
/* Libvisual-plugins - Standard plugins for libvisual |
| 2 |
* |
| 3 |
* Copyright (C) 2023 Sebastian Pipping <[email protected]> |
| 4 |
* |
| 5 |
* Authors: Sebastian Pipping <[email protected]> |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU Lesser General Public License as |
| 9 |
* published by the Free Software Foundation; either version 2.1 |
| 10 |
* of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <stdlib.h> // malloc |
| 23 |
|
| 24 |
#include <libvisual/libvisual.h> |
| 25 |
#include "gettext.h" |
| 26 |
#include <portaudio.h> |
| 27 |
|
| 28 |
VISUAL_PLUGIN_API_VERSION_VALIDATOR |
| 29 |
|
| 30 |
#define SAMPLE_RATE 44100 |
| 31 |
#define SAMPLE_RATE_TYPE_LV VISUAL_AUDIO_SAMPLE_RATE_44100 |
| 32 |
#define SAMPLE_FORMAT_BITS 16 |
| 33 |
#define SAMPLE_FORMAT_PA paInt16 |
| 34 |
#define SAMPLE_FORMAT_LV VISUAL_AUDIO_SAMPLE_FORMAT_S16 |
| 35 |
|
| 36 |
#define FRAMES 1024 |
| 37 |
|
| 38 |
typedef struct { |
| 39 |
PaStream *stream; |
| 40 |
unsigned int channel_count; // 0 (unknown), 1 (mono) or 2 (stereo) |
| 41 |
void * buffer; |
| 42 |
} PortAudioPrivate; |
| 43 |
|
| 44 |
static int inp_portaudio_init (VisPluginData *plugin); |
| 45 |
static int inp_portaudio_cleanup (VisPluginData *plugin); |
| 46 |
static int inp_portaudio_upload (VisPluginData *plugin, VisAudio *audio); |
| 47 |
|
| 48 |
const VisPluginInfo *get_plugin_info (int *count) |
| 49 |
{ |
| 50 |
static VisInputPlugin input[] = {{ |
| 51 |
.upload = inp_portaudio_upload |
| 52 |
}}; |
| 53 |
|
| 54 |
static VisPluginInfo info[] = {{ |
| 55 |
.type = VISUAL_PLUGIN_TYPE_INPUT, |
| 56 |
|
| 57 |
.plugname = "portaudio", |
| 58 |
.name = "portaudio", |
| 59 |
.author = "Sebastian Pipping <[email protected]>", |
| 60 |
.version = "1.0", |
| 61 |
.about = N_("PortAudio capture plugin"), |
| 62 |
.help = N_("Use this plugin to capture PCM data from the PortAudio record device"), |
| 63 |
.license = VISUAL_PLUGIN_LICENSE_LGPL, |
| 64 |
|
| 65 |
.init = inp_portaudio_init, |
| 66 |
.cleanup = inp_portaudio_cleanup, |
| 67 |
|
| 68 |
.plugin = VISUAL_OBJECT (&input[0]) |
| 69 |
}}; |
| 70 |
|
| 71 |
*count = sizeof (info) / sizeof (*info); |
| 72 |
|
| 73 |
return info; |
| 74 |
} |
| 75 |
|
| 76 |
int inp_portaudio_init (VisPluginData *plugin) |
| 77 |
{ |
| 78 |
#if ENABLE_NLS |
| 79 |
bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); |
| 80 |
#endif |
| 81 |
|
| 82 |
PortAudioPrivate * const priv = visual_mem_new0 (PortAudioPrivate, 1); |
| 83 |
visual_log_return_val_if_fail(priv != NULL, -1); |
| 84 |
visual_object_set_private (VISUAL_OBJECT (plugin), priv); |
| 85 |
|
| 86 |
// Init PortAudio |
| 87 |
const PaError init_error = Pa_Initialize(); |
| 88 |
visual_log_return_val_if_fail (init_error == paNoError, -2); |
| 89 |
|
| 90 |
// Select input device |
| 91 |
const PaDeviceIndex input_device = Pa_GetDefaultInputDevice (); |
| 92 |
visual_log_return_val_if_fail (input_device != paNoDevice, -3); |
| 93 |
|
| 94 |
// Open stream |
| 95 |
// Try stereo first, fall back to mono as needed |
| 96 |
priv->channel_count = 2; // i.e. stereo |
| 97 |
const double latency_seconds = 1.0 / SAMPLE_RATE * FRAMES; |
| 98 |
PaStreamParameters input_parameters = { |
| 99 |
.device = input_device, |
| 100 |
.channelCount = priv->channel_count, |
| 101 |
.sampleFormat = SAMPLE_FORMAT_PA, |
| 102 |
.suggestedLatency = latency_seconds, |
| 103 |
.hostApiSpecificStreamInfo = NULL}; |
| 104 |
const PaError stereo_open_error = |
| 105 |
Pa_OpenStream (&priv->stream, &input_parameters, NULL, |
| 106 |
SAMPLE_RATE, FRAMES, paClipOff, NULL, NULL); |
| 107 |
if (stereo_open_error != paNoError) { |
| 108 |
if (stereo_open_error != paInvalidChannelCount) { |
| 109 |
visual_log (VISUAL_LOG_CRITICAL, |
| 110 |
"PortAudio: Could not open stereo input stream, error %d \"%s\".", |
| 111 |
stereo_open_error, Pa_GetErrorText (stereo_open_error)); |
| 112 |
return -3; |
| 113 |
} |
| 114 |
|
| 115 |
// Try again with mono |
| 116 |
assert(stereo_open_error == paInvalidChannelCount); |
| 117 |
visual_log (VISUAL_LOG_INFO, "PortAudio: Could not open stereo input stream, trying again with mono."); |
| 118 |
priv->channel_count = 1; // i.e. mono |
| 119 |
input_parameters.channelCount = priv->channel_count; |
| 120 |
|
| 121 |
const PaError mono_open_error = |
| 122 |
Pa_OpenStream (&priv->stream, &input_parameters, NULL, |
| 123 |
SAMPLE_RATE, FRAMES, paClipOff, NULL, NULL); |
| 124 |
if (mono_open_error != paNoError) { |
| 125 |
visual_log (VISUAL_LOG_CRITICAL, |
| 126 |
"PortAudio: Could not open mono input stream, error %d \"%s\".", |
| 127 |
mono_open_error, Pa_GetErrorText (mono_open_error)); |
| 128 |
return -3; |
| 129 |
} |
| 130 |
} |
| 131 |
visual_log (VISUAL_LOG_INFO, "PortAudio: Input stream opened with %d channel(s).", priv->channel_count); |
| 132 |
|
| 133 |
// Allocate buffer |
| 134 |
const visual_size_t buffer_size_bytes = FRAMES * priv->channel_count * (SAMPLE_FORMAT_BITS / 8); |
| 135 |
priv->buffer = malloc (buffer_size_bytes); |
| 136 |
visual_log_return_val_if_fail (priv->buffer != NULL, -4); |
| 137 |
|
| 138 |
// Start stream |
| 139 |
const PaError input_start_error = Pa_StartStream (priv->stream); |
| 140 |
visual_log_return_val_if_fail (input_start_error == paNoError, -5); |
| 141 |
|
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
int inp_portaudio_cleanup (VisPluginData *plugin) |
| 146 |
{ |
| 147 |
visual_log_return_val_if_fail (plugin != NULL, -1); |
| 148 |
|
| 149 |
PortAudioPrivate * const priv = visual_object_get_private (VISUAL_OBJECT (plugin)); |
| 150 |
visual_log_return_val_if_fail (priv != NULL, -2); |
| 151 |
|
| 152 |
free (priv->buffer); |
| 153 |
priv->buffer = NULL; |
| 154 |
|
| 155 |
Pa_StopStream (priv->stream); |
| 156 |
Pa_CloseStream (priv->stream); |
| 157 |
priv->stream = NULL; |
| 158 |
|
| 159 |
Pa_Terminate (); |
| 160 |
|
| 161 |
visual_mem_free (priv); |
| 162 |
|
| 163 |
return 0; |
| 164 |
} |
| 165 |
|
| 166 |
int inp_portaudio_upload (VisPluginData *plugin, VisAudio *audio) |
| 167 |
{ |
| 168 |
visual_log_return_val_if_fail (plugin != NULL, -1); |
| 169 |
visual_log_return_val_if_fail (audio != NULL, -2); |
| 170 |
|
| 171 |
PortAudioPrivate * const priv = visual_object_get_private (VISUAL_OBJECT (plugin)); |
| 172 |
visual_log_return_val_if_fail (priv != NULL, -3); |
| 173 |
visual_log_return_val_if_fail (priv->stream != NULL, -4); |
| 174 |
|
| 175 |
for (;;) { |
| 176 |
long frames_to_read = Pa_GetStreamReadAvailable (priv->stream); |
| 177 |
if (frames_to_read < 1) { |
| 178 |
return 0; |
| 179 |
} |
| 180 |
if (frames_to_read > FRAMES) { |
| 181 |
frames_to_read = FRAMES; // because that's all that priv->buffer has space for |
| 182 |
} |
| 183 |
|
| 184 |
const PaError read_error = Pa_ReadStream(priv->stream, priv->buffer, frames_to_read); |
| 185 |
if (read_error != paNoError) { |
| 186 |
return 0; |
| 187 |
} |
| 188 |
|
| 189 |
const visual_size_t bytes_to_write = frames_to_read * priv->channel_count * (SAMPLE_FORMAT_BITS / 8); |
| 190 |
|
| 191 |
VisBuffer buffer; |
| 192 |
visual_buffer_init(&buffer, priv->buffer, bytes_to_write, NULL); |
| 193 |
|
| 194 |
if (priv->channel_count == 2) { |
| 195 |
visual_audio_samplepool_input(audio->samplepool, &buffer, SAMPLE_RATE_TYPE_LV, |
| 196 |
SAMPLE_FORMAT_LV, VISUAL_AUDIO_SAMPLE_CHANNEL_STEREO); |
| 197 |
} else { |
| 198 |
assert(priv->channel_count == 1); |
| 199 |
|
| 200 |
// Let's feed the same mono input to both the left and right channel. |
| 201 |
// This helps actors plugins that call |
| 202 |
// "visual_audio_get_sample ([..], [..], VISUAL_AUDIO_CHANNEL_RIGHT)" |
| 203 |
// explicitly without checking return values. |
| 204 |
const char * const channels[] = { |
| 205 |
VISUAL_AUDIO_CHANNEL_LEFT, |
| 206 |
VISUAL_AUDIO_CHANNEL_RIGHT |
| 207 |
}; |
| 208 |
|
| 209 |
for (size_t i = 0; i < sizeof(channels) / sizeof(channels[0]); i++) { |
| 210 |
visual_audio_samplepool_input_channel(audio->samplepool, &buffer, SAMPLE_RATE_TYPE_LV, |
| 211 |
SAMPLE_FORMAT_LV, channels[i]); |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
// we never get here |
| 217 |
} |