1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
/* gAlan - Graphical Audio Language
* Copyright (C) 1999 Tony Garnock-Jones
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>
#include <fcntl.h>
#include <unistd.h>
#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include "global.h"
#include "generator.h"
#include "comp.h"
#include "control.h"
#include "gencomp.h"
#include "msgbox.h"
#include "prefs.h"
#define DEFAULT_FRAGMENT_EXPONENT 12
#define SIG_OUTPUT 0
typedef signed short OUTPUTSAMPLE;
typedef struct Data {
int audiofd;
gboolean preread;
} Data;
PRIVATE int instance_count = 0;
PRIVATE char dspname[256];
PRIVATE int audio_fragment_exponent = DEFAULT_FRAGMENT_EXPONENT;
PRIVATE gboolean eightbit_in = FALSE;
PRIVATE int open_audiofd(void) {
int i;
int audiofd;
audiofd = open(dspname, O_RDONLY);
if (audiofd == -1) {
perror("open_audiofd/oss_input");
return -1;
}
RETURN_VAL_UNLESS(audiofd != -1, -1);
i = (4 << 16) | audio_fragment_exponent; /* 4 buffers */
RETURN_VAL_UNLESS(ioctl(audiofd, SNDCTL_DSP_SETFRAGMENT, &i) != -1, -1);
i = eightbit_in ? AFMT_S8 : AFMT_S16_NE;
RETURN_VAL_UNLESS(ioctl(audiofd, SNDCTL_DSP_SETFMT, &i) != -1, -1);
i = 0;
RETURN_VAL_UNLESS(ioctl(audiofd, SNDCTL_DSP_STEREO, &i) != -1, -1);
i = SAMPLE_RATE;
RETURN_VAL_UNLESS(ioctl(audiofd, SNDCTL_DSP_SPEED, &i) != -1, -1);
return audiofd;
}
PRIVATE gboolean init_instance(Generator *g) {
Data *data;
instance_count++;
if (instance_count > 1)
/* Not allowed more than one of these things. */
return 0;
data = safe_malloc(sizeof(Data));
data->audiofd = open_audiofd();
data->preread = FALSE;
if (data->audiofd < 0) {
free(data);
popup_msgbox("Error", MSGBOX_OK, 120000, MSGBOX_OK,
"Could not open audio device, %s.", dspname);
return 0;
}
g->data = data;
return TRUE;
}
PRIVATE void destroy_instance(Generator *g) {
Data *data = g->data;
if (data != NULL) {
close(data->audiofd);
free(data);
}
instance_count--;
}
PRIVATE gboolean output_generator(Generator *g, SAMPLE *buf, int buflen) {
Data *data = g->data;
int i;
if( eightbit_in )
{
gint8 samp[MAXIMUM_REALTIME_STEP];
if( ! data->preread ) {
data->preread = TRUE;
return FALSE;
}
if (read(data->audiofd, samp, sizeof(gint8) * buflen) < sizeof(gint8) * buflen) {
printf("."); fflush(stdout);
}
for (i = 0; i < buflen; i++)
buf[i] = samp[i] / 128.0;
}
else
{
gint16 samp[MAXIMUM_REALTIME_STEP];
if( ! data->preread ) {
data->preread = TRUE;
return FALSE;
}
if (read(data->audiofd, samp, sizeof(gint16) * buflen) < sizeof(gint16) * buflen) {
printf("."); fflush(stdout);
}
for (i = 0; i < buflen; i++)
buf[i] = samp[i] / 32768.0;
}
return TRUE;
}
PRIVATE OutputSignalDescriptor output_sigs[] = {
{ "Output", SIG_FLAG_REALTIME, { output_generator, } },
{ NULL, }
};
PRIVATE void setup_class(void) {
GeneratorClass *k;
gboolean prefer;
{
char *value = prefs_get_item("input_plugin");
prefer = value ? !strcmp(value, "OSS") : FALSE;
}
prefs_register_option("input_plugin", "OSS");
{
char *name = prefs_get_item("input_oss_device");
sprintf(dspname, "%s", name ? name : "/dev/dsp");
}
prefs_register_option("input_oss_device", "/dev/dsp");
prefs_register_option("input_oss_device", "/dev/dsp0");
prefs_register_option("input_oss_device", "/dev/dsp1");
{
char *name = prefs_get_item("input_oss_fragment_size");
if (name == NULL || sscanf(name, "%d", &audio_fragment_exponent) != 1) {
audio_fragment_exponent = DEFAULT_FRAGMENT_EXPONENT;
}
audio_fragment_exponent = MAX(audio_fragment_exponent, 7);
}
prefs_register_option("input_oss_fragment_size", "7");
prefs_register_option("input_oss_fragment_size", "8");
prefs_register_option("input_oss_fragment_size", "9");
prefs_register_option("input_oss_fragment_size", "10");
prefs_register_option("input_oss_fragment_size", "11");
prefs_register_option("input_oss_fragment_size", "12");
prefs_register_option("input_oss_fragment_size", "13");
prefs_register_option("input_oss_fragment_size", "14");
prefs_register_option("input_oss_fragment_size", "15");
prefs_register_option("input_oss_fragment_size", "16");
{
char *num_bits = prefs_get_item("input_oss_bits");
if (num_bits == NULL || ( strcmp( num_bits, "16" ) && strcmp( num_bits, "8" ) ) )
eightbit_in = FALSE;
else
eightbit_in = strcmp(num_bits, "8" ) ? FALSE : TRUE;
}
prefs_register_option("input_oss_bits", "16");
prefs_register_option("input_oss_bits", "8");
k = gen_new_generatorclass("audio_in", prefer, 0, 0,
NULL, output_sigs, NULL,
init_instance, destroy_instance,
(AGenerator_pickle_t) init_instance, NULL);
gencomp_register_generatorclass(k, prefer, "Sources/OSS Input",
NULL,
NULL);
}
PUBLIC void init_plugin_oss_input(void) {
setup_class();
}
|