| 1 |
/* Libvisual-plugins - Standard plugins for libvisual |
| 2 |
* |
| 3 |
* Copyright (C) 2004, 2005, 2006 Dennis Smit <[email protected]> |
| 4 |
* |
| 5 |
* Authors: Dennis Smit <[email protected]> |
| 6 |
* |
| 7 |
* $Id: input_jack.c,v 1.14 2006/01/22 13:25:27 synap Exp $ |
| 8 |
* |
| 9 |
* This program is free software; you can redistribute it and/or modify |
| 10 |
* it under the terms of the GNU Lesser General Public License as |
| 11 |
* published by the Free Software Foundation; either version 2.1 |
| 12 |
* of the License, or (at your option) any later version. |
| 13 |
* |
| 14 |
* This program is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
* GNU General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU Lesser General Public License |
| 20 |
* along with this program; if not, write to the Free Software |
| 21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 |
*/ |
| 23 |
|
| 24 |
#include <config.h> |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <unistd.h> |
| 29 |
#include <fcntl.h> |
| 30 |
#include <string.h> |
| 31 |
#include <gettext.h> |
| 32 |
|
| 33 |
#include <jack/jack.h> |
| 34 |
|
| 35 |
#include <libvisual/libvisual.h> |
| 36 |
|
| 37 |
#define PCM_BUF_SIZE 1024 |
| 38 |
|
| 39 |
typedef struct { |
| 40 |
jack_client_t *client; |
| 41 |
jack_port_t *input_port; |
| 42 |
|
| 43 |
int shutdown; |
| 44 |
|
| 45 |
VisBuffer *buffer; |
| 46 |
|
| 47 |
short fakebuf[PCM_BUF_SIZE]; |
| 48 |
} JackPrivate; |
| 49 |
|
| 50 |
static int process_callback (jack_nframes_t nframes, void *arg); |
| 51 |
static void shutdown_callback (void *arg); |
| 52 |
|
| 53 |
int inp_jack_init (VisPluginData *plugin); |
| 54 |
int inp_jack_cleanup (VisPluginData *plugin); |
| 55 |
int inp_jack_upload (VisPluginData *plugin, VisAudio *audio); |
| 56 |
|
| 57 |
VISUAL_PLUGIN_API_VERSION_VALIDATOR |
| 58 |
|
| 59 |
const VisPluginInfo *get_plugin_info (int *count) |
| 60 |
{ |
| 61 |
static VisInputPlugin input[] = {{ |
| 62 |
.upload = inp_jack_upload |
| 63 |
}}; |
| 64 |
|
| 65 |
static VisPluginInfo info[] = {{ |
| 66 |
.type = VISUAL_PLUGIN_TYPE_INPUT, |
| 67 |
|
| 68 |
.plugname = "jack", |
| 69 |
.name = "jack", |
| 70 |
.author = "Dennis Smit <[email protected]>", |
| 71 |
.version = "0.1", |
| 72 |
.about = N_("Jackit capture plugin"), |
| 73 |
.help = N_("Use this plugin to capture PCM data from the jackd daemon"), |
| 74 |
.license = VISUAL_PLUGIN_LICENSE_LGPL, |
| 75 |
|
| 76 |
.init = inp_jack_init, |
| 77 |
.cleanup = inp_jack_cleanup, |
| 78 |
|
| 79 |
.plugin = VISUAL_OBJECT (&input[0]) |
| 80 |
}}; |
| 81 |
|
| 82 |
*count = sizeof (info) / sizeof (*info); |
| 83 |
|
| 84 |
return info; |
| 85 |
} |
| 86 |
|
| 87 |
int inp_jack_init (VisPluginData *plugin) |
| 88 |
{ |
| 89 |
JackPrivate *priv; |
| 90 |
const char **ports; |
| 91 |
|
| 92 |
#if ENABLE_NLS |
| 93 |
bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); |
| 94 |
#endif |
| 95 |
|
| 96 |
visual_log_return_val_if_fail (plugin != NULL, -1); |
| 97 |
|
| 98 |
priv = visual_mem_new0 (JackPrivate, 1); |
| 99 |
visual_log_return_val_if_fail (priv != NULL, -1); |
| 100 |
visual_object_set_private (VISUAL_OBJECT (plugin), priv); |
| 101 |
|
| 102 |
if ((priv->client = jack_client_new (_("Libvisual jackit capture"))) == NULL) { |
| 103 |
visual_log (VISUAL_LOG_CRITICAL, _("jack server probably not running")); |
| 104 |
return -1; |
| 105 |
} |
| 106 |
|
| 107 |
jack_set_process_callback (priv->client, process_callback, priv); |
| 108 |
jack_on_shutdown (priv->client, shutdown_callback, priv); |
| 109 |
|
| 110 |
priv->input_port = jack_port_register (priv->client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0); |
| 111 |
|
| 112 |
if (jack_activate (priv->client) == 1) { |
| 113 |
visual_log (VISUAL_LOG_CRITICAL, _("Cannot activate the jack client")); |
| 114 |
|
| 115 |
return -1; |
| 116 |
} |
| 117 |
|
| 118 |
if ((ports = jack_get_ports (priv->client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput)) == NULL) { |
| 119 |
visual_log (VISUAL_LOG_CRITICAL, _("Cannot find any physical capture ports")); |
| 120 |
|
| 121 |
return -1; |
| 122 |
} |
| 123 |
|
| 124 |
if (jack_connect (priv->client, ports[0], jack_port_name (priv->input_port))) { |
| 125 |
visual_log (VISUAL_LOG_CRITICAL, _("Cannot connect input ports")); |
| 126 |
|
| 127 |
visual_mem_free (ports); |
| 128 |
|
| 129 |
return -1; |
| 130 |
} |
| 131 |
|
| 132 |
visual_mem_free (ports); |
| 133 |
|
| 134 |
// visual_buffer_init_allocate (&priv->buffer, 65536, |
| 135 |
|
| 136 |
return 0; |
| 137 |
} |
| 138 |
|
| 139 |
int inp_jack_cleanup (VisPluginData *plugin) |
| 140 |
{ |
| 141 |
JackPrivate *priv = visual_object_get_private (VISUAL_OBJECT (plugin)); |
| 142 |
|
| 143 |
visual_log_return_val_if_fail( plugin != NULL, -1 ); |
| 144 |
priv = visual_object_get_private (VISUAL_OBJECT (plugin)); |
| 145 |
visual_log_return_val_if_fail( priv != NULL, -1 ); |
| 146 |
|
| 147 |
if (priv->client != NULL) |
| 148 |
jack_client_close (priv->client); |
| 149 |
|
| 150 |
visual_mem_free (priv); |
| 151 |
|
| 152 |
return 0; |
| 153 |
} |
| 154 |
|
| 155 |
int inp_jack_upload (VisPluginData *plugin, VisAudio *audio) |
| 156 |
{ |
| 157 |
JackPrivate *priv = NULL; |
| 158 |
VisBuffer buffer; |
| 159 |
int i; |
| 160 |
|
| 161 |
visual_log_return_val_if_fail(audio != NULL, -1); |
| 162 |
visual_log_return_val_if_fail(plugin != NULL, -1); |
| 163 |
|
| 164 |
priv = visual_object_get_private (VISUAL_OBJECT (plugin)); |
| 165 |
|
| 166 |
visual_log_return_val_if_fail(priv != NULL, -1); |
| 167 |
|
| 168 |
if (priv->shutdown == TRUE) { |
| 169 |
visual_log (VISUAL_LOG_CRITICAL, _("The jack server seems to have shutdown")); |
| 170 |
|
| 171 |
return -1; |
| 172 |
} |
| 173 |
|
| 174 |
/* |
| 175 |
visual_buffer_init (&buffer, data, rcnt, NULL); |
| 176 |
|
| 177 |
visual_audio_samplepool_input (audio->samplepool, &buffer, VISUAL_AUDIO_SAMPLE_RATE_44100, |
| 178 |
VISUAL_AUDIO_SAMPLE_FORMAT_FLOAT, VISUAL_AUDIO_SAMPLE_CHANNEL_STEREO); |
| 179 |
*/ |
| 180 |
|
| 181 |
for (i = 0; i < PCM_BUF_SIZE && i < 1024; i += 2) { |
| 182 |
audio->plugpcm[0][i >> 1] = priv->fakebuf[i]; |
| 183 |
audio->plugpcm[1][i >> 1] = priv->fakebuf[i + 1]; |
| 184 |
} |
| 185 |
|
| 186 |
return 0; |
| 187 |
} |
| 188 |
|
| 189 |
static int process_callback (jack_nframes_t nframes, void *arg) |
| 190 |
{ |
| 191 |
JackPrivate *priv = arg; |
| 192 |
jack_default_audio_sample_t *in; |
| 193 |
int i; |
| 194 |
|
| 195 |
in = (jack_default_audio_sample_t *) jack_port_get_buffer (priv->input_port, nframes); |
| 196 |
|
| 197 |
visual_mem_set (&priv->fakebuf, 0, sizeof (priv->fakebuf)); |
| 198 |
|
| 199 |
for (i = 0; i < nframes && i < 1024; i++) |
| 200 |
priv->fakebuf[i] = in[i] * 32767; |
| 201 |
|
| 202 |
return 0; |
| 203 |
} |
| 204 |
|
| 205 |
static void shutdown_callback (void *arg) |
| 206 |
{ |
| 207 |
JackPrivate *priv = arg; |
| 208 |
|
| 209 |
priv->shutdown = TRUE; |
| 210 |
} |
| 211 |
|