/* Copyright (C) 2015 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org */ /* This file is part of ODR-DPD. ODR-DPD 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. ODR-DPD 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 ODR-DPD. If not, see . */ #include "OutputUHD.hpp" #include "utils.hpp" #include size_t read_samples(FILE* fd, std::vector& samples, size_t count) { if (samples.size() < count) { MDEBUG("HAD TO RESIZE BUFFER!\n"); samples.resize(count); } size_t num_read = fread(&samples.front(), sizeof(complexf), count, fd); if (num_read == 0) { rewind(fd); num_read = fread(&samples.front(), sizeof(complexf), count, fd); } return num_read; } int main(int argc, char **argv) { double txgain = 0; if (argc == 3) { txgain = strtod(argv[2], nullptr); if (!(0 <= txgain and txgain < 80)) { MDEBUG("txgain wrong: %f\n", txgain); return -1; } } MDEBUG("TX Gain is %f\n", txgain); if (argc < 2) { MDEBUG("Require input file or url\n"); return -1; } std::string uri = argv[1]; const size_t samps_per_buffer = 20480; OutputUHD output_uhd(txgain); zmq::context_t ctx; zmq::socket_t zmq_sock(ctx, ZMQ_SUB); FILE* fd = nullptr; if (uri.find("tcp://") != 0) { fd = fopen(uri.c_str(), "rb"); if (!fd) { MDEBUG("Could not open file\n"); return -1; } } else { zmq_sock.connect(uri); zmq_sock.setsockopt(ZMQ_SUBSCRIBE, NULL, 0); } std::vector input_samples(samps_per_buffer); size_t samps_read = 0; size_t total_samps_read = samps_read; double last_print_time = 0; size_t sent = 0; do { double first_sample_time = 0; if (fd) { samps_read = read_samples(fd, input_samples, samps_per_buffer); sent = output_uhd.Transmit(&input_samples.front(), samps_read, &first_sample_time); } else { zmq::message_t msg; if (not zmq_sock.recv(&msg)) { MDEBUG("zmq recv error\n"); return -1; } if (msg.size() % sizeof(complexf) != 0) { MDEBUG("Received incomplete size %zu\n", msg.size()); return -1; } samps_read = msg.size() / sizeof(complexf); sent = output_uhd.Transmit((complexf*)msg.data(), samps_read, &first_sample_time); } if (first_sample_time - last_print_time > 1) { MDEBUG("Tx %zu samples at t=%f\n", samps_read, first_sample_time); last_print_time = first_sample_time; } total_samps_read += samps_read; } while (samps_read and sent); MDEBUG("Read %zu samples in total\n", total_samps_read); }