Skip to content

Commit

Permalink
move wav_sink to receiver_base_cf
Browse files Browse the repository at this point in the history
Prepare for squelch-triggered recording
  • Loading branch information
vladisslav2011 committed Jan 27, 2022
1 parent d678276 commit fe2e260
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 46 deletions.
55 changes: 11 additions & 44 deletions src/applications/gqrx/receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1135,33 +1135,15 @@ receiver::status receiver::start_audio_recording(const std::string filename)

return STATUS_ERROR;
}

// 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,
(unsigned int) d_audio_rate,
16);
#else
wav_sink = gr::blocks::wavfile_sink::make(filename.c_str(), 2,
(unsigned int) d_audio_rate,
gr::blocks::FORMAT_WAV, gr::blocks::FORMAT_PCM_16);
#endif

if(rx->start_audio_recording(filename) == 0)
{
d_recording_wav = true;
std::cout << "Recording audio to " << filename << std::endl;
return STATUS_OK;
}
catch (std::runtime_error &e) {
std::cout << "Error opening " << filename << ": " << e.what() << std::endl;
else
return STATUS_ERROR;
}

tb->lock();
tb->connect(rx, 0, wav_sink, 0);
tb->connect(rx, 1, wav_sink, 1);
tb->unlock();
d_recording_wav = true;

std::cout << "Recording audio to " << filename << std::endl;

return STATUS_OK;
}

/** Stop WAV file recorder. */
Expand All @@ -1180,20 +1162,8 @@ receiver::status receiver::stop_audio_recording()

return STATUS_ERROR;
}
rx->stop_audio_recording();

// not strictly necessary to lock but I think it is safer
tb->lock();
wav_sink->close();
tb->disconnect(rx, 0, wav_sink, 0);
tb->disconnect(rx, 1, wav_sink, 1);

// Temporary workaround for https://github.com/gnuradio/gnuradio/issues/5436
tb->disconnect(ddc, 0, rx, 0);
tb->connect(ddc, 0, rx, 0);
// End temporary workaronud

tb->unlock();
wav_sink.reset();
d_recording_wav = false;

std::cout << "Audio recorder stopped" << std::endl;
Expand Down Expand Up @@ -1569,6 +1539,7 @@ void receiver::connect_all(rx_chain type, enum file_formats fmt)
// Visualization
tb->connect(b, 0, iq_fft, 0);

receiver_base_cf_sptr old_rx = rx;
// RX demod chain
switch (type)
{
Expand Down Expand Up @@ -1604,10 +1575,7 @@ void receiver::connect_all(rx_chain type, enum file_formats fmt)
tb->connect(rx, 1, audio_snk, 1);
// Recorders and sniffers
if (d_recording_wav)
{
tb->connect(rx, 0, wav_sink, 0);
tb->connect(rx, 1, wav_sink, 1);
}
rx->continue_audio_recording(old_rx);

if (d_sniffer_active)
{
Expand All @@ -1619,8 +1587,7 @@ void receiver::connect_all(rx_chain type, enum file_formats fmt)
{
if (d_recording_wav)
{
wav_sink->close();
wav_sink.reset();
rx->stop_audio_recording();
d_recording_wav = false;
}

Expand Down
2 changes: 0 additions & 2 deletions src/applications/gqrx/receiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
//#include <gnuradio/blocks/file_sink.h>
//#include <gnuradio/blocks/file_source.h>
#include <gnuradio/blocks/null_sink.h>
#include <gnuradio/blocks/wavfile_sink.h>
#include <gnuradio/blocks/wavfile_source.h>
#include <gnuradio/blocks/throttle.h>
#include <gnuradio/top_block.h>
Expand Down Expand Up @@ -332,7 +331,6 @@ class receiver
gr::blocks::throttle::sptr input_throttle;
file_source::sptr input_file;

gr::blocks::wavfile_sink::sptr wav_sink; /*!< WAV file sink for recording. */
gr::blocks::wavfile_source::sptr wav_src; /*!< WAV file source for playback. */
gr::blocks::null_sink::sptr audio_null_sink0; /*!< Audio null sink used during playback. */
gr::blocks::null_sink::sptr audio_null_sink1; /*!< Audio null sink used during playback. */
Expand Down
51 changes: 51 additions & 0 deletions src/receivers/receiver_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,54 @@ bool receiver_base_cf::is_rds_decoder_active()
{
return false;
}

int receiver_base_cf::start_audio_recording(const std::string filename)
{
// 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,
(unsigned int) d_audio_rate,
16);
#else
wav_sink = gr::blocks::wavfile_sink::make(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;
return 1;
}

lock();
connect(agc, 0, wav_sink, 0);
connect(agc, 1, wav_sink, 1);
unlock();
return 0;
}

void receiver_base_cf::stop_audio_recording()
{
// not strictly necessary to lock but I think it is safer
lock();
wav_sink->close();
disconnect(agc, 0, wav_sink, 0);
disconnect(agc, 1, wav_sink, 1);

unlock();
wav_sink.reset();
}

//FIXME Reimplement wavfile_sink correctly to make this work as expected
void receiver_base_cf::continue_audio_recording(receiver_base_cf_sptr from)
{
if(from.get() == this)
return;
from->disconnect(from->agc, 0, from->wav_sink, 0);
from->disconnect(from->agc, 1, from->wav_sink, 1);
wav_sink = from->wav_sink;
connect(agc, 0, wav_sink, 0);
connect(agc, 1, wav_sink, 1);
from->wav_sink.reset();
}
5 changes: 5 additions & 0 deletions src/receivers/receiver_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <gnuradio/hier_block2.h>
#include <gnuradio/analog/simple_squelch_cc.h>
#include <gnuradio/blocks/wavfile_sink.h>
#include "dsp/resampler_xx.h"
#include "dsp/rx_meter.h"
#include "dsp/rx_agc_xx.h"
Expand Down Expand Up @@ -110,6 +111,9 @@ 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 void stop_audio_recording();
virtual void continue_audio_recording(receiver_base_cf_sptr from);
protected:
float d_quad_rate; /*!< Input sample rate. */
int d_audio_rate; /*!< Audio output rate. */
Expand All @@ -118,6 +122,7 @@ class receiver_base_cf : public gr::hier_block2
rx_meter_c_sptr meter; /*!< Signal strength. */
rx_agc_2f_sptr agc; /*!< Receiver AGC. */
gr::analog::simple_squelch_cc::sptr sql; /*!< Squelch. */
gr::blocks::wavfile_sink::sptr wav_sink; /*!< WAV file sink for recording. */
private:
float d_pref_quad_rate;
};
Expand Down

0 comments on commit fe2e260

Please sign in to comment.