This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
167 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[bumpversion] | ||
current_version = 0.2.1 | ||
current_version = 0.2.2 | ||
|
||
[bumpversion:file:waves/__init__.py] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ | |
:caption: Reference | ||
|
||
ref/sound | ||
ref/sound.generator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
sound.generator | ||
=============== | ||
|
||
.. autofunction:: waves.sound.generator.mono_ttf_gen | ||
.. autofunction:: waves.sound.generator.stereo_ttf_gen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"""Mono sound wave generators tests.""" | ||
|
||
import pytest | ||
|
||
from waves import Sound | ||
|
||
|
||
@pytest.mark.parametrize("fps", (44100, 22050), ids=("fps=44100", "22050")) | ||
@pytest.mark.parametrize( | ||
"frequency", | ||
(110, 220, 440), | ||
ids=("frequency=110", "frequency=220", "frequency=440"), | ||
) | ||
@pytest.mark.parametrize( | ||
"volume", | ||
(0, 0.5, 1), | ||
ids=("volume=0", "volume=0.5", "volume=1"), | ||
) | ||
def test_mono_ttf_gen(mono_ttf_gen, fps, frequency, volume): | ||
time_to_frame = mono_ttf_gen(fps=fps, frequency=frequency, volume=volume) | ||
|
||
sound = Sound.from_datatimes(time_to_frame, fps=fps).with_duration(1) | ||
|
||
assert sound.fps == fps | ||
assert sound.n_channels == 1 | ||
assert sound.duration == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"""Stereo sound wave generators tests.""" | ||
|
||
import pytest | ||
|
||
from waves import Sound | ||
|
||
|
||
@pytest.mark.parametrize("fps", (44100, 22050), ids=("fps=44100", "22050")) | ||
@pytest.mark.parametrize( | ||
"frequencies", | ||
((110, 220), (440, 880), (120, 360, 720)), | ||
ids=("frequency=110", "frequency=220", "frequency=440"), | ||
) | ||
@pytest.mark.parametrize( | ||
"volume", | ||
(0, 0.5, 1), | ||
ids=("volume=0", "volume=0.5", "volume=1"), | ||
) | ||
def test_stereo_ttf_gen(stereo_ttf_gen, fps, frequencies, volume): | ||
time_to_frame = stereo_ttf_gen(fps=fps, frequencies=frequencies, volume=volume) | ||
|
||
sound = Sound.from_datatimes(time_to_frame, fps=fps).with_duration(1) | ||
|
||
assert sound.fps == fps | ||
assert sound.n_channels == len(frequencies) | ||
assert sound.duration == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
"""Breaks code base.""" | ||
|
||
from waves.sound.generator import mono_ttf_gen, stereo_ttf_gen | ||
from waves.sound.main import Sound | ||
|
||
|
||
__all__ = ("Sound",) | ||
__all__ = ("Sound", "mono_ttf_gen", "stereo_ttf_gen") | ||
|
||
__title__ = "waves" | ||
__version__ = "0.2.1" | ||
__version__ = "0.2.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import functools | ||
|
||
import numpy as np | ||
|
||
|
||
@functools.lru_cache(maxsize=None) | ||
def mono_ttf_gen(fps=44100, frequency=110, volume=0.5): | ||
"""Generates a ``time_to_frame`` function for a mono sine wave which can be | ||
passed to ``Sound.from_datatimes``. | ||
Parameters | ||
---------- | ||
fps : int, optional | ||
Frames per second of the sound to generate. | ||
frequency : int, optional | ||
Frequency of the sine wave. | ||
volume : float, optional | ||
Value between 0 and 1 that will define the volume of the wave. | ||
Returns | ||
------- | ||
function: Takes a parameter time ``t`` and returns the sound data for that | ||
time. | ||
Examples | ||
-------- | ||
>>> from waves import Sound, mono_ttf_gen | ||
>>> | ||
>>> time_to_frame = mono_ttf_gen(frequency=660, volume=0.2) | ||
>>> sound = Sound.from_datatimes(time_to_frame).with_duration(2) | ||
""" | ||
amplitude = np.iinfo(np.int16).max * volume | ||
|
||
def time_to_frame(t): | ||
return (np.sin(frequency * 2 * np.pi * t) * amplitude).astype(np.int16) | ||
|
||
return time_to_frame | ||
|
||
|
||
@functools.lru_cache(maxsize=None) | ||
def stereo_ttf_gen(fps=44100, frequencies=(440, 110), volume=0.5): | ||
"""Generates a ``time_to_frame`` function for a stereo sine wave which can be | ||
passed to ``Sound.from_datatimes``. | ||
Parameters | ||
---------- | ||
fps : int, optional | ||
Frames per second of the sound to generate. | ||
frequencies : tuple or list, optional | ||
Frequencies of each channel for the sine wave. The number of channels | ||
will be determined by the number of values introduced in this parameter. | ||
volume : float, optional | ||
Value between 0 and 1 that will define the volume of the wave. | ||
Returns | ||
------- | ||
function: Takes a parameter time ``t`` and returns the sound data for that | ||
time for each channel of the sound. | ||
Examples | ||
-------- | ||
>>> from waves import Sound, stereo_ttf_gen | ||
>>> | ||
>>> time_to_frame = stereo_ttf_gen(frequencies=(660, 290), volume=0.2) | ||
>>> sound = Sound.from_datatimes(time_to_frame).with_duration(2) | ||
""" | ||
amplitude = np.iinfo(np.int16).max * volume | ||
|
||
def time_to_frame(t): | ||
return [np.sin(freq * 2 * np.pi * t) * amplitude for freq in frequencies] | ||
|
||
return time_to_frame |