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