libsample: Add another function for converting between int16_t and sample_t

Now we have speech level and 1mW (0 dBm) conversion functions
master
Andreas Eversberg 4 months ago
parent 486d6b3780
commit a756ba8fd9
  1. 2
      src/eurosignal/eurosignal.c
  2. 4
      src/libmobile/call.c
  3. 4
      src/libmobile/console.c
  4. 29
      src/libsample/sample.c
  5. 6
      src/libsample/sample.h
  6. 2
      src/zeitansage/zeitansage.c

@ -506,7 +506,7 @@ static void call_play_announcement(euro_call_t *call)
else
chunk[i] = 0.0;
}
int16_to_samples(spl, chunk, 160);
int16_to_samples_speech(spl, chunk, 160);
call_up_audio(call->callref, spl, 160);
}

@ -395,7 +395,7 @@ void down_audio(struct osmo_cc_session_codec *codec, uint16_t sequence_number, u
/* if we are disconnected, ignore audio */
if (!process || process->pattern != PATTERN_NONE)
return;
int16_to_samples(samples, (int16_t *)data, len / 2);
int16_to_samples_speech(samples, (int16_t *)data, len / 2);
#ifdef DEBUG_LEVEL
double lev = level_of(samples, len / 2);
printf("festnetz-level: %s %.4f\n", debug_db(lev), (20 * log10(lev)));
@ -621,7 +621,7 @@ void call_up_audio(int callref, sample_t *samples, int count)
double lev = level_of(samples, count);
printf(" mobil-level: %s%.4f\n", debug_db(lev), (20 * log10(lev)));
#endif
samples_to_int16(data, samples, count);
samples_to_int16_speech(data, samples, count);
osmo_cc_rtp_send(process->codec, (uint8_t *)data, count * 2, 1, count);
/* don't destroy process here in case of an error */
}

@ -148,7 +148,7 @@ void up_audio(struct osmo_cc_session_codec *codec, uint16_t sequence_number, uin
/* save audio from transceiver to jitter buffer */
if (console.sound) {
int16_to_samples(samples, (int16_t *)data, count);
int16_to_samples_speech(samples, (int16_t *)data, count);
jitter_save(&console.dejitter, samples, count, 1, sequence_number, timestamp, ssrc);
return;
}
@ -627,7 +627,7 @@ void process_console(int c)
/* only if we have a call */
if (console.callref && console.codec) {
int16_t data[160];
samples_to_int16(data, console.tx_buffer, 160);
samples_to_int16_speech(data, console.tx_buffer, 160);
osmo_cc_rtp_send(console.codec, (uint8_t *)data, 160 * 2, 1, 160);
}
}

@ -25,6 +25,7 @@
* of 16 bits signed value:
*/
static double int_16_speech_level = SPEECH_LEVEL * 0.7079; /* 16 dBm below dBm0, which is about 3dBm below full 16 bit range */
static double int_16_1mw_level = 0.7079; /* dBm0, 3dBm below full 16 bit range */
/* A sample_t is a value that has virtually infinite precision but will also
* support high numbers. 'double' or 'float' types are sufficient.
@ -40,7 +41,8 @@ static double int_16_speech_level = SPEECH_LEVEL * 0.7079; /* 16 dBm below dBm0,
* envelope is network dependent.
*/
void samples_to_int16(int16_t *spl, sample_t *samples, int length)
/* sample conversion relative to SPEECH level */
void samples_to_int16_speech(int16_t *spl, sample_t *samples, int length)
{
int32_t value;
@ -55,10 +57,33 @@ void samples_to_int16(int16_t *spl, sample_t *samples, int length)
}
}
void int16_to_samples(sample_t *samples, int16_t *spl, int length)
void int16_to_samples_speech(sample_t *samples, int16_t *spl, int length)
{
while (length--) {
*samples++ = (double)(*spl++) / 32767.0 / int_16_speech_level;
}
}
/* sample conversion relative to 1mW level */
void samples_to_int16_1mw(int16_t *spl, sample_t *samples, int length)
{
int32_t value;
while (length--) {
value = *samples++ * int_16_1mw_level * 32768.0;
if (value > 32767.0)
*spl++ = 32767;
else if (value < -32767.0)
*spl++ = -32767;
else
*spl++ = (uint16_t)value;
}
}
void int16_to_samples_1mw(sample_t *samples, int16_t *spl, int length)
{
while (length--) {
*samples++ = (double)(*spl++) / 32767.0 / int_16_1mw_level;
}
}

@ -3,6 +3,8 @@ typedef double sample_t;
#define SPEECH_LEVEL 0.1585
void samples_to_int16(int16_t *spl, sample_t *samples, int length);
void int16_to_samples(sample_t *samples, int16_t *spl, int length);
void samples_to_int16_speech(int16_t *spl, sample_t *samples, int length);
void int16_to_samples_speech(sample_t *samples, int16_t *spl, int length);
void samples_to_int16_1mw(int16_t *spl, sample_t *samples, int length);
void int16_to_samples_1mw(sample_t *samples, int16_t *spl, int length);

@ -296,7 +296,7 @@ next_sample:
call->spl_time = spl_time;
/* convert to samples, apply gain and send toward fixed network */
int16_to_samples(spl, chunk, 160);
int16_to_samples_speech(spl, chunk, 160);
for (i = 0; i < 160; i++)
spl[i] *= audio_gain;
call_up_audio(call->callref, spl, 160);

Loading…
Cancel
Save