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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
/****************************************************************
Copyright (C) 1986-2000 by
F6FBB - Jean-Paul ROUBELAT
6, rue George Sand
31120 - Roquettes - France
jpr@f6fbb.org
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
Parts of code have been taken from many other softwares.
Thanks for the help.
****************************************************************/
/*
vplay.c - plays and records
CREATIVE LABS VOICE-files, Microsoft WAVE-files and raw data
Autor: Michael Beck - beck@informatik.hu-berlin.de
*/
#include <stdio.h>
#include <malloc.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#ifndef __STDC__
#include <getopt.h>
#endif /* __STDC__ */
#include <fcntl.h>
#include <sys/ioctl.h>
#ifdef __STDC__
#include <sys/soundcard.h>
#else /* __STDC__ */
#include <linux/soundcard.h>
#endif /* __STDC__ */
#include "fmtheaders.h"
/* trying to define independent ioctl for sounddrivers version 1 and 2+ ,
but it dosn't work everywere */
#ifdef SOUND_VERSION
#define IOCTL(a,b,c) ioctl(a,b,&c)
#else
#define IOCTL(a,b,c) (c = ioctl(a,b,c) )
#endif
#define DEFAULT_DSP_SPEED 8000
#define AUDIO "/dev/dsp"
#define min(a,b) ((a) <= (b) ? (a) : (b))
#define VOC_FMT 0
#define WAVE_FMT 1
#define RAW_DATA 2
/* global data */
static int dsp_speed = DEFAULT_DSP_SPEED, dsp_stereo = 0;
static int samplesize = 8;
static int quiet_mode = 0;
static u_long count;
static int audio, abuf_size, zbuf_size;
static u_char *audiobuf, *zerobuf;
/* needed prototypes */
static int r_play (char *name);
/*
test, if it's a .WAV file, 0 if ok (and set the speed, stereo etc.)
< 0 if not
*/
int test_wavefile(void *buffer)
{
WaveHeader *wp = buffer;
if (wp->main_chunk == RIFF && wp->chunk_type == WAVE &&
wp->sub_chunk == FMT && wp->data_chunk == DATA)
{
if (wp->format != PCM_CODE)
{
fprintf (stderr, "can't play not PCM-coded WAVE-files\n");
return (-1);
}
if (wp->modus > 2)
{
fprintf (stderr, "can't play WAVE-files with %d tracks\n", wp->modus);
return (-1);
}
dsp_stereo = (wp->modus == WAVE_STEREO) ? 1 : 0;
samplesize = wp->bit_p_spl;
dsp_speed = wp->sample_fq;
count = wp->data_length;
return 0;
}
return -1;
}
/* if need a SYNC,
(is needed if we plan to change speed, stereo ... during output)
*/
#ifdef __STDC__
int sync_dsp(void)
#else /* __STDC__ */
inline int sync_dsp(void)
#endif /* __STDC__ */
{
if (ioctl (audio, SNDCTL_DSP_SYNC, NULL) < 0)
{
perror(AUDIO);
return (0);
}
return(1);
}
/* setting the speed for output */
int set_dsp_speed (int dsp_speed)
{
if (IOCTL(audio, SNDCTL_DSP_SPEED, dsp_speed) < 0)
{
fprintf (stderr, "unable to set audio speed\n");
perror (AUDIO);
return(0);
}
return(1);
}
/* if to_mono:
compress 8 bit stereo data 2:1, needed if we want play 'one track';
this is usefull, if you habe SB 1.0 - 2.0 (I have 1.0) and want
hear the sample (in Mono)
if to_8:
compress 16 bit to 8 by using hi-byte; wave-files use signed words,
so we need to convert it in "unsigned" sample (0x80 is now zero)
WARNING: this procedure can't compress 16 bit stereo to 16 bit mono,
because if you have a 16 (or 12) bit card you should have
stereo (or I'm wrong ?? )
*/
#ifdef __STDC__
u_long
one_channel(u_char *buf, u_long l, char to_mono, char to_8)
#else /* __STDC__ */
inline u_long one_channel(char *buf, u_long l, char to_mono, char to_8)
#endif /* __STDC__ */
{
register u_char *w = buf;
register u_char *w2 = buf;
char ofs = 0;
u_long incr = 0;
u_long c, ret;
if (to_mono)
++incr;
if (to_8)
{
++incr;
++w2;
ofs = 128;
}
ret = c = l >> incr;
incr = incr << 1;
while (c--)
{
*w++ = *w2 + ofs;
w2 += incr;
}
return ret;
}
int play (char *name)
{
int retour;
audio = open (AUDIO, O_WRONLY, 0);
if (audio == -1)
{
perror (AUDIO);
return (0);
}
IOCTL(audio, SNDCTL_DSP_GETBLKSIZE, abuf_size);
if (abuf_size < 4096 || abuf_size > 65536)
{
if (abuf_size == -1)
perror (AUDIO);
else
fprintf (stderr, "Invalid audio buffers size %d\n", abuf_size);
return(0);
}
zbuf_size = 256;
if ( (audiobuf = (u_char *)malloc (abuf_size)) == NULL ||
(zerobuf = (u_char *)malloc (zbuf_size)) == NULL )
{
fprintf (stderr, "unable to allocate input/output buffer\n");
return(0);
}
memset ((char *)zerobuf, 128, zbuf_size);
retour = r_play (name);
close (audio);
return retour;
}
/* playing/recording raw data, this proc handels WAVE files and
recording .VOCs (as one block) */
int recplay (int fd, int loaded, u_long count, int rtype, char *name)
{
int l, real_l;
u_long c;
char one_chn = 0;
char to_8 = 0;
int tmps;
if (!sync_dsp())
return(0);
if (!quiet_mode)
{
if (samplesize != 8)
fprintf(stderr, "%d bit, ", samplesize);
fprintf (stderr, "Speed %d Hz ", dsp_speed);
fprintf (stderr, "%s ...\n", dsp_stereo ? "Stereo" : "Mono");
}
tmps = samplesize;
IOCTL(audio, SNDCTL_DSP_SAMPLESIZE, tmps);
if (tmps != samplesize)
{
fprintf (stderr, "unable to set %d bit sample size", samplesize);
if (samplesize == 16)
{
samplesize = 8;
IOCTL(audio, SNDCTL_DSP_SAMPLESIZE, samplesize);
if (samplesize != 8)
{
fprintf(stderr, "unable to set 8 bit sample size!\n");
return(0);
}
fprintf (stderr, "; playing 8 bit\n");
to_8 = 1;
}
else
{
fprintf (stderr, "\n");
return(0);
}
}
#ifdef SOUND_VERSION
if (ioctl (audio, SNDCTL_DSP_STEREO, &dsp_stereo) < 0)
{
#else
if (dsp_stereo != ioctl (audio, SNDCTL_DSP_STEREO, dsp_stereo) )
{
#endif
fprintf (stderr, "can't play in Stereo; playing only one channel\n");
dsp_stereo = MODE_MONO;
one_chn = 1;
}
if (!set_dsp_speed (dsp_speed))
return(0);
while (count)
{
c = count;
if (c > abuf_size)
c = abuf_size;
if ((l = read (fd, (char *)audiobuf + loaded, c - loaded)) > 0)
{
l += loaded; loaded = 0; /* correct the count; ugly but ... */
real_l = (one_chn || to_8) ? one_channel(audiobuf, l, one_chn, to_8) : l;
if (write (audio, (char *)audiobuf, real_l) != real_l)
{
perror (AUDIO);
return(0);
}
count -= l;
}
else
{
if (l == -1)
{
perror (name);
return(0);
}
count = 0; /* Stop */
}
} /* while (count) */
return(1);
}
/*
let's play or record it (record_type says VOC/WAVE/raw)
*/
int r_play(char *name)
{
int fd;
int retour = 0;
fprintf(stderr, "Playing %s : ", name);
if ((fd = open (name, O_RDONLY, 0)) == -1)
{
perror (name);
return(0);
}
/* read bytes for WAVE-header */
read (fd, (char *)audiobuf, sizeof(WaveHeader));
if (test_wavefile (audiobuf) >= 0)
{
retour = recplay (fd, 0, count, WAVE_FMT, name);
}
if (fd != 0)
close(fd);
return(retour);
}
|