Ed Coleman
Ed Coleman

Reputation: 137

Interrupting a simple GNUradio flow

I'm experimenting with writing GNUradio scripts in python. My eventual goal is to have a routine that periodically writes a floating point result from within a GNUradio process to the serial port. As a first step I wanted to simply pause a simple routine The following code plays a 1kHz tone thru the sound card:

`#!/usr/bin/env python
##################################################
# Gnuradio Python Flow Graph
# Title: simpleTone
# Generated: Wed Jun 29 07:26:02 2016
##################################################

from gnuradio import analog
from gnuradio import audio
from gnuradio import blocks
from gnuradio import gr
import time

class simpleTone(gr.top_block):

    def __init__(self):
    gr.top_block.__init__(self)

        ##################################################
        # Variables
        ##################################################
        self.samp_rate = samp_rate = 32000

        ##################################################
        # Blocks
        ##################################################
        self.blocks_throttle_0 = blocks.throttle(gr.sizeof_float*1, samp_rate)
        self.audio_sink_0 = audio.sink(samp_rate, "", True)
        self.analog_sig_source_x_0 = analog.sig_source_f(samp_rate, analog.GR_COS_WAVE, 1000, 1, 0)

        ##################################################
        # Connections
        ##################################################
        self.connect((self.analog_sig_source_x_0, 0), (self.blocks_throttle_0, 0))
        self.connect((self.blocks_throttle_0, 0), (self.audio_sink_0, 0))


if __name__ == '__main__':
    simpleTone().run()`

The code above works fine, with the exception of some overflows and clicking in the audio. However if I make the following substitution:

`        
if __name__ == '__main__':
    simpleTone().start()
    time.sleep(3)
    simpleTone().stop()`

The result is that the file runs, and does end after 3 seconds but no audio is produced.

I'm sure I've missed something fairly basic, any help would be appreciated. Thanks.

-Ed

Upvotes: 2

Views: 3641

Answers (2)

Suman Bhunia
Suman Bhunia

Reputation: 1

This is problem with the way you are crating objects in python. Each time you are calling 'simpleTone()', you are actually initiating a new object. of the class simpleTone. I agree with the earlier answer that creating one object will fix the problem.

Upvotes: 0

Ed Coleman
Ed Coleman

Reputation: 137

This question was also posed here:

http://lists.gnu.org/archive/html/discuss-gnuradio

and was answered. I will share that answer here in case someone comes across this question:

Your problem is that you're constructing three unrelated top blocks: you have three separate occurrences of "simpleTone()". Instead you need to create one and continue to use it, like so:

tb = simpleTone()
tb.start()
...
tb.stop()

You have another problem, too, which you will find after fixing the first one. .wait() means to wait for the flowgraph to finish all processing, and your flowgraph has no elements within it to finish such as a Head block, so the .stop() will never be reached.

Instead, you need to proceed like this:

tb = simpleTone()
tb.start()
# the flowgraph is now running independently
time.sleep(3)
tb.stop()
tb.wait()

The final .wait() is not actually necessary in this case -- what it does is wait for the flowgraph to finish, which will happen shortly after .stop() is called, but if you later wish to start the same top block again, you must have called .wait() before you call .start(), so always having a matched set of [start, stop, wait] or [start, wait, stop] is good practice.

Upvotes: 5

Related Questions