Skip to content

Commit

Permalink
audio_rec: Move file name generation to receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
vladisslav2011 committed Jan 27, 2022
1 parent fe2e260 commit cb293ad
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 41 deletions.
26 changes: 19 additions & 7 deletions src/applications/gqrx/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,13 @@ MainWindow::MainWindow(const QString& cfgfile, bool edit_conf, QWidget *parent)
connect(uiDockAudio, SIGNAL(audioMuteChanged(bool)), this, SLOT(setAudioMute(bool)));
connect(uiDockAudio, SIGNAL(audioStreamingStarted(QString,int,bool)), this, SLOT(startAudioStream(QString,int,bool)));
connect(uiDockAudio, SIGNAL(audioStreamingStopped()), this, SLOT(stopAudioStreaming()));
connect(uiDockAudio, SIGNAL(audioRecStarted(QString)), this, SLOT(startAudioRec(QString)));
connect(uiDockAudio, SIGNAL(audioRecStarted(QString)), remote, SLOT(startAudioRecorder(QString)));
connect(uiDockAudio, SIGNAL(audioRecStopped()), this, SLOT(stopAudioRec()));
connect(uiDockAudio, SIGNAL(audioRecStopped()), remote, SLOT(stopAudioRecorder()));
connect(uiDockAudio, SIGNAL(audioRecStart()), this, SLOT(startAudioRec()));
connect(uiDockAudio, SIGNAL(audioRecStart()), remote, SLOT(startAudioRecorder()));
connect(uiDockAudio, SIGNAL(audioRecStop()), this, SLOT(stopAudioRec()));
connect(uiDockAudio, SIGNAL(audioRecStop()), remote, SLOT(stopAudioRecorder()));
connect(uiDockAudio, SIGNAL(audioPlayStarted(QString)), this, SLOT(startAudioPlayback(QString)));
connect(uiDockAudio, SIGNAL(audioPlayStopped()), this, SLOT(stopAudioPlayback()));
connect(uiDockAudio, SIGNAL(recDirChanged(QString)), this, SLOT(recDirChanged(QString)));
connect(uiDockAudio, SIGNAL(fftRateChanged(int)), this, SLOT(setAudioFftRate(int)));
connect(uiDockFft, SIGNAL(fftSizeChanged(int)), this, SLOT(setIqFftSize(int)));
connect(uiDockFft, SIGNAL(fftRateChanged(int)), this, SLOT(setIqFftRate(int)));
Expand Down Expand Up @@ -1525,11 +1526,20 @@ void MainWindow::rdsTimeout()
}
}

/**
* @brief Set audio recording directory.
* @param dir The directory, where audio files should be created.
*/
void MainWindow::recDirChanged(const QString dir)
{
rx->set_audio_rec_dir(dir.toStdString());
}

/**
* @brief Start audio recorder.
* @param filename The file name into which audio should be recorded.
*/
void MainWindow::startAudioRec(const QString& filename)
void MainWindow::startAudioRec()
{
if (!d_have_audio)
{
Expand All @@ -1541,7 +1551,7 @@ void MainWindow::startAudioRec(const QString& filename)
msg_box.exec();
uiDockAudio->setAudioRecButtonState(false);
}
else if (rx->start_audio_recording(filename.toStdString()))
else if (rx->start_audio_recording())
{
ui->statusBar->showMessage(tr("Error starting audio recorder"));

Expand All @@ -1550,7 +1560,8 @@ void MainWindow::startAudioRec(const QString& filename)
}
else
{
ui->statusBar->showMessage(tr("Recording audio to %1").arg(filename));
ui->statusBar->showMessage(tr("Recording audio to %1").arg(rx->get_last_audio_filename().data()));
uiDockAudio->audioRecStarted(QString(rx->get_last_audio_filename().data()));
}
}

Expand All @@ -1562,6 +1573,7 @@ void MainWindow::stopAudioRec()
/* okay, this one would be weird if it really happened */
ui->statusBar->showMessage(tr("Error stopping audio recorder"));

uiDockAudio->audioRecStopped();
uiDockAudio->setAudioRecButtonState(true);
}
else
Expand Down
3 changes: 2 additions & 1 deletion src/applications/gqrx/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ private slots:
void setPassband(int bandwidth);

/* audio recording and playback */
void startAudioRec(const QString& filename);
void recDirChanged(const QString dir);
void startAudioRec();
void stopAudioRec();
void startAudioPlayback(const QString& filename);
void stopAudioPlayback();
Expand Down
31 changes: 25 additions & 6 deletions src/applications/gqrx/receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ receiver::status receiver::set_rf_freq(double freq_hz)
d_rf_freq = freq_hz;

src->set_center_freq(d_rf_freq);
rx->set_center_freq(d_rf_freq);//to generate audio filename
// FIXME: read back frequency?

return STATUS_OK;
Expand Down Expand Up @@ -775,6 +776,7 @@ receiver::status receiver::set_filter_offset(double offset_hz)
{
d_filter_offset = offset_hz;
ddc->set_center_freq(d_filter_offset - d_cw_offset);
rx->set_offset(offset_hz);//to generate audio filename from

return STATUS_OK;
}
Expand Down Expand Up @@ -1110,6 +1112,12 @@ receiver::status receiver::set_amsync_pll_bw(float pll_bw)
return STATUS_OK;
}

receiver::status receiver::set_audio_rec_dir(const std::string dir)
{
rx->set_rec_dir(dir);
return STATUS_OK;
}

/**
* @brief Start WAV file recorder.
* @param filename The filename where to record.
Expand All @@ -1119,7 +1127,7 @@ receiver::status receiver::set_amsync_pll_bw(float pll_bw)
* file names does not work with WAV files (the initial /tmp/gqrx.wav will not be stopped
* because the wav file can not be empty). See https://github.com/gqrx-sdr/gqrx/issues/36
*/
receiver::status receiver::start_audio_recording(const std::string filename)
receiver::status receiver::start_audio_recording()
{
if (d_recording_wav)
{
Expand All @@ -1135,11 +1143,11 @@ receiver::status receiver::start_audio_recording(const std::string filename)

return STATUS_ERROR;
}
if(rx->start_audio_recording(filename) == 0)

if(rx->start_audio_recording() == 0)
{
d_recording_wav = true;
std::cout << "Recording audio to " << filename << std::endl;
std::cout << "Recording audio to " << rx->get_last_audio_filename() << std::endl;
return STATUS_OK;
}
else
Expand Down Expand Up @@ -1171,6 +1179,12 @@ receiver::status receiver::stop_audio_recording()
return STATUS_OK;
}

/** get last recorded audio file name. */
std::string receiver::get_last_audio_filename()
{
return rx->get_last_audio_filename();
}

/** Start audio playback. */
receiver::status receiver::start_audio_playback(const std::string filename)
{
Expand Down Expand Up @@ -1574,8 +1588,13 @@ void receiver::connect_all(rx_chain type, enum file_formats fmt)
tb->connect(rx, 0, audio_snk, 0);
tb->connect(rx, 1, audio_snk, 1);
// Recorders and sniffers
if (d_recording_wav)
rx->continue_audio_recording(old_rx);
if(old_rx.get() != rx.get())
{
if (d_recording_wav)
rx->continue_audio_recording(old_rx);
rx->set_center_freq(d_rf_freq);
rx->set_offset(d_filter_offset);
}

if (d_sniffer_active)
{
Expand Down
4 changes: 3 additions & 1 deletion src/applications/gqrx/receiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,10 @@ class receiver
status set_amsync_pll_bw(float pll_bw);

/* Audio parameters */
status start_audio_recording(const std::string filename);
status set_audio_rec_dir(const std::string dir);
status start_audio_recording();
status stop_audio_recording();
std::string get_last_audio_filename();
status start_audio_playback(const std::string filename);
status stop_audio_playback();

Expand Down
2 changes: 1 addition & 1 deletion src/applications/gqrx/remote_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ void RemoteControl::setSquelchLevel(double level)
}

/*! \brief Start audio recorder (from mainwindow). */
void RemoteControl::startAudioRecorder(QString unused)
void RemoteControl::startAudioRecorder()
{
if (rc_mode > 0)
audio_recorder_status = true;
Expand Down
2 changes: 1 addition & 1 deletion src/applications/gqrx/remote_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public slots:
void setMode(int mode);
void setPassband(int passband_lo, int passband_hi);
void setSquelchLevel(double level);
void startAudioRecorder(QString unused);
void startAudioRecorder();
void stopAudioRecorder();
bool setGain(QString name, double gain);
void setRDSstatus(bool enabled);
Expand Down
36 changes: 21 additions & 15 deletions src/qtgui/dockaudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,25 +223,12 @@ void DockAudio::on_audioStreamButton_clicked(bool checked)
void DockAudio::on_audioRecButton_clicked(bool checked)
{
if (checked) {
// FIXME: option to use local time
// use toUTC() function compatible with older versions of Qt.
QString file_name = QDateTime::currentDateTime().toUTC().toString("gqrx_yyyyMMdd_hhmmss");
last_audio = QString("%1/%2_%3.wav").arg(rec_dir).arg(file_name).arg(rx_freq);
QFileInfo info(last_audio);

// emit signal and start timer
emit audioRecStarted(last_audio);

ui->audioRecLabel->setText(info.fileName());
ui->audioRecButton->setToolTip(tr("Stop audio recorder"));
ui->audioPlayButton->setEnabled(false); /* prevent playback while recording */
emit audioRecStart();
}
else {
ui->audioRecLabel->setText("<i>DSP</i>");
ui->audioRecButton->setToolTip(tr("Start audio recorder"));
emit audioRecStopped();

ui->audioPlayButton->setEnabled(true);
emit audioRecStop();
}
}

Expand Down Expand Up @@ -460,6 +447,7 @@ void DockAudio::setNewWaterfallRange(int min, int max)
void DockAudio::setNewRecDir(const QString &dir)
{
rec_dir = dir;
emit recDirChanged(dir);
}

/*! \brief Slot called when a new network host has been entered. */
Expand All @@ -483,6 +471,24 @@ void DockAudio::setNewUdpStereo(bool enabled)
udp_stereo = enabled;
}

/*! \brief Slot called when audio recording is started after clicking rec or being triggered by squelch. */
void DockAudio::audioRecStarted(const QString filename)
{
last_audio = filename;
QFileInfo info(last_audio);
ui->audioRecLabel->setText(info.fileName());
ui->audioRecButton->setToolTip(tr("Stop audio recorder"));
ui->audioPlayButton->setEnabled(false); /* prevent playback while recording */
}

void DockAudio::audioRecStopped()
{
ui->audioRecLabel->setText("<i>DSP</i>");
ui->audioRecButton->setToolTip(tr("Start audio recorder"));
ui->audioPlayButton->setEnabled(true);
}


void DockAudio::recordToggleShortcut() {
ui->audioRecButton->click();
}
Expand Down
9 changes: 7 additions & 2 deletions src/qtgui/dockaudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public slots:
void stopAudioRecorder(void);
void setRxFrequency(qint64 freq);
void setWfColormap(const QString &cmap);
void audioRecStarted(const QString filename);
void audioRecStopped();

signals:
/*! \brief Signal emitted when audio gain has changed. Gain is in dB. */
Expand All @@ -85,10 +87,10 @@ public slots:
void audioStreamingStopped();

/*! \brief Signal emitted when audio recording is started. */
void audioRecStarted(const QString filename);
void audioRecStart();

/*! \brief Signal emitted when audio recording is stopped. */
void audioRecStopped();
void audioRecStop();

/*! \brief Signal emitted when audio playback is started. */
void audioPlayStarted(const QString filename);
Expand All @@ -102,6 +104,9 @@ public slots:
/*! \brief Signal emitted when audio mute has changed. */
void audioMuteChanged(bool mute);

/*! \brief Signal emitted when recording directory has changed. */
void recDirChanged(const QString dir);

private slots:
void on_audioGainSlider_valueChanged(int value);
void on_audioStreamButton_clicked(bool checked);
Expand Down
36 changes: 32 additions & 4 deletions src/receivers/receiver_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <gnuradio/io_signature.h>
#include "receivers/receiver_base.h"
#include <QDebug>
#include <QDateTime>
#include <QDir>


static const int MIN_IN = 1; /* Minimum number of input streams. */
Expand Down Expand Up @@ -61,6 +63,21 @@ void receiver_base_cf::set_quad_rate(float quad_rate)
}
}

void receiver_base_cf::set_center_freq(double center_freq)
{
d_center_freq = center_freq;
}

void receiver_base_cf::set_offset(double offset)
{
d_offset = offset;
}

void receiver_base_cf::set_rec_dir(std::string dir)
{
d_rec_dir = dir;
}

float receiver_base_cf::get_signal_level()
{
return meter->get_level_db();
Expand Down Expand Up @@ -206,22 +223,28 @@ bool receiver_base_cf::is_rds_decoder_active()
return false;
}

int receiver_base_cf::start_audio_recording(const std::string filename)
int receiver_base_cf::start_audio_recording()
{
// FIXME: option to use local time
// use toUTC() function compatible with older versions of Qt.
QString file_name = QDateTime::currentDateTime().toUTC().toString("gqrx_yyyyMMdd_hhmmss");
QString filename = QString("%1/%2_%3.wav").arg(QString(d_rec_dir.data())).arg(file_name).arg(qint64(d_center_freq + d_offset));
d_audio_filename = filename.toStdString();

// if this fails, we don't want to go and crash now, do we
try {
#if GNURADIO_VERSION < 0x030900
wav_sink = gr::blocks::wavfile_sink::make(filename.c_str(), 2,
wav_sink = gr::blocks::wavfile_sink::make(d_audio_filename.c_str(), 2,
(unsigned int) d_audio_rate,
16);
#else
wav_sink = gr::blocks::wavfile_sink::make(filename.c_str(), 2,
wav_sink = gr::blocks::wavfile_sink::make(d_audio_filename.c_str(), 2,
(unsigned int) d_audio_rate,
gr::blocks::FORMAT_WAV, gr::blocks::FORMAT_PCM_16);
#endif
}
catch (std::runtime_error &e) {
std::cout << "Error opening " << filename << ": " << e.what() << std::endl;
std::cout << "Error opening " << d_audio_filename << ": " << e.what() << std::endl;
return 1;
}

Expand Down Expand Up @@ -256,3 +279,8 @@ void receiver_base_cf::continue_audio_recording(receiver_base_cf_sptr from)
connect(agc, 1, wav_sink, 1);
from->wav_sink.reset();
}

std::string receiver_base_cf::get_last_audio_filename()
{
return d_audio_filename;
}
15 changes: 12 additions & 3 deletions src/receivers/receiver_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class receiver_base_cf : public gr::hier_block2
virtual bool stop() = 0;

virtual void set_quad_rate(float quad_rate);
virtual void set_center_freq(double center_freq);
virtual void set_offset(double offset);
virtual void set_rec_dir(std::string dir);

virtual void set_filter(double low, double high, double tw) = 0;
virtual void set_cw_offset(double offset) = 0;
Expand Down Expand Up @@ -111,12 +114,18 @@ class receiver_base_cf : public gr::hier_block2
virtual void stop_rds_decoder();
virtual void reset_rds_parser();
virtual bool is_rds_decoder_active();
virtual int start_audio_recording(std::string filename);
virtual int start_audio_recording();
virtual void stop_audio_recording();
virtual void continue_audio_recording(receiver_base_cf_sptr from);
virtual std::string get_last_audio_filename();

protected:
float d_quad_rate; /*!< Input sample rate. */
int d_audio_rate; /*!< Audio output rate. */
float d_quad_rate; /*!< Input sample rate. */
int d_audio_rate; /*!< Audio output rate. */
double d_center_freq;
double d_offset;
std::string d_rec_dir;
std::string d_audio_filename;

resampler_cc_sptr iq_resamp; /*!< Baseband resampler. */
rx_meter_c_sptr meter; /*!< Signal strength. */
Expand Down

0 comments on commit cb293ad

Please sign in to comment.