mirror of
https://github.com/radio95-rnt/rds95.git
synced 2026-07-29 16:29:17 +02:00
simplify main
This commit is contained in:
+12
-62
@@ -1,21 +1,3 @@
|
|||||||
/*
|
|
||||||
* mpxgen - FM multiplex encoder with Stereo and RDS
|
|
||||||
* Copyright (C) 2019 Anthony96922
|
|
||||||
*
|
|
||||||
* 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/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
@@ -37,7 +19,6 @@ static void stop() {
|
|||||||
stop_rds = 1;
|
stop_rds = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* threads */
|
/* threads */
|
||||||
static void *control_pipe_worker() {
|
static void *control_pipe_worker() {
|
||||||
while (!stop_rds) {
|
while (!stop_rds) {
|
||||||
@@ -51,8 +32,7 @@ static void *control_pipe_worker() {
|
|||||||
|
|
||||||
static void show_help(char *name) {
|
static void show_help(char *name) {
|
||||||
printf(
|
printf(
|
||||||
"This is MiniRDS, a lightweight RDS encoder.\n"
|
"rds95 (a RDS encoder by radio95) version %.1f\n"
|
||||||
"Version %f\n"
|
|
||||||
"\n"
|
"\n"
|
||||||
"Usage: %s [options]\n"
|
"Usage: %s [options]\n"
|
||||||
"\n"
|
"\n"
|
||||||
@@ -74,20 +54,14 @@ static void show_help(char *name) {
|
|||||||
" -d,--di DI code\n"
|
" -d,--di DI code\n"
|
||||||
" -C,--ctl FIFO control pipe\n"
|
" -C,--ctl FIFO control pipe\n"
|
||||||
" -h,--help Show this help text and exit\n"
|
" -h,--help Show this help text and exit\n"
|
||||||
" -v,--version Show version and exit\n"
|
|
||||||
"\n",
|
"\n",
|
||||||
VERSION,
|
VERSION,
|
||||||
name
|
name
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void show_version() {
|
|
||||||
printf("MiniRDS version (radio95 Edit) %f\n", VERSION);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
int opt;
|
char control_pipe[51] = "\0";
|
||||||
char control_pipe[51];
|
|
||||||
struct rds_params_t rds_params = {
|
struct rds_params_t rds_params = {
|
||||||
.ps = "radio95",
|
.ps = "radio95",
|
||||||
.rt1 = "",
|
.rt1 = "",
|
||||||
@@ -95,10 +69,6 @@ int main(int argc, char **argv) {
|
|||||||
.ecc = 0xE2,
|
.ecc = 0xE2,
|
||||||
.lps = "radio95 - Radio Nowotomyskie"
|
.lps = "radio95 - Radio Nowotomyskie"
|
||||||
};
|
};
|
||||||
/* buffers */
|
|
||||||
float *mpx_buffer;
|
|
||||||
int8_t r;
|
|
||||||
|
|
||||||
/* PASIMPLE */
|
/* PASIMPLE */
|
||||||
pa_simple *device;
|
pa_simple *device;
|
||||||
pa_sample_spec format;
|
pa_sample_spec format;
|
||||||
@@ -106,12 +76,9 @@ int main(int argc, char **argv) {
|
|||||||
/* pthread */
|
/* pthread */
|
||||||
pthread_attr_t attr;
|
pthread_attr_t attr;
|
||||||
pthread_t control_pipe_thread;
|
pthread_t control_pipe_thread;
|
||||||
pthread_mutex_t control_pipe_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
||||||
pthread_cond_t control_pipe_cond;
|
pthread_cond_t control_pipe_cond;
|
||||||
|
|
||||||
|
const char *short_opt = "R:i:s:r:p:T:A:P:l:e:L:d:C:h";
|
||||||
const char *short_opt = "R:i:s:r:p:T:A:P:l:e:L:d:C:"
|
|
||||||
"hv";
|
|
||||||
|
|
||||||
struct option long_opt[] =
|
struct option long_opt[] =
|
||||||
{
|
{
|
||||||
@@ -130,17 +97,11 @@ int main(int argc, char **argv) {
|
|||||||
{"ctl", required_argument, NULL, 'C'},
|
{"ctl", required_argument, NULL, 'C'},
|
||||||
|
|
||||||
{"help", no_argument, NULL, 'h'},
|
{"help", no_argument, NULL, 'h'},
|
||||||
{"version", no_argument, NULL, 'v'},
|
|
||||||
{ 0, 0, 0, 0 }
|
{ 0, 0, 0, 0 }
|
||||||
};
|
};
|
||||||
|
|
||||||
memset(control_pipe, 0, 51);
|
int opt;
|
||||||
|
while((opt = getopt_long(argc, argv, short_opt, long_opt, NULL)) != -1) {
|
||||||
keep_parsing_opts:
|
|
||||||
|
|
||||||
opt = getopt_long(argc, argv, short_opt, long_opt, NULL);
|
|
||||||
if (opt == -1) goto done_parsing_opts;
|
|
||||||
|
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'i': /* pi */
|
case 'i': /* pi */
|
||||||
rds_params.pi = strtoul(optarg, NULL, 16);
|
rds_params.pi = strtoul(optarg, NULL, 16);
|
||||||
@@ -186,29 +147,17 @@ keep_parsing_opts:
|
|||||||
memcpy(control_pipe, optarg, 50);
|
memcpy(control_pipe, optarg, 50);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'v': /* version */
|
|
||||||
show_version();
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case 'h': /* help */
|
case 'h': /* help */
|
||||||
case '?':
|
|
||||||
default:
|
default:
|
||||||
show_help(argv[0]);
|
show_help(argv[0]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
goto keep_parsing_opts;
|
|
||||||
|
|
||||||
done_parsing_opts:
|
|
||||||
|
|
||||||
/* Initialize pthread stuff */
|
/* Initialize pthread stuff */
|
||||||
pthread_mutex_init(&control_pipe_mutex, NULL);
|
|
||||||
pthread_cond_init(&control_pipe_cond, NULL);
|
pthread_cond_init(&control_pipe_cond, NULL);
|
||||||
pthread_attr_init(&attr);
|
pthread_attr_init(&attr);
|
||||||
|
|
||||||
/* Setup buffers */
|
|
||||||
mpx_buffer = malloc(NUM_MPX_FRAMES * 2 * sizeof(float));
|
|
||||||
|
|
||||||
/* Gracefully stop the encoder on SIGINT or SIGTERM */
|
/* Gracefully stop the encoder on SIGINT or SIGTERM */
|
||||||
signal(SIGINT, stop);
|
signal(SIGINT, stop);
|
||||||
signal(SIGTERM, stop);
|
signal(SIGTERM, stop);
|
||||||
@@ -223,7 +172,7 @@ done_parsing_opts:
|
|||||||
|
|
||||||
device = pa_simple_new(
|
device = pa_simple_new(
|
||||||
NULL, // Default PulseAudio server
|
NULL, // Default PulseAudio server
|
||||||
"MiniRDS", // Application name
|
"rds95", // Application name
|
||||||
PA_STREAM_PLAYBACK, // Direction (playback)
|
PA_STREAM_PLAYBACK, // Direction (playback)
|
||||||
"RDS", // Default device
|
"RDS", // Default device
|
||||||
"RDS Generator", // Stream description
|
"RDS Generator", // Stream description
|
||||||
@@ -242,6 +191,7 @@ done_parsing_opts:
|
|||||||
if (open_control_pipe(control_pipe) == 0) {
|
if (open_control_pipe(control_pipe) == 0) {
|
||||||
fprintf(stderr, "Reading control commands on %s.\n", control_pipe);
|
fprintf(stderr, "Reading control commands on %s.\n", control_pipe);
|
||||||
/* Create control pipe polling worker */
|
/* Create control pipe polling worker */
|
||||||
|
uint8_t r;
|
||||||
r = pthread_create(&control_pipe_thread, &attr, control_pipe_worker, NULL);
|
r = pthread_create(&control_pipe_thread, &attr, control_pipe_worker, NULL);
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
fprintf(stderr, "Could not create control pipe thread.\n");
|
fprintf(stderr, "Could not create control pipe thread.\n");
|
||||||
@@ -258,13 +208,15 @@ done_parsing_opts:
|
|||||||
|
|
||||||
int pulse_error;
|
int pulse_error;
|
||||||
|
|
||||||
|
float mpx_buffer[NUM_MPX_FRAMES];
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
for (size_t i = 0; i < NUM_MPX_FRAMES; i++) {
|
for (size_t i = 0; i < NUM_MPX_FRAMES; i++) {
|
||||||
mpx_buffer[i] = fminf(1.0f, fmaxf(-1.0f, get_rds_sample()));
|
mpx_buffer[i] = fminf(1.0f, fmaxf(-1.0f, get_rds_sample()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* num_bytes = audio frames( * channels) * bytes per sample */
|
/* num_bytes = audio frames( * channels) * bytes per sample */
|
||||||
if (pa_simple_write(device, mpx_buffer, NUM_MPX_FRAMES * sizeof(float), &pulse_error) != 0) {
|
if (pa_simple_write(device, mpx_buffer, sizeof(mpx_buffer), &pulse_error) != 0) {
|
||||||
fprintf(stderr, "Error: could not play audio. (%s : %d)\n", pa_strerror(pulse_error), pulse_error);
|
fprintf(stderr, "Error: could not play audio. (%s : %d)\n", pa_strerror(pulse_error), pulse_error);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -284,10 +236,8 @@ exit:
|
|||||||
}
|
}
|
||||||
|
|
||||||
pthread_attr_destroy(&attr);
|
pthread_attr_destroy(&attr);
|
||||||
pa_simple_free(device);
|
|
||||||
exit_rds_encoder();
|
exit_rds_encoder();
|
||||||
|
pa_simple_free(device);
|
||||||
free(mpx_buffer);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user