1
0
mirror of https://github.com/JvanKatwijk/qt-dab.git synced 2025-10-06 08:12:40 +02:00
Files
SDR-DAB_Qt-DAB/src/backend/audio-backend.cpp

198 lines
5.8 KiB
C++
Raw Normal View History

2017-02-06 14:09:08 +01:00
#
/*
2017-11-30 18:52:51 +01:00
* Copyright (C) 2014 .. 2017
2017-02-06 14:09:08 +01:00
* Jan van Katwijk (J.vanKatwijk@gmail.com)
2017-05-31 17:46:42 +02:00
* Lazy Chair Computing
2017-02-06 14:09:08 +01:00
*
2017-11-30 18:52:51 +01:00
* This file is part of Qt-DAB
* Qt-DAB is free software; you can redistribute it and/or modify
2017-02-06 14:09:08 +01:00
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Qt-DAB is distributed in the hope that it will be useful,
2017-02-06 14:09:08 +01:00
* 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 Qt-DAB; if not, write to the Free Software
2017-02-06 14:09:08 +01:00
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#
#include "dab-constants.h"
#include "audio-backend.h"
2017-02-06 14:09:08 +01:00
#include "mp2processor.h"
#include "mp4processor.h"
#include "eep-protection.h"
#include "uep-protection.h"
#include "radio.h"
//
// Interleaving is - for reasons of simplicity - done
// inline rather than through a special class-object
//
// fragmentsize == Length * CUSize
audioBackend::audioBackend (RadioInterface *mr,
audiodata *d,
RingBuffer<int16_t> *buffer,
QString picturesPath):
virtualBackend (d -> startAddr,
d -> length),
outV (d -> bitRate * 24)
#ifdef __THREADED_BACKEND
2018-01-03 18:46:52 +01:00
,freeSlots (20)
2017-10-01 19:07:22 +02:00
#endif
{
2017-02-06 14:09:08 +01:00
int32_t i, j;
this -> myRadioInterface = mr;
this -> dabModus = d -> ASCTy == 077 ? DAB_PLUS : DAB;
this -> fragmentSize = d -> length * CUSize;
this -> bitRate = d -> bitRate;
this -> shortForm = d -> shortForm;
this -> protLevel = d -> protLevel;
2017-02-06 14:09:08 +01:00
this -> audioBuffer = buffer;
interleaveData = new int16_t *[16]; // max size
for (i = 0; i < 16; i ++) {
interleaveData [i] = new int16_t [fragmentSize];
memset (interleaveData [i], 0, fragmentSize * sizeof (int16_t));
}
2018-02-09 20:21:44 +01:00
2017-10-01 19:07:22 +02:00
countforInterleaver = 0;
interleaverIndex = 0;
2017-02-06 14:09:08 +01:00
2017-02-08 19:30:09 +01:00
if (shortForm)
2017-02-06 14:09:08 +01:00
protectionHandler = new uep_protection (bitRate,
protLevel);
else
protectionHandler = new eep_protection (bitRate,
protLevel);
//
if (dabModus == DAB)
our_dabProcessor = new mp2Processor (myRadioInterface,
bitRate,
2017-05-31 17:46:42 +02:00
audioBuffer,
picturesPath);
2017-02-06 14:09:08 +01:00
else
if (dabModus == DAB_PLUS)
our_dabProcessor = new mp4Processor (myRadioInterface,
bitRate,
2017-05-31 17:46:42 +02:00
audioBuffer,
picturesPath);
2017-02-06 14:09:08 +01:00
else // cannot happen
2017-10-01 19:07:22 +02:00
our_dabProcessor = new frameProcessor ();
2017-02-06 14:09:08 +01:00
2017-09-16 10:48:26 +02:00
fprintf (stderr, "we now have %s\n", dabModus == DAB_PLUS ? "DAB+" : "DAB");
2018-01-17 16:25:29 +01:00
tempX. resize (fragmentSize);
2018-02-09 20:21:44 +01:00
uint8_t shiftRegister [9];
disperseVector. resize (24 * bitRate);
memset (shiftRegister, 1, 9);
for (i = 0; i < bitRate * 24; i ++) {
uint8_t b = shiftRegister [8] ^ shiftRegister [4];
for (j = 8; j > 0; j--)
shiftRegister [j] = shiftRegister [j - 1];
shiftRegister [0] = b;
disperseVector [i] = b;
}
#ifdef __THREADED_BACKEND
2017-10-01 19:07:22 +02:00
// for local buffering the input, we have
nextIn = 0;
nextOut = 0;
for (i = 0; i < 20; i ++)
theData [i] = new int16_t [fragmentSize];
running. store (true);
2017-02-06 14:09:08 +01:00
start ();
2017-10-01 19:07:22 +02:00
#endif
2017-02-06 14:09:08 +01:00
}
audioBackend::~audioBackend (void) {
2017-02-06 14:09:08 +01:00
int16_t i;
#ifdef __THREADED_BACKEND
2017-10-01 19:07:22 +02:00
running. store (false);
2017-02-06 14:09:08 +01:00
while (this -> isRunning ())
usleep (1000);
2017-10-01 19:07:22 +02:00
#endif
2017-02-06 14:09:08 +01:00
delete protectionHandler;
delete our_dabProcessor;
for (i = 0; i < 16; i ++)
delete[] interleaveData [i];
2017-10-01 19:07:22 +02:00
delete [] interleaveData;
#ifdef __THREADED_BACKEND
2017-07-12 12:02:20 +02:00
for (i = 0; i < 20; i ++)
delete [] theData [i];
2017-10-01 19:07:22 +02:00
#endif
2017-02-06 14:09:08 +01:00
}
int32_t audioBackend::process (int16_t *v, int16_t cnt) {
2017-02-06 14:09:08 +01:00
#ifdef __THREADED_BACKEND
2017-07-12 12:02:20 +02:00
while (!freeSlots. tryAcquire (1, 200))
2017-02-06 14:09:08 +01:00
if (!running)
return 0;
2017-07-12 12:02:20 +02:00
memcpy (theData [nextIn], v, fragmentSize * sizeof (int16_t));
nextIn = (nextIn + 1) % 20;
usedSlots. release ();
2017-10-01 19:07:22 +02:00
#else
processSegment (v);
#endif
2017-07-12 12:02:20 +02:00
return 1;
2017-02-06 14:09:08 +01:00
}
const int16_t interleaveMap [] = {0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
void audioBackend::processSegment (int16_t *Data) {
2017-02-06 14:09:08 +01:00
int16_t i, j;
2017-10-01 19:07:22 +02:00
for (i = 0; i < fragmentSize; i ++) {
tempX [i] = interleaveData [(interleaverIndex +
interleaveMap [i & 017]) & 017][i];
interleaveData [interleaverIndex][i] = Data [i];
}
2017-07-30 20:19:26 +02:00
2017-10-01 19:07:22 +02:00
interleaverIndex = (interleaverIndex + 1) & 0x0F;
#ifdef __THREADED_BACKEND
2017-10-01 19:07:22 +02:00
nextOut = (nextOut + 1) % 20;
freeSlots. release ();
#endif
2017-07-12 12:02:20 +02:00
2017-02-06 14:09:08 +01:00
// only continue when de-interleaver is filled
2017-10-01 19:07:22 +02:00
if (countforInterleaver <= 15) {
countforInterleaver ++;
return;
}
2018-01-17 16:25:29 +01:00
protectionHandler -> deconvolve (tempX. data (),
fragmentSize,
outV. data ());
2017-02-06 14:09:08 +01:00
//
2018-02-09 20:21:44 +01:00
// and the energy dispersal
for (i = 0; i < bitRate * 24; i ++)
outV [i] ^= disperseVector [i];
2017-10-01 19:07:22 +02:00
our_dabProcessor -> addtoFrame (outV);
2017-02-06 14:09:08 +01:00
}
2017-10-01 19:07:22 +02:00
#ifdef __THREADED_BACKEND
void audioBackend::run (void) {
2017-10-01 19:07:22 +02:00
while (running. load ()) {
while (!usedSlots. tryAcquire (1, 200))
if (!running)
return;
processSegment (theData [nextOut]);
}
}
#endif
2017-02-06 14:09:08 +01:00
// It might take a msec for the task to stop
void audioBackend::stopRunning (void) {
#ifdef __THREADED_BACKEND
2017-02-06 14:09:08 +01:00
running = false;
while (this -> isRunning ())
usleep (1);
// myAudioSink -> stop ();
2017-10-01 19:07:22 +02:00
#endif
2017-02-06 14:09:08 +01:00
}