Skip to content

Commit c31257a

Browse files
committed
Added semitones_to_ratio and ratio_to_semitones
1 parent 05e2c6e commit c31257a

File tree

2 files changed

+125
-2
lines changed

2 files changed

+125
-2
lines changed

docs/usage.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ Search for pitch-shift targets that can be computed quickly for a given sample r
169169
</tr>
170170
</tbody>
171171
</table>
172+
172173
#### Return value
173174

174175
<table>
@@ -186,6 +187,88 @@ Search for pitch-shift targets that can be computed quickly for a given sample r
186187
</tbody>
187188
</table>
188189

190+
### `semitones_to_ratio`
191+
Convert semitonal shifts into ratios.
192+
193+
<table>
194+
<thead>
195+
<tr>
196+
<th>Argument</th>
197+
<th>Required</th>
198+
<th>Default Value</th>
199+
<th>Type</th>
200+
<th>Description</th>
201+
</tr>
202+
</thead>
203+
<tbody>
204+
<tr>
205+
<td><code>semitones</code></td>
206+
<td>Yes</td>
207+
<td></td>
208+
<td><code>float</code></td>
209+
<td>The number of semitones for a desired shift.</td>
210+
</tr>
211+
</tbody>
212+
</table>
213+
214+
#### Return value
215+
216+
<table>
217+
<thead>
218+
<tr>
219+
<th>Type</th>
220+
<th>Description</th>
221+
</tr>
222+
</thead>
223+
<tbody>
224+
<tr>
225+
<td><code>Fraction</code></td>
226+
<td>A Fraction indicating a pitch shift ratio</td>
227+
</tr>
228+
</tbody>
229+
</table>
230+
231+
### `ratio_to_semitones`
232+
Convert rational shifts to semitones.
233+
234+
<table>
235+
<thead>
236+
<tr>
237+
<th>Argument</th>
238+
<th>Required</th>
239+
<th>Default Value</th>
240+
<th>Type</th>
241+
<th>Description</th>
242+
</tr>
243+
</thead>
244+
<tbody>
245+
<tr>
246+
<td><code>ratio</code></td>
247+
<td>Yes</td>
248+
<td></td>
249+
<td><code>Fraction</code></td>
250+
<td>The ratio for a desired shift.</td>
251+
</tr>
252+
</tbody>
253+
</table>
254+
255+
#### Return value
256+
257+
<table>
258+
<thead>
259+
<tr>
260+
<th>Type</th>
261+
<th>Description</th>
262+
</tr>
263+
</thead>
264+
<tbody>
265+
<tr>
266+
<td><code>float</code></td>
267+
<td>The magnitude of a pitch shift in semitones</td>
268+
</tr>
269+
</tbody>
270+
</table>
271+
189272
## Example
190273

191274
See [example.py](https://github.com/KentoNishi/torch-pitch-shift/blob/master/example.py) to see an example of `torch-pitch-shift` in action!

torch_pitch_shift/main.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
from fractions import Fraction
33
from functools import reduce
44
from itertools import chain, count, islice, repeat
5-
from typing import Union, Callable
5+
from typing import Union, Callable, List
66
from torch.nn.functional import pad
77
import torch
88
import torchaudio.transforms as T
99
from primePy import primes
10+
from math import log2
1011
import warnings
1112

1213
warnings.simplefilter("ignore")
@@ -35,7 +36,7 @@ def _combinations_without_repetition(r, iterable=None, values=None, counts=None)
3536

3637
def get_fast_shifts(
3738
sample_rate: int, condition: Callable = lambda x: x >= 0.5 and x <= 2 and x != 1
38-
):
39+
) -> List[Fraction]:
3940
"""
4041
Search for pitch-shift targets that can be computed quickly for a given sample rate.
4142
@@ -46,6 +47,11 @@ def get_fast_shifts(
4647
condition: Callable [optional]
4748
A function to validate fast shift ratios.
4849
Default is `lambda x: x >= 0.5 and x <= 2 and x != 1` (between -1 and +1 octaves).
50+
51+
Returns
52+
-------
53+
output: List[Fraction]
54+
A list of fast pitch-shift target ratios
4955
"""
5056
fast_shifts = set()
5157
factors = primes.factors(sample_rate)
@@ -65,6 +71,40 @@ def get_fast_shifts(
6571
return list(fast_shifts)
6672

6773

74+
def semitones_to_ratio(semitones: float) -> Fraction:
75+
"""
76+
Convert semitonal shifts into ratios.
77+
78+
Parameters
79+
----------
80+
semitones: float
81+
The number of semitones for a desired shift.
82+
83+
Returns
84+
-------
85+
output: Fraction
86+
A Fraction indicating a pitch shift ratio
87+
"""
88+
return Fraction(2.0 ** (semitones / 12.0))
89+
90+
91+
def ratio_to_semitones(ratio: Fraction) -> float:
92+
"""
93+
Convert rational shifts to semitones.
94+
95+
Parameters
96+
----------
97+
ratio: Fraction
98+
The ratio for a desired shift.
99+
100+
Returns
101+
-------
102+
output: float
103+
The magnitude of a pitch shift in semitones
104+
"""
105+
return float(12.0 * log2(ratio))
106+
107+
68108
def pitch_shift(
69109
input: torch.Tensor,
70110
shift: Union[float, Fraction],

0 commit comments

Comments
 (0)