Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added stdin/stdout backend #56

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.o
*~
.deps
Makefile
Makefile.in
Expand Down
3 changes: 2 additions & 1 deletion INSTALL
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ without warranty of any kind.
Basic Installation
==================

Briefly, the shell commands `./configure; make; make install' should
Briefly, the shell commands `./install-dependencies-linux.sh;
autoreconf --install; ./configure; make; make install' should
configure, build, and install this package. The following
more-detailed instructions are generic; see the `README' file for
instructions specific to this package. Some packages provide this
Expand Down
12 changes: 12 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ AS_IF([test "x$with_sndfile" = "xno"], [
AC_DEFINE_UNQUOTED([USE_SNDFILE], [$use_sndfile],
[Define to 1 to enable sndfile support])

# stdio
AC_ARG_WITH([stdio], AS_HELP_STRING([--without-stdio],
[build without stdio support]))
AS_IF([test "x$with_stdio" = "xno"], [
use_stdio=0
], [
use_stdio=1
])
AC_DEFINE_UNQUOTED([USE_STDIO], [$use_stdio],
[Define to 1 to enable stdio support])

# benchmarks
AC_ARG_WITH([benchmarks], AS_HELP_STRING([--disable-benchmarks],
[build without internal benchmarks]))
Expand All @@ -81,6 +92,7 @@ option summary:
benchmarks $with_benchmarks ($use_benchmarks)
pulseaudio $with_pulseaudio ($use_pulseaudio)
sndfile $with_sndfile ($use_sndfile)
stdio $with_stdio ($use_stdio)
sndio $with_sndio ($use_sndio)
])

Expand Down
3 changes: 2 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ SIMPLEAUDIO_SRC = \
simpleaudio-alsa.c \
simpleaudio-sndio.c \
simpleaudio-benchmark.c \
simpleaudio-sndfile.c
simpleaudio-sndfile.c \
simpleaudio-stdio.c

FSK_SRC = fsk.h fsk.c

Expand Down
18 changes: 15 additions & 3 deletions src/minimodem.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ usage()
" -5, --baudot Baudot 5-N-1\n"
" -u, --usos {0|1}\n"
" -f, --file {filename.flac}\n"
" -O, --stdio\n"
" -b, --bandwidth {rx_bandwidth}\n"
" -v, --volume {amplitude or 'E'}\n"
" -M, --mark {mark_freq}\n"
Expand Down Expand Up @@ -555,8 +556,8 @@ main( int argc, char*argv[] )
/* validate the default system audio mechanism */
#if !(USE_SNDIO || USE_PULSEAUDIO || USE_ALSA)
# define _MINIMODEM_NO_SYSTEM_AUDIO
# if !USE_SNDFILE
# error At least one of {USE_SNDIO,USE_PULSEAUDIO,USE_ALSA,USE_SNDFILE} must be enabled!
# if !(USE_SNDFILE || USE_STDIO)
# error At least one of {USE_SNDIO,USE_PULSEAUDIO,USE_ALSA,USE_SNDFILE,USE_STDIO} must be enabled!
# endif
#endif

Expand Down Expand Up @@ -607,6 +608,7 @@ main( int argc, char*argv[] )
{ "usos", 1, 0, 'u' },
{ "msb-first", 0, 0, MINIMODEM_OPT_MSBFIRST },
{ "file", 1, 0, 'f' },
{ "stdio", 0, 0, 'O' },
{ "bandwidth", 1, 0, 'b' },
{ "volume", 1, 0, 'v' },
{ "mark", 1, 0, 'M' },
Expand All @@ -631,7 +633,7 @@ main( int argc, char*argv[] )
{ "tx-carrier", 0, 0, MINIMODEM_OPT_TXCARRIER },
{ 0 }
};
c = getopt_long(argc, argv, "Vtrc:l:ai875u:f:b:v:M:S:T:qs::A::R:",
c = getopt_long(argc, argv, "Vtrc:l:ai875u:f:Ob:v:M:S:T:qs::A::R:",
long_options, &option_index);
if ( c == -1 )
break;
Expand Down Expand Up @@ -664,6 +666,16 @@ main( int argc, char*argv[] )
case 'f':
filename = optarg;
break;
case 'O':
#if USE_STDIO
sa_backend = SA_BACKEND_STDIO;
// Apparently the receiver wants floats, so I'mma just output floats
sample_format = SA_SAMPLE_FORMAT_FLOAT;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a --float-samples command line flag. Instead of forcing the value here, couldn't you rely on that flag?

#else
fprintf(stderr, "E: This build of minimodem was configured without stdio support.\n");
exit(1);
#endif
break;
case '8':
bfsk_n_data_bits = 8;
break;
Expand Down
129 changes: 129 additions & 0 deletions src/simpleaudio-stdio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* simpleaudio-stdfile.c
*
* Copyright (C) 2011-2020 Kamal Mostafa <[email protected]>
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#if USE_STDIO

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "simpleaudio.h"
#include "simpleaudio_internal.h"


/*
* stdio backend for simpleaudio
*/

static ssize_t
sa_stdio_read( simpleaudio *sa, void *buf, size_t nframes )
{
int n;
switch ( sa->format ) {
case SA_SAMPLE_FORMAT_FLOAT:
n = fread(buf, 4, nframes, stdin);
break;
case SA_SAMPLE_FORMAT_S16:
n = fread(buf, 2, nframes, stdin);
break;
default:
assert(0);
break;
}
if ( n < 0 ) {
fprintf(stderr, "read error");
return -1;
}

if ( sa->rxnoise != 0.0f ) {
int i;
float *fbuf = buf;
float f = sa->rxnoise * 2;
for ( i=0; i<nframes; i++ )
fbuf[i] += (rand()/RAND_MAX - 0.5f) * f;
}

// fprintf(stderr, "sf_read: nframes=%ld n=%d\n", nframes, n);
return n;
}

static ssize_t
sa_stdio_write( simpleaudio *sa, void *buf, size_t nframes )
{
// fprintf(stderr, "sf_write: nframes=%ld\n", nframes);
int n;
switch ( sa->format ) {
case SA_SAMPLE_FORMAT_FLOAT:
n = fwrite(buf, 4, nframes, stdout);
break;
case SA_SAMPLE_FORMAT_S16:
n = fwrite(buf, 2, nframes, stdout);
break;
default:
assert(0);
break;
}
if ( n < 0 ) {
fprintf(stderr, "write error");
return -1;
}
return n;
}


static void
sa_stdio_close( simpleaudio *sa )
{
// This is probably safe even when reading, right?
fflush(stdout);
}

static int
sa_stdio_open_stream(
simpleaudio *sa,
const char *backend_device,
sa_direction_t sa_stream_direction,
sa_format_t sa_format,
unsigned int rate, unsigned int channels,
char *app_name, char *stream_name )
{
/* good or bad to override these? */
sa->rate = rate;
sa->channels = channels;

sa->backend_handle = NULL;
sa->backend_framesize = sa->channels * sa->samplesize;

return 1;
}


const struct simpleaudio_backend simpleaudio_backend_stdio = {
sa_stdio_open_stream,
sa_stdio_read,
sa_stdio_write,
sa_stdio_close,
};

#endif /* USE_STDIO */
6 changes: 6 additions & 0 deletions src/simpleaudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ simpleaudio_open_stream(
break;
#endif

#if USE_STDIO
case SA_BACKEND_STDIO:
sa->backend = &simpleaudio_backend_stdio;
break;
#endif

#if USE_BENCHMARKS
case SA_BACKEND_BENCHMARK:
sa->backend = &simpleaudio_backend_benchmark;
Expand Down
1 change: 1 addition & 0 deletions src/simpleaudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ typedef struct simpleaudio simpleaudio;
typedef enum {
SA_BACKEND_SYSDEFAULT=0,
SA_BACKEND_FILE,
SA_BACKEND_STDIO,
SA_BACKEND_BENCHMARK,
SA_BACKEND_ALSA,
SA_BACKEND_PULSEAUDIO,
Expand Down
1 change: 1 addition & 0 deletions src/simpleaudio_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ struct simpleaudio_backend {

extern const struct simpleaudio_backend simpleaudio_backend_benchmark;
extern const struct simpleaudio_backend simpleaudio_backend_sndfile;
extern const struct simpleaudio_backend simpleaudio_backend_stdio;
extern const struct simpleaudio_backend simpleaudio_backend_alsa;
extern const struct simpleaudio_backend simpleaudio_backend_pulseaudio;
extern const struct simpleaudio_backend simpleaudio_backend_sndio;
Expand Down