kopia lustrzana https://github.com/markondej/fm_transmitter
Use float instead of double
rodzic
0b7423090f
commit
f0966e73a3
4
main.cpp
4
main.cpp
|
@ -51,8 +51,8 @@ void sigIntHandler(int sigNum)
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
double frequency = 100.0;
|
float frequency = 100.f;
|
||||||
double bandwidth = 100.0;
|
float bandwidth = 100.f;
|
||||||
uint16_t dmaChannel = 0;
|
uint16_t dmaChannel = 0;
|
||||||
bool loop = false;
|
bool loop = false;
|
||||||
std::string filename;
|
std::string filename;
|
||||||
|
|
12
sample.cpp
12
sample.cpp
|
@ -35,7 +35,7 @@
|
||||||
#include <climits>
|
#include <climits>
|
||||||
|
|
||||||
Sample::Sample(uint8_t *data, uint16_t channels, uint16_t bitsPerChannel)
|
Sample::Sample(uint8_t *data, uint16_t channels, uint16_t bitsPerChannel)
|
||||||
: value(0.)
|
: value(0.f)
|
||||||
{
|
{
|
||||||
int32_t sum = 0;
|
int32_t sum = 0;
|
||||||
int16_t *channelValues = new int16_t[channels];
|
int16_t *channelValues = new int16_t[channels];
|
||||||
|
@ -48,25 +48,25 @@ Sample::Sample(uint8_t *data, uint16_t channels, uint16_t bitsPerChannel)
|
||||||
}
|
}
|
||||||
sum += channelValues[i];
|
sum += channelValues[i];
|
||||||
}
|
}
|
||||||
value = 2 * sum / channels / static_cast<double>(USHRT_MAX);
|
value = 2 * sum / channels / static_cast<float>(USHRT_MAX);
|
||||||
delete[] channelValues;
|
delete[] channelValues;
|
||||||
}
|
}
|
||||||
|
|
||||||
double Sample::getMonoValue()
|
float Sample::getMonoValue()
|
||||||
{
|
{
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef NO_PREEMP
|
#ifndef NO_PREEMP
|
||||||
PreEmphasis::PreEmphasis(uint32_t sampleRate)
|
PreEmphasis::PreEmphasis(uint32_t sampleRate)
|
||||||
: timeConst(sampleRate * 75.0e-6), prevValue(0.)
|
: timeConst(sampleRate * 75.0e-6f), prevValue(0.f)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
double PreEmphasis::filter(double value)
|
float PreEmphasis::filter(float value)
|
||||||
{
|
{
|
||||||
prevValue = value;
|
prevValue = value;
|
||||||
value = value + (prevValue - value) / (1. - timeConst);
|
value = value + (prevValue - value) / (1.f - timeConst);
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -40,9 +40,9 @@ class Sample
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Sample(uint8_t *data, uint16_t channels, uint16_t bitsPerChannel);
|
Sample(uint8_t *data, uint16_t channels, uint16_t bitsPerChannel);
|
||||||
double getMonoValue();
|
float getMonoValue();
|
||||||
protected:
|
protected:
|
||||||
double value;
|
float value;
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifndef NO_PREEMP
|
#ifndef NO_PREEMP
|
||||||
|
@ -50,9 +50,9 @@ class PreEmphasis
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
PreEmphasis(uint32_t sampleRate);
|
PreEmphasis(uint32_t sampleRate);
|
||||||
double filter(double value);
|
float filter(float value);
|
||||||
protected:
|
protected:
|
||||||
double timeConst, prevValue;
|
float timeConst, prevValue;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -165,7 +165,7 @@ uint32_t Transmitter::getPeripheralsSize()
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
double Transmitter::getSourceFreq()
|
float Transmitter::getSourceFreq()
|
||||||
{
|
{
|
||||||
return (getPeripheralsVirtBaseAddress() == BCM2838_PERI_VIRT_BASE) ? BCM2838_PLLD_FREQ : BCM2835_PLLD_FREQ;
|
return (getPeripheralsVirtBaseAddress() == BCM2838_PERI_VIRT_BASE) ? BCM2838_PLLD_FREQ : BCM2835_PLLD_FREQ;
|
||||||
}
|
}
|
||||||
|
@ -214,7 +214,7 @@ void Transmitter::freeMemory(AllocatedMemory &memory)
|
||||||
volatile PWMRegisters *Transmitter::initPwmController()
|
volatile PWMRegisters *Transmitter::initPwmController()
|
||||||
{
|
{
|
||||||
volatile ClockRegisters *pwmClk = reinterpret_cast<ClockRegisters *>(getPeripheralVirtAddress(PWMCLK_BASE_OFFSET));
|
volatile ClockRegisters *pwmClk = reinterpret_cast<ClockRegisters *>(getPeripheralVirtAddress(PWMCLK_BASE_OFFSET));
|
||||||
double pwmClkFreq = PWM_WRITES_PER_SAMPLE * PWM_CHANNEL_RANGE * sampleRate / 1000000;
|
float pwmClkFreq = PWM_WRITES_PER_SAMPLE * PWM_CHANNEL_RANGE * sampleRate / 1000000;
|
||||||
pwmClk->ctl = (0x5A << 24) | 0x06;
|
pwmClk->ctl = (0x5A << 24) | 0x06;
|
||||||
usleep(1000);
|
usleep(1000);
|
||||||
pwmClk->div = (0x5A << 24) | static_cast<uint32_t>(getSourceFreq() * (0x01 << 12) / pwmClkFreq);
|
pwmClk->div = (0x5A << 24) | static_cast<uint32_t>(getSourceFreq() * (0x01 << 12) / pwmClkFreq);
|
||||||
|
@ -270,7 +270,7 @@ void Transmitter::closeClockOutput(volatile ClockRegisters *clock)
|
||||||
clock->ctl = (0x5A << 24) | 0x06;
|
clock->ctl = (0x5A << 24) | 0x06;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Transmitter::transmit(WaveReader &reader, double frequency, double bandwidth, uint8_t dmaChannel, bool preserveCarrierOnExit)
|
void Transmitter::transmit(WaveReader &reader, float frequency, float bandwidth, uint8_t dmaChannel, bool preserveCarrierOnExit)
|
||||||
{
|
{
|
||||||
if (transmitting) {
|
if (transmitting) {
|
||||||
throw std::runtime_error("Cannot play, transmitter already in use");
|
throw std::runtime_error("Cannot play, transmitter already in use");
|
||||||
|
@ -283,7 +283,7 @@ void Transmitter::transmit(WaveReader &reader, double frequency, double bandwidt
|
||||||
|
|
||||||
preserveCarrier = preserveCarrierOnExit;
|
preserveCarrier = preserveCarrierOnExit;
|
||||||
clockDivisor = static_cast<uint32_t>(round(getSourceFreq() * (0x01 << 12) / frequency));
|
clockDivisor = static_cast<uint32_t>(round(getSourceFreq() * (0x01 << 12) / frequency));
|
||||||
divisorRange = clockDivisor - static_cast<uint32_t>(round(getSourceFreq() * (0x01 << 12) / (frequency + 0.0005 * bandwidth)));
|
divisorRange = clockDivisor - static_cast<uint32_t>(round(getSourceFreq() * (0x01 << 12) / (frequency + 0.0005f * bandwidth)));
|
||||||
sampleRate = header.sampleRate;
|
sampleRate = header.sampleRate;
|
||||||
|
|
||||||
output = (output != nullptr) ? output : initClockOutput();
|
output = (output != nullptr) ? output : initClockOutput();
|
||||||
|
@ -377,7 +377,6 @@ void Transmitter::transmitViaDma(WaveReader &reader, uint32_t bufferSize, uint8_
|
||||||
|
|
||||||
volatile PWMRegisters *pwm = initPwmController();
|
volatile PWMRegisters *pwm = initPwmController();
|
||||||
|
|
||||||
double value;
|
|
||||||
uint32_t i, cbOffset = 0;
|
uint32_t i, cbOffset = 0;
|
||||||
#ifndef NO_PREEMP
|
#ifndef NO_PREEMP
|
||||||
PreEmphasis preEmphasis(sampleRate);
|
PreEmphasis preEmphasis(sampleRate);
|
||||||
|
@ -387,7 +386,7 @@ void Transmitter::transmitViaDma(WaveReader &reader, uint32_t bufferSize, uint8_
|
||||||
volatile uint32_t *clkDiv = reinterpret_cast<uint32_t *>(reinterpret_cast<uint32_t>(dmaCb) + 2 * sizeof(DMAControllBlock) * bufferSize);
|
volatile uint32_t *clkDiv = reinterpret_cast<uint32_t *>(reinterpret_cast<uint32_t>(dmaCb) + 2 * sizeof(DMAControllBlock) * bufferSize);
|
||||||
volatile uint32_t *pwmFifoData = reinterpret_cast<uint32_t *>(reinterpret_cast<uint32_t>(clkDiv) + sizeof(uint32_t) * bufferSize);
|
volatile uint32_t *pwmFifoData = reinterpret_cast<uint32_t *>(reinterpret_cast<uint32_t>(clkDiv) + sizeof(uint32_t) * bufferSize);
|
||||||
for (i = 0; i < bufferSize; i++) {
|
for (i = 0; i < bufferSize; i++) {
|
||||||
value = samples[i].getMonoValue();
|
float value = samples[i].getMonoValue();
|
||||||
#ifndef NO_PREEMP
|
#ifndef NO_PREEMP
|
||||||
value = preEmphasis.filter(value);
|
value = preEmphasis.filter(value);
|
||||||
#endif
|
#endif
|
||||||
|
@ -424,7 +423,7 @@ void Transmitter::transmitViaDma(WaveReader &reader, uint32_t bufferSize, uint8_
|
||||||
cbOffset = 0;
|
cbOffset = 0;
|
||||||
eof = samples.size() < bufferSize;
|
eof = samples.size() < bufferSize;
|
||||||
for (i = 0; i < samples.size(); i++) {
|
for (i = 0; i < samples.size(); i++) {
|
||||||
value = samples[i].getMonoValue();
|
float value = samples[i].getMonoValue();
|
||||||
#ifndef NO_PREEMP
|
#ifndef NO_PREEMP
|
||||||
value = preEmphasis.filter(value);
|
value = preEmphasis.filter(value);
|
||||||
#endif
|
#endif
|
||||||
|
@ -489,7 +488,7 @@ void Transmitter::transmitThread()
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
uint32_t prevOffset = offset;
|
uint32_t prevOffset = offset;
|
||||||
double value = samples[offset].getMonoValue();
|
float value = samples[offset].getMonoValue();
|
||||||
#ifndef NO_PREEMP
|
#ifndef NO_PREEMP
|
||||||
value = preEmphasis.filter(value);
|
value = preEmphasis.filter(value);
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -49,13 +49,13 @@ class Transmitter
|
||||||
Transmitter(const Transmitter &source) = delete;
|
Transmitter(const Transmitter &source) = delete;
|
||||||
Transmitter &operator=(const Transmitter &source) = delete;
|
Transmitter &operator=(const Transmitter &source) = delete;
|
||||||
static Transmitter &getInstance();
|
static Transmitter &getInstance();
|
||||||
void transmit(WaveReader &reader, double frequency, double bandwidth, uint8_t dmaChannel, bool preserveCarrierOnExit);
|
void transmit(WaveReader &reader, float frequency, float bandwidth, uint8_t dmaChannel, bool preserveCarrierOnExit);
|
||||||
void stop();
|
void stop();
|
||||||
private:
|
private:
|
||||||
Transmitter();
|
Transmitter();
|
||||||
uint32_t getPeripheralsVirtBaseAddress();
|
uint32_t getPeripheralsVirtBaseAddress();
|
||||||
uint32_t getPeripheralsSize();
|
uint32_t getPeripheralsSize();
|
||||||
double getSourceFreq();
|
float getSourceFreq();
|
||||||
uint32_t getPeripheralPhysAddress(volatile void *object);
|
uint32_t getPeripheralPhysAddress(volatile void *object);
|
||||||
static uint32_t getPeripheralVirtAddress(uint32_t offset);
|
static uint32_t getPeripheralVirtAddress(uint32_t offset);
|
||||||
uint32_t getMemoryPhysAddress(AllocatedMemory &memory, volatile void *object);
|
uint32_t getMemoryPhysAddress(AllocatedMemory &memory, volatile void *object);
|
||||||
|
|
Ładowanie…
Reference in New Issue