common: Add pre and de emphasis, if the radio does not support it

Connect to the oscillator of the transmitter - good girl!
You need to enable pre emphasis in this case.

Connect to the discriminator of the receiver - good boy!
You need to enable de emphasis in this case.
This commit is contained in:
Andreas Eversberg
2016-04-23 18:50:11 +02:00
parent 5062628e52
commit 4356c93afa
20 changed files with 256 additions and 19 deletions

View File

@@ -18,6 +18,7 @@ libcommon_a_SOURCES = \
../common/besetztton.c \
../common/mncc_sock.c \
../common/cause.c \
../common/emphasis.c \
../common/compander.c \
../common/sender.c \
../common/main_common.c

102
src/common/emphasis.c Normal file
View File

@@ -0,0 +1,102 @@
/* Pre-Emphasis and De-Emphasis implementation
*
* (C) 2016 by Andreas Eversberg <jolly@eversberg.eu>
* All Rights Reserved
*
* 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 <stdio.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include "emphasis.h"
#include "debug.h"
int init_emphasis(emphasis_t *state, int samplerate)
{
double factor;
memset(state, 0, sizeof(*state));
if (samplerate < 24000) {
PDEBUG(DDSP, DEBUG_ERROR, "Sample rate must be at least 24000 Hz!\n");
return -1;
}
factor = 0.95;
state->p.factor = factor;
state->p.amp = samplerate / 48000.0 * 4.0; /* mysterious 48000 */
state->d.factor = factor;
state->d.amp = 1.0 / (samplerate / 48000.0 * 4.0); /* mysterious 48000 */
return 0;
}
void pre_emphasis(emphasis_t *state, int16_t *samples, int num)
{
int32_t sample;
double old_value, new_value, last_value, factor, amp;
int i;
last_value = state->p.last_value;
factor = state->p.factor;
amp = state->p.amp;
for (i = 0; i < num; i++) {
old_value = (double)(*samples) / 32768.0;
new_value = old_value - factor * last_value;
last_value = old_value;
sample = (int)(amp * new_value * 32768.0);
if (sample > 32767)
sample = 32767;
else if (sample < -32768)
sample = -32768;
*samples++ = sample;
}
state->p.last_value = last_value;
}
void de_emphasis(emphasis_t *state, int16_t *samples, int num)
{
int32_t sample;
double old_value, new_value, last_value, factor, amp;
int i;
last_value = state->d.last_value;
factor = state->d.factor;
amp = state->d.amp;
for (i = 0; i < num; i++) {
old_value = (double)(*samples) / 32768.0;
new_value = old_value + factor * last_value;
last_value = new_value;
sample = (int)(amp * new_value * 32768.0);
if (sample > 32767)
sample = 32767;
else if (sample < -32768)
sample = -32768;
*samples++ = sample;
}
state->d.last_value = last_value;
}

17
src/common/emphasis.h Normal file
View File

@@ -0,0 +1,17 @@
typedef struct emphasis {
struct {
double last_value;
double factor;
double amp;
} p;
struct {
double last_value;
double factor;
double amp;
} d;
} emphasis_t;
int init_emphasis(emphasis_t *state, int samplerate);
void pre_emphasis(emphasis_t *state, int16_t *samples, int num);
void de_emphasis(emphasis_t *state, int16_t *samples, int num);

View File

@@ -8,6 +8,8 @@ extern int use_mncc_sock;
extern int send_patterns;
extern int loopback;
extern int rt_prio;
extern int do_pre_emphasis;
extern int do_de_emphasis;
extern const char *read_wave;
extern const char *write_wave;

View File

@@ -36,6 +36,8 @@ int use_mncc_sock = 0;
int send_patterns = 1;
int loopback = 0;
int rt_prio = 0;
int do_pre_emphasis = 0;
int do_de_emphasis = 0;
const char *read_wave = NULL;
const char *write_wave = NULL;
@@ -66,6 +68,12 @@ void print_help_common(const char *arg0, const char *ext_usage)
printf(" Loopback test: 1 = internal | 2 = external | 3 = echo\n");
printf(" -r --realtime <prio>\n");
printf(" Set prio: 0 to diable, 99 for maximum (default = %d)\n", rt_prio);
printf(" -E --pre-emphasis\n");
printf(" Enable pre-emphasis, if you directly connect to the oscillator of the\n");
printf(" transmitter. (No pre-emphasis done by the transmitter.)\n");
printf(" -e --de-emphasis\n");
printf(" Enable de-emphasis, if you directly connect to the discriminator of\n");
printf(" the receiver. (No de-emphasis done by the receiver.)\n");
printf(" -W --write-wave <file>\n");
printf(" Write received audio to given wav audio file.\n");
printf(" -R --read-wave <file>\n");
@@ -84,12 +92,14 @@ static struct option long_options_common[] = {
{"send-patterns", 0, 0, 'p'},
{"loopback", 1, 0, 'L'},
{"realtime", 1, 0, 'r'},
{"pre-emphasis", 0, 0, 'E'},
{"de-emphasis", 0, 0, 'e'},
{"write-wave", 1, 0, 'W'},
{"read-wave", 1, 0, 'R'},
{0, 0, 0, 0}
};
const char *optstring_common = "hD:k:d:s:c:l:mp:L:r:W:R:";
const char *optstring_common = "hD:k:d:s:c:l:mp:L:r:EeW:R:";
struct option *long_options;
char *optstring;
@@ -159,6 +169,14 @@ void opt_switch_common(int c, char *arg0, int *skip_args)
rt_prio = atoi(optarg);
*skip_args += 2;
break;
case 'E':
do_pre_emphasis = 1;
*skip_args += 1;
break;
case 'e':
do_de_emphasis = 1;
*skip_args += 1;
break;
case 'W':
write_wave = strdup(optarg);
*skip_args += 2;

View File

@@ -32,13 +32,15 @@ sender_t *sender_head = NULL;
static sender_t **sender_tailp = &sender_head;
/* Init transceiver instance and link to list of transceivers. */
int sender_create(sender_t *sender, const char *sounddev, int samplerate, const char *write_wave, const char *read_wave, int kanal, int loopback, double loss_volume, int use_pilot_signal)
int sender_create(sender_t *sender, const char *sounddev, int samplerate, int pre_emphasis, int de_emphasis, const char *write_wave, const char *read_wave, int kanal, int loopback, double loss_volume, int use_pilot_signal)
{
int rc = 0;
PDEBUG(DSENDER, DEBUG_DEBUG, "Creating 'Sender' instance\n");
sender->samplerate = samplerate;
sender->pre_emphasis = pre_emphasis;
sender->de_emphasis = de_emphasis;
sender->kanal = kanal;
sender->loopback = loopback;
sender->loss_volume = loss_volume;
@@ -80,6 +82,10 @@ int sender_create(sender_t *sender, const char *sounddev, int samplerate, const
}
}
rc = init_emphasis(&sender->estate, samplerate);
if (rc < 0)
goto error;
*sender_tailp = sender;
sender_tailp = &sender->next;
@@ -151,6 +157,8 @@ void process_sender(sender_t *sender, int latspl)
jitter_load(&sender->audio, samples, count);
else
sender_send(sender, samples, count);
if (sender->pre_emphasis)
pre_emphasis(&sender->estate, samples, count);
switch (sender->use_pilot_signal) {
case 2:
/* tone if pilot signal is on */
@@ -201,6 +209,8 @@ void process_sender(sender_t *sender, int latspl)
return;
}
if (count) {
if (sender->de_emphasis)
de_emphasis(&sender->estate, samples, count);
if (sender->wave_play.fp)
wave_read(&sender->wave_play, samples, count);
if (sender->loopback != 1) {

View File

@@ -3,6 +3,7 @@
#include "samplerate.h"
#include "jitter.h"
#include "loss.h"
#include "emphasis.h"
/* common structure of each transmitter */
typedef struct sender {
@@ -18,6 +19,9 @@ typedef struct sender {
void *sound;
int samplerate;
samplerate_t srstate; /* sample rate conversion state */
int pre_emphasis; /* use pre_emhasis, done by sender */
int de_emphasis; /* use de_emhasis, done by sender */
emphasis_t estate; /* pre and de emphasis */
/* loopback test */
int loopback; /* 0 = off, 1 = internal, 2 = external */
@@ -47,7 +51,7 @@ typedef struct sender {
/* list of all senders */
extern sender_t *sender_head;
int sender_create(sender_t *sender, const char *sounddev, int samplerate, const char *write_wave, const char *read_wave, int kanal, int loopback, double loss_volume, int use_pilot_signal);
int sender_create(sender_t *sender, const char *sounddev, int samplerate, int pre_emphasis, int de_emphasis, const char *write_wave, const char *read_wave, int kanal, int loopback, double loss_volume, int use_pilot_signal);
void sender_destroy(sender_t *sender);
void sender_send(sender_t *sender, int16_t *samples, int count);
void sender_receive(sender_t *sender, int16_t *samples, int count);