def __make_demodulator(self, mode, state): """Returns the demodulator.""" t0 = time.time() mode_def = lookup_mode(mode) if mode_def is None: # TODO: Better handling, like maybe a dummy demod raise ValueError('Unknown mode: ' + mode) clas = mode_def.demod_class state = state.copy() # don't modify arg if 'mode' in state: del state['mode'] # don't switch back to the mode we just switched from facet = ContextForDemodulator(self) init_kwargs = dict( mode=mode, input_rate=self.__get_device().get_rx_driver().get_output_type().get_sample_rate(), context=facet) demodulator = IDemodulator(unserialize_exported_state( ctor=clas, state=state, kwargs=init_kwargs)) # until _enabled, ignore any callbacks resulting from unserialization calling setters facet._enabled = True log.msg('Constructed %s demodulator: %i ms.' % (mode, (time.time() - t0) * 1000)) return demodulator
def __make_demodulator(self, mode, state): """Returns the demodulator.""" t0 = time.time() mode_def = lookup_mode(mode) if mode_def is None: # TODO: Better handling, like maybe a dummy demod raise ValueError('Unknown mode: ' + mode) clas = mode_def.demod_class state = state.copy() # don't modify arg if 'mode' in state: del state[ 'mode'] # don't switch back to the mode we just switched from facet = ContextForDemodulator(self) init_kwargs = dict(mode=mode, input_rate=self.__get_device().get_rx_driver(). get_output_type().get_sample_rate(), context=facet) demodulator = unserialize_exported_state(ctor=clas, state=state, kwargs=init_kwargs) # until _enabled, ignore any callbacks resulting from unserialization calling setters facet._enabled = True log.msg('Constructed %s demodulator: %i ms.' % (mode, (time.time() - t0) * 1000)) return demodulator
def setUpFor(self, mode, demod_class=None, state=None, skip_if_unavailable=False): # pylint: disable=attribute-defined-outside-init if state is None: state = {} mode_def = lookup_mode(mode, include_unavailable=True) if mode_def and not mode_def.available and skip_if_unavailable: raise unittest.SkipTest( 'mode {!r} marked unavailable'.format(mode)) if mode_def is not None and demod_class is None: demod_class = mode_def.demod_class if demod_class is None: if mode_def is None: raise Exception('Mode {!r} not registered'.format(mode)) else: raise Exception( 'Demodulator not registered for mode {!r}'.format(mode)) # Wire up top block. We don't actually want to inspect the signal processing; we just want to see if GR has a complaint about the flow graph connectivity. self.__top = gr.top_block() self.__adapter = DemodulatorAdapter(mode=mode, demod_class=demod_class, input_rate=100000, output_rate=22050, quiet=True) self.demodulator = self.__adapter.get_demodulator() self.__top.connect(blocks.vector_source_c([]), (self.__adapter, 0), blocks.null_sink(gr.sizeof_float)) self.__top.connect((self.__adapter, 1), blocks.null_sink(gr.sizeof_float)) DemodulatorTestCase.setUp(self) # neither super nor self call
def __init__(self, mode, input_rate, output_rate, mod_class=None): gr.hier_block2.__init__(self, type(self).__name__, gr.io_signature(1, 1, gr.sizeof_float), gr.io_signature(1, 1, gr.sizeof_gr_complex)) if mod_class is None: mode_def = lookup_mode(mode) if mode_def is None: raise Exception( '{}: No modulator registered for mode {!r}, only {!r}'. format( type(self).__name__, mode, [md.mode for md in get_modes() if md.mod_class])) mod_class = mode_def.mod_class context = _ModulatorAdapterContext(adapter=self) modulator = self.__modulator = IModulator( mod_class(mode=mode, context=context)) self.__connect_with_resampling( self, input_rate, modulator, modulator.get_input_type().get_sample_rate(), False) self.__connect_with_resampling( modulator, modulator.get_output_type().get_sample_rate(), self, output_rate, True)
def __init__(self, mode, input_rate, output_rate, mod_class=None): gr.hier_block2.__init__( self, type(self).__name__, gr.io_signature(1, 1, gr.sizeof_float), gr.io_signature(1, 1, gr.sizeof_gr_complex)) if mod_class is None: mode_def = lookup_mode(mode) if mode_def is None: raise Exception('{}: No modulator registered for mode {!r}, only {!r}'.format( type(self).__name__, mode, [md.mode for md in get_modes() if md.mod_class])) mod_class = mode_def.mod_class context = _ModulatorAdapterContext(adapter=self) modulator = self.__modulator = IModulator(mod_class( mode=mode, context=context)) self.__connect_with_resampling( self, input_rate, modulator, modulator.get_input_type().get_sample_rate(), False) self.__connect_with_resampling( modulator, modulator.get_output_type().get_sample_rate(), self, output_rate, True)
def setUpFor(self, mode, demod_class=None, state=None, skip_if_unavailable=False): # pylint: disable=attribute-defined-outside-init if state is None: state = {} mode_def = lookup_mode(mode, include_unavailable=True) if mode_def and not mode_def.available and skip_if_unavailable: raise unittest.SkipTest('mode {!r} marked unavailable'.format(mode)) if mode_def is not None and demod_class is None: demod_class = mode_def.demod_class if demod_class is None: if mode_def is None: raise Exception('Mode {!r} not registered'.format(mode)) else: raise Exception('Demodulator not registered for mode {!r}'.format(mode)) # Wire up top block. We don't actually want to inspect the signal processing; we just want to see if GR has a complaint about the flow graph connectivity. self.__top = gr.top_block() self.__adapter = DemodulatorAdapter( mode=mode, demod_class=demod_class, input_rate=100000, output_rate=22050, quiet=True) self.demodulator = self.__adapter.get_demodulator() self.__top.connect( blocks.vector_source_c([]), (self.__adapter, 0), blocks.null_sink(gr.sizeof_float)) self.__top.connect( (self.__adapter, 1), blocks.null_sink(gr.sizeof_float)) DemodulatorTestCase.setUp(self) # neither super nor self call
def __init__(self, mode, freq_absolute=100.0, freq_relative=None, freq_linked_to_device=False, audio_destination=None, device_name=None, audio_gain=-6, audio_pan=0, audio_channels=0, context=None): assert audio_channels == 1 or audio_channels == 2 assert audio_destination is not None assert device_name is not None gr.hier_block2.__init__( # str() because insists on non-unicode self, str('%s receiver' % (mode,)), gr.io_signature(1, 1, gr.sizeof_gr_complex * 1), gr.io_signature(audio_channels, audio_channels, gr.sizeof_float * 1), ) if lookup_mode(mode) is None: # TODO: communicate back to client if applicable log.msg('Unknown mode %r in Receiver(); using AM' % (mode,)) mode = 'AM' # Provided by caller self.context = context self.__audio_channels = audio_channels # cached info from device self.__device_name = device_name # Simple state self.mode = mode self.audio_gain = audio_gain self.audio_pan = min(1, max(-1, audio_pan)) self.__audio_destination = audio_destination # Receive frequency. self.__freq_linked_to_device = bool(freq_linked_to_device) if self.__freq_linked_to_device and freq_relative is not None: self.__freq_relative = float(freq_relative) self.__freq_absolute = self.__freq_relative + self.__get_device().get_freq() else: self.__freq_absolute = float(freq_absolute) self.__freq_relative = self.__freq_absolute - self.__get_device().get_freq() # Blocks self.__rotator = blocks.rotator_cc() self.__demodulator = self.__make_demodulator(mode, {}) self.__update_demodulator_info() self.__audio_gain_blocks = [blocks.multiply_const_ff(0.0) for _ in xrange(self.__audio_channels)] self.probe_audio = analog.probe_avg_mag_sqrd_f(0, alpha=10.0 / 44100) # TODO adapt to output audio rate # Other internals self.__last_output_type = None self.__update_rotator() # initialize rotator, also in case of __demod_tunable self.__update_audio_gain() self.__do_connect(reason=u'initialization')
def test_no_audio_demodulator(self): """Smoke test for demodulator with no audio output.""" # TODO: Allow parameterizing with a different mode table so that we can use a test stub mode rather than a real one. Also fix rtl_433 leaving unclean reactor. for mode in ['MODE-S']: if lookup_mode(mode): self.receiver.set_mode(mode) break else: raise unittest.SkipTest('No no-audio mode available.')
def __init__(self, mode, input_rate, output_rate, demod_class=None, freq=0.0, quiet=False): gr.hier_block2.__init__( self, type(self).__name__, gr.io_signature(1, 1, gr.sizeof_gr_complex), gr.io_signature(2, 2, gr.sizeof_float)) if demod_class is None: mode_def = lookup_mode(mode) if mode_def is None: raise Exception('{}: No demodulator registered for mode {!r}, only {!r}'.format( type(self).__name__, mode, [md.mode for md in get_modes()])) demod_class = mode_def.demod_class context = _DemodulatorAdapterContext(adapter=self, freq=freq) demod = self.__demodulator = IDemodulator(demod_class( mode=mode, input_rate=input_rate, context=context)) self.connect(self, demod) output_type = demod.get_output_type() demod_output_rate = output_type.get_sample_rate() same_rate = demod_output_rate == output_rate stereo = output_type.get_kind() == 'STEREO' # connect outputs, resampling and adapting mono/stereo as needed # TODO: Make the logic for this in receiver.py reusable? if output_type.get_kind() == 'NONE': # TODO: produce correct sample rate of zeroes and maybe a warning dummy = blocks.vector_source_f([]) self.connect(dummy, (self, 0)) self.connect(dummy, (self, 1)) else: if stereo: splitter = blocks.vector_to_streams(gr.sizeof_float, 2) self.connect(demod, splitter) if same_rate: if stereo: self.connect((splitter, 0), (self, 0)) self.connect((splitter, 1), (self, 1)) else: self.connect(demod, (self, 0)) self.connect(demod, (self, 1)) else: if not quiet: gr.log.info(b'{}: Native {} demodulated rate is {}; resampling to {}'.format( type(self).__name__, mode, demod_output_rate, output_rate)) if stereo: self.connect((splitter, 0), make_resampler(demod_output_rate, output_rate), (self, 0)) self.connect((splitter, 1), make_resampler(demod_output_rate, output_rate), (self, 1)) else: resampler = make_resampler(demod_output_rate, output_rate) self.connect(demod, resampler, (self, 0)) self.connect(resampler, (self, 1))
def __init__(self, mode, input_rate, output_rate, demod_class=None, freq=0.0, quiet=False): gr.hier_block2.__init__( self, type(self).__name__, gr.io_signature(1, 1, gr.sizeof_gr_complex), gr.io_signature(2, 2, gr.sizeof_float)) if demod_class is None: mode_def = lookup_mode(mode) if mode_def is None: raise Exception('{}: No demodulator registered for mode {!r}, only {!r}'.format( type(self).__name__, mode, [md.mode for md in get_modes()])) demod_class = mode_def.demod_class context = _DemodulatorAdapterContext(adapter=self, freq=freq) demod = self.__demodulator = IDemodulator(demod_class( mode=mode, input_rate=input_rate, context=context)) self.connect(self, demod) output_type = demod.get_output_type() demod_output_rate = output_type.get_sample_rate() same_rate = demod_output_rate == output_rate stereo = output_type.get_kind() == 'STEREO' # connect outputs, resampling and adapting mono/stereo as needed # TODO: Make the logic for this in receiver.py reusable? if output_type.get_kind() == 'NONE': # TODO: produce correct sample rate of zeroes and maybe a warning dummy = blocks.vector_source_f([]) self.connect(dummy, (self, 0)) self.connect(dummy, (self, 1)) else: if stereo: splitter = blocks.vector_to_streams(gr.sizeof_float, 2) self.connect(demod, splitter) if same_rate: if stereo: self.connect((splitter, 0), (self, 0)) self.connect((splitter, 1), (self, 1)) else: self.connect(demod, (self, 0)) self.connect(demod, (self, 1)) else: if not quiet: gr.log.info('{}: Native {} demodulated rate is {}; resampling to {}'.format( type(self).__name__, mode, demod_output_rate, output_rate)) if stereo: self.connect((splitter, 0), make_resampler(demod_output_rate, output_rate), (self, 0)) self.connect((splitter, 1), make_resampler(demod_output_rate, output_rate), (self, 1)) else: resampler = make_resampler(demod_output_rate, output_rate) self.connect(demod, resampler, (self, 0)) self.connect(resampler, (self, 1))
def __init__(self, mode, state=None): # TODO: Refactor things so that we can take the demod ctor rather than a mode string if state is None: state = {} mode_def = lookup_mode(mode) if mode_def is None: raise Exception('No such mode is registered: ' + repr(mode)) # TODO: Tell the simulated device to have no modulators, or have a simpler dummy source for testing, so we don't waste time on setup self.__top = Top(devices={'s1': SimulatedDevice()}) (_, receiver) = self.__top.add_receiver(mode, key='a', state=state) self.__demodulator = receiver.get_demodulator() if not isinstance(self.__demodulator, mode_def.demod_class): raise Exception('Demodulator not of expected class: ' + repr(self.__demodulator)) self.__top.start() # TODO overriding internals
def __init__(self, mode, input_rate, output_rate, freq=0.0): gr.hier_block2.__init__(self, type(self).__name__, gr.io_signature(1, 1, gr.sizeof_gr_complex), gr.io_signature(2, 2, gr.sizeof_float)) mode_def = lookup_mode(mode) if mode_def is None: raise Exception( '{}: No demodulator registered for mode {!r}, only {!r}'. format( type(self).__name__, mode, [md.mode for md in get_modes()])) context = _DemodulatorAdapterContext(adapter=self, freq=freq) demod = self.__demodulator = IDemodulator( mode_def.demod_class(mode=mode, input_rate=input_rate, context=context)) self.connect(self, demod) output_type = demod.get_output_type() demod_output_rate = output_type.get_sample_rate() same_rate = demod_output_rate == output_rate stereo = output_type.get_kind() == 'STEREO' # connect outputs, resampling and adapting mono/stereo as needed if same_rate: self.connect((demod, 0), (self, 0)) self.connect((demod, 1 if stereo else 0), (self, 1)) else: gr.log.info( '{}: Native {} demodulated rate is {}; resampling to {}'. format( type(self).__name__, mode, demod_output_rate, output_rate)) if stereo: self.connect((demod, 0), make_resampler(demod_output_rate, output_rate), (self, 0)) self.connect((demod, 1), make_resampler(demod_output_rate, output_rate), (self, 1)) else: resampler = make_resampler(demod_output_rate, output_rate) self.connect((demod, 0), resampler, (self, 0)) self.connect(resampler, (self, 1))
def add_modulator(freq, key, mode_or_modulator_ctor, **kwargs): if isinstance(mode_or_modulator_ctor, type): mode = None ctor = mode_or_modulator_ctor else: mode = mode_or_modulator_ctor mode_def = lookup_mode(mode) if mode_def is None: # missing plugin, say return ctor = mode_def.mod_class context = None # TODO implement context modulator = ctor(context=context, mode=mode, **kwargs) tx = _SimulatedTransmitter(modulator, audio_rate, rf_rate, freq) self.connect(audio_signal, tx) signals.append(tx) self.__transmitters[key] = tx