Convert to pulseaudio

This commit is contained in:
Daniel 2021-04-11 08:56:52 -04:00
parent b5a39e5849
commit 2b32f77957
3 changed files with 223 additions and 241 deletions

View file

@ -9,7 +9,7 @@ BIN = ./beepbeep
CC = gcc CC = gcc
LD = $(CC) LD = $(CC)
CFLAGS = -Wall -g CFLAGS = -Wall -g
LIBS = -lm LIBS = -lm -lpulse-simple -lpulse
all: $(BIN) all: $(BIN)
@echo "All done" @echo "All done"

3
README
View file

@ -1,3 +1,6 @@
Originally by Andreas Everberg
Converted to use PulseAudio by Daniel Ponte
The use of this software on telephone lines without the permission of the The use of this software on telephone lines without the permission of the
owners of these lines is illegal. owners of these lines is illegal.

View file

@ -4,6 +4,7 @@
** ** ** **
**---------------------------------------------------------------------------** **---------------------------------------------------------------------------**
** Copyright: Andreas Eversberg ** ** Copyright: Andreas Eversberg **
** Converted to use PulseAudio by Daniel Ponte **
** ** ** **
** minimal blueboxing software ** ** minimal blueboxing software **
** ** ** **
@ -20,10 +21,13 @@
#include <fcntl.h> #include <fcntl.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <errno.h> #include <errno.h>
#include <linux/soundcard.h> #include <stdlib.h>
#include <pulse/simple.h>
#include <pulse/error.h>
#include <pulse/gccmacro.h>
#define SAMPLE_RATE 8000 #define SAMPLE_RATE 8000
#define DSP_DEVICE "/dev/dsp"
#define PI 3.14159265 #define PI 3.14159265
struct ccitt5 { struct ccitt5 {
@ -52,16 +56,24 @@ struct ccitt5 {
{0, 0, 0, 0, 0} {0, 0, 0, 0, 0}
}; };
void close_pa(pa_simple *s)
{
if (s)
pa_simple_free(s);
}
/* /*
* stream bell sound * stream bell sound
*/ */
void dial_dsp(int dsp, char *dial) void dial_pa(pa_simple *s, char *dial)
{ {
int i, j, len; int i, j, len;
int error;
double k1, k2; double k1, k2;
unsigned char buffer[SAMPLE_RATE]; /* one second */ unsigned char buffer[SAMPLE_RATE]; /* one second */
if (dsp < 0) if(!s)
return; return;
while(*dial) { while(*dial) {
@ -78,92 +90,57 @@ void dial_dsp(int dsp, char *dial)
k2 = 2.0 * PI * ccitt5[i].f2 / SAMPLE_RATE; k2 = 2.0 * PI * ccitt5[i].f2 / SAMPLE_RATE;
for (j = 0; j < len; j++) for (j = 0; j < len; j++)
buffer[j] = 64.0 * (2.0 + sin(k1 * j) + sin(k2 * j)); buffer[j] = 64.0 * (2.0 + sin(k1 * j) + sin(k2 * j));
write(dsp, buffer, len); if (pa_simple_write(s, buffer, (size_t) len, &error) < 0) {
fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error));
close_pa(s);
exit(-1);
}
/* send pause */ /* send pause */
len = ccitt5[i].delay * SAMPLE_RATE / 1000; len = ccitt5[i].delay * SAMPLE_RATE / 1000;
memset(buffer, 128, len); memset(buffer, 128, len);
write(dsp, buffer, len); if (pa_simple_write(s, buffer, (size_t) len, &error) < 0) {
fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error));
close_pa(s);
exit(-1);
}
} }
dial++; dial++;
} }
ioctl(dsp, SOUND_PCM_SYNC, 0); if (pa_simple_drain(s, &error) < 0) {
fprintf(stderr, __FILE__": pa_simple_drain() failed: %s\n", pa_strerror(error));
close_pa(s);
exit(-1);
}
} }
/* /*
* open dsp * open pulse
*/ */
int open_dsp(char *device, int rate) pa_simple* open_pa(char *pname)
{ {
int dsp; int error;
unsigned long arg;
// audio_buf_info info;
if ((dsp=open(device, O_WRONLY)) < 0) { static const pa_sample_spec ss = {
fprintf(stderr, "cannot open '%s'\n", device); .format = PA_SAMPLE_U8,
return -errno; .rate = SAMPLE_RATE,
} .channels = 1
#if 0 };
if (ioctl(dsp, SNDCTL_DSP_RESET, NULL) < 0) { pa_simple *s = NULL;
fprintf(stderr, "ioctl failed with SOUND_DSP_RESET\n");
close(dsp); if (!(s = pa_simple_new(NULL, pname, PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &error))) {
return -errno; fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
} return NULL;
#endif
arg = 8;
if (ioctl(dsp, SOUND_PCM_WRITE_BITS, &arg) < 0) {
fprintf(stderr, "ioctl failed with SOUND_PCM_WRITE_BITS = %lu\n", arg);
close(dsp);
return -errno;
}
if ((int)arg != 8) {
fprintf(stderr, "ioctl cannot set correct sample size\n");
close(dsp);
return -errno;
} }
arg = 1; return s;
if (ioctl(dsp, SOUND_PCM_WRITE_CHANNELS, &arg) < 0) {
fprintf(stderr, "ioctl failed with SOUND_PCM_WRITE_CHANNELS = %lu\n", arg);
close(dsp);
return -errno;
}
if ((int)arg != 1) {
fprintf(stderr, "ioctl cannot set correct channel number\n");
close(dsp);
return -errno;
}
arg = rate;
if (ioctl(dsp, SOUND_PCM_WRITE_RATE,&arg) < 0) {
fprintf(stderr, "ioctl failed with SOUND_PCM_WRITE_RATE = %lu\n", arg);
close(dsp);
return -errno;
}
if ((int)arg != rate) {
fprintf(stderr, "ioctl cannot set correct sample rate (got %lu)\n",arg);
close(dsp);
return -errno;
}
return dsp;
}
void close_dsp(int dsp)
{
if (dsp >= 0) {
#if 0
if (ioctl(dsp, SNDCTL_DSP_RESET, NULL) < 0)
fprintf(stderr, "ioctl failed with SOUND_DSP_RESET\n");
#endif
close(dsp);
}
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int dsp; pa_simple *s;
char *dial = ""; char *dial = "";
int ch; int ch;
char digit[2] = "x"; char digit[2] = "x";
@ -179,9 +156,11 @@ int main(int argc, char *argv[])
if (argc > 1) if (argc > 1)
dial = argv[1]; dial = argv[1];
dsp = open_dsp(DSP_DEVICE, SAMPLE_RATE); s = open_pa(argv[0]);
if (dsp < 0) if (s == NULL) {
return dsp; fprintf(stderr, "cannot open pulse\n");
return 1;
}
printf("Welcome to Beep-Beep, your simple bluebox!\n"); printf("Welcome to Beep-Beep, your simple bluebox!\n");
printf("------------------------------------------\n"); printf("------------------------------------------\n");
@ -208,23 +187,23 @@ int main(int argc, char *argv[])
while ((ch = getc(stdin)) != 27) { while ((ch = getc(stdin)) != 27) {
switch(ch) { switch(ch) {
case 'y': case 'z': case 'y': case 'z':
dial_dsp(dsp, "C"); dial_pa(s, "C");
break; break;
case 'x': case 'x':
dial_dsp(dsp, "A"); dial_pa(s, "A");
break; break;
case 'f': case 'f':
dial_dsp(dsp, "B"); dial_pa(s, "B");
break; break;
case '1': case '2': case '3': case '4': case '5': case '1': case '2': case '3': case '4': case '5':
case '6': case '7': case '8': case '9': case '0': case '6': case '7': case '8': case '9': case '0':
case 'a': case 'b': case 'c': case 'd': case 'e': case 'a': case 'b': case 'c': case 'd': case 'e':
digit[0] = ch; digit[0] = ch;
dial_dsp(dsp, digit); dial_pa(s, digit);
break; break;
case '.': case '.':
if (dial[0]) if (dial[0])
dial_dsp(dsp, dial); dial_pa(s, dial);
break; break;
} }
if (ch == 3) if (ch == 3)
@ -232,7 +211,7 @@ int main(int argc, char *argv[])
} }
tcsetattr(0, TCSANOW, &orig); tcsetattr(0, TCSANOW, &orig);
close_dsp(dsp); close_pa(s);
return 0; return 0;
} }