1# Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. 2# 3# Use of this source code is governed by a BSD-style license 4# that can be found in the LICENSE file in the root of the source 5# tree. An additional intellectual property rights grant can be found 6# in the file PATENTS. All contributing project authors may 7# be found in the AUTHORS file in the root of the source tree. 8 9"""Input signal creator module. 10""" 11 12from . import exceptions 13from . import signal_processing 14 15 16class InputSignalCreator(object): 17 """Input signal creator class. 18 """ 19 20 @classmethod 21 def Create(cls, name, raw_params): 22 """Creates a input signal and its metadata. 23 24 Args: 25 name: Input signal creator name. 26 raw_params: Tuple of parameters to pass to the specific signal creator. 27 28 Returns: 29 (AudioSegment, dict) tuple. 30 """ 31 try: 32 signal = {} 33 params = {} 34 35 if name == 'pure_tone': 36 params['frequency'] = float(raw_params[0]) 37 params['duration'] = int(raw_params[1]) 38 signal = cls._CreatePureTone(params['frequency'], params['duration']) 39 else: 40 raise exceptions.InputSignalCreatorException( 41 'Invalid input signal creator name') 42 43 # Complete metadata. 44 params['signal'] = name 45 46 return signal, params 47 except (TypeError, AssertionError) as e: 48 raise exceptions.InputSignalCreatorException( 49 'Invalid signal creator parameters: {}'.format(e)) 50 51 @classmethod 52 def _CreatePureTone(cls, frequency, duration): 53 """ 54 Generates a pure tone at 48000 Hz. 55 56 Args: 57 frequency: Float in (0-24000] (Hz). 58 duration: Integer (milliseconds). 59 60 Returns: 61 AudioSegment instance. 62 """ 63 assert 0 < frequency <= 24000 64 assert duration > 0 65 template = signal_processing.SignalProcessingUtils.GenerateSilence(duration) 66 return signal_processing.SignalProcessingUtils.GeneratePureTone( 67 template, frequency) 68