pyquist 0.2.0__tar.gz → 0.2.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. {pyquist-0.2.0/pyquist.egg-info → pyquist-0.2.2}/PKG-INFO +1 -1
  2. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist/__init__.py +1 -1
  3. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist/audio.py +33 -0
  4. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist/audio_test.py +60 -2
  5. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist/cli.py +4 -0
  6. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist/device.py +8 -2
  7. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist/device_test.py +16 -1
  8. {pyquist-0.2.0 → pyquist-0.2.2/pyquist.egg-info}/PKG-INFO +1 -1
  9. {pyquist-0.2.0 → pyquist-0.2.2}/LICENSE +0 -0
  10. {pyquist-0.2.0 → pyquist-0.2.2}/README.md +0 -0
  11. {pyquist-0.2.0 → pyquist-0.2.2}/pyproject.toml +0 -0
  12. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist/helper.py +0 -0
  13. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist/helper_test.py +0 -0
  14. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist/paths.py +0 -0
  15. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist/plot.py +0 -0
  16. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist/plot_test.py +0 -0
  17. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist/py.typed +0 -0
  18. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist/score.py +0 -0
  19. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist/score_test.py +0 -0
  20. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist/test_data/232009__danmitch3ll__xylophone-sweep.wav +0 -0
  21. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist/test_data/388954__fullmetaljedi__blues-riff-in-g-nylon.wav +0 -0
  22. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist/test_data/434013__mrpearch__drum-patern.mp3 +0 -0
  23. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist/test_data/ATTRIBUTION.md +0 -0
  24. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist/test_data/ravel_bolero.mid +0 -0
  25. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist/web/__init__.py +0 -0
  26. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist/web/freesound.py +0 -0
  27. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist/web/theorytab.py +0 -0
  28. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist.egg-info/SOURCES.txt +0 -0
  29. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist.egg-info/dependency_links.txt +0 -0
  30. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist.egg-info/entry_points.txt +0 -0
  31. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist.egg-info/requires.txt +0 -0
  32. {pyquist-0.2.0 → pyquist-0.2.2}/pyquist.egg-info/top_level.txt +0 -0
  33. {pyquist-0.2.0 → pyquist-0.2.2}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyquist
3
- Version: 0.2.0
3
+ Version: 0.2.2
4
4
  Summary: Barebones utilities for low-level computer music programming in Python and NumPy. Teaching library for CMU 15-322.
5
5
  Author: Chris Donahue
6
6
  License-Expression: MIT
@@ -1,4 +1,4 @@
1
- __version__ = "0.2.0"
1
+ __version__ = "0.2.2"
2
2
 
3
3
  from .audio import Audio
4
4
  from .device import (
@@ -386,6 +386,39 @@ class Audio:
386
386
  f"Cannot convert audio with {self.num_channels} channels to stereo."
387
387
  )
388
388
 
389
+ def pan(self, position: float = 0.0, *, in_place: bool = True) -> "Audio":
390
+ """Pans stereo audio to ``position`` using an equal-power pan law.
391
+
392
+ Channel gains are ``cos(theta)`` and ``sin(theta)`` for
393
+ ``theta = (position + 1) * pi / 4``, so the total power is the same at
394
+ every position. Mono audio must be widened first, e.g.
395
+ ``audio.as_stereo().pan(1.0)``.
396
+
397
+ Args:
398
+ position: Stereo position in ``[-1.0, 1.0]``. ``-1.0`` is hard
399
+ left, ``0.0`` (default) is center, ``1.0`` is hard right.
400
+ in_place: If ``True`` (default), modifies and returns ``self``.
401
+ If ``False``, returns a new ``Audio`` and leaves the original
402
+ untouched.
403
+
404
+ Raises:
405
+ ValueError: If ``position`` is outside ``[-1.0, 1.0]``, or if the
406
+ audio is not stereo.
407
+ """
408
+ position = float(position)
409
+ if not -1.0 <= position <= 1.0:
410
+ raise ValueError("position must be in [-1.0, 1.0].")
411
+ if self.num_channels != 2:
412
+ raise ValueError(
413
+ f"Cannot pan audio with {self.num_channels} channels (stereo only)."
414
+ )
415
+ theta = (position + 1.0) * (np.pi / 4.0)
416
+ gains = np.array([np.cos(theta), np.sin(theta)], dtype=np.float32)
417
+ if in_place:
418
+ self._samples *= gains
419
+ return self
420
+ return Audio(self._samples * gains, sample_rate=self._sample_rate)
421
+
389
422
  def resample(self, new_sample_rate: int, **kwargs) -> "Audio":
390
423
  """Returns a new ``Audio`` resampled to ``new_sample_rate``.
391
424
 
@@ -101,11 +101,17 @@ class TestAudio(unittest.TestCase):
101
101
  Audio.concatenate([])
102
102
  with self.assertRaises(ValueError):
103
103
  Audio.concatenate(
104
- [Audio(np.ones(3), sample_rate=44100), Audio(np.ones(3), sample_rate=22050)]
104
+ [
105
+ Audio(np.ones(3), sample_rate=44100),
106
+ Audio(np.ones(3), sample_rate=22050),
107
+ ]
105
108
  )
106
109
  with self.assertRaises(ValueError):
107
110
  Audio.concatenate(
108
- [Audio(np.ones((3, 1)), sample_rate=44100), Audio(np.ones((3, 2)), sample_rate=44100)]
111
+ [
112
+ Audio(np.ones((3, 1)), sample_rate=44100),
113
+ Audio(np.ones((3, 2)), sample_rate=44100),
114
+ ]
109
115
  )
110
116
 
111
117
  def test_from_file_wav(self):
@@ -242,6 +248,58 @@ class TestAudio(unittest.TestCase):
242
248
  with self.assertRaises(ValueError):
243
249
  multi4.as_stereo()
244
250
 
251
+ def test_pan(self):
252
+ stereo = Audio(np.ones((10, 2), dtype=np.float32), sample_rate=44100)
253
+
254
+ # Center: equal gain of -3 dB in both channels.
255
+ centered = stereo.pan(in_place=False)
256
+ self.assertEqual(centered.shape, (10, 2))
257
+ self.assertEqual(centered.samples.dtype, np.float32)
258
+ self.assertEqual(centered.sample_rate, 44100)
259
+ self.assertTrue(np.allclose(centered.samples, np.sqrt(0.5), atol=1e-6))
260
+
261
+ # Hard left/right mute the opposite channel and pass the other through.
262
+ left = stereo.pan(-1.0, in_place=False)
263
+ self.assertTrue(np.allclose(left.samples[:, 0], 1.0, atol=1e-6))
264
+ self.assertTrue(np.allclose(left.samples[:, 1], 0.0, atol=1e-6))
265
+ right = stereo.pan(1.0, in_place=False)
266
+ self.assertTrue(np.allclose(right.samples[:, 0], 0.0, atol=1e-6))
267
+ self.assertTrue(np.allclose(right.samples[:, 1], 1.0, atol=1e-6))
268
+
269
+ # Equal power: the summed power is constant across positions.
270
+ for position in [-1.0, -0.5, -0.25, 0.0, 0.25, 0.5, 1.0]:
271
+ panned = stereo.pan(position, in_place=False)
272
+ self.assertAlmostEqual(
273
+ float((panned.samples**2).sum()),
274
+ float((stereo.samples**2).sum()) / 2,
275
+ places=5,
276
+ )
277
+
278
+ # in_place=False above didn't mutate the source; in_place=True does.
279
+ self.assertTrue(np.allclose(stereo.samples, 1.0))
280
+ self.assertIs(stereo.pan(1.0), stereo)
281
+ self.assertTrue(np.allclose(stereo.samples[:, 0], 0.0, atol=1e-6))
282
+ self.assertTrue(np.allclose(stereo.samples[:, 1], 1.0, atol=1e-6))
283
+
284
+ # Mono is widened first: as_stereo().pan() is a full-amplitude hard pan.
285
+ mono = Audio(np.ones((10, 1), dtype=np.float32), sample_rate=44100)
286
+ panned = mono.as_stereo().pan(1.0)
287
+ self.assertTrue(np.allclose(panned.samples[:, 0], 0.0, atol=1e-6))
288
+ self.assertTrue(np.allclose(panned.samples[:, 1], 1.0, atol=1e-6))
289
+
290
+ # Out-of-range positions raise.
291
+ with self.assertRaises(ValueError):
292
+ stereo.pan(-1.5)
293
+ with self.assertRaises(ValueError):
294
+ stereo.pan(1.5)
295
+
296
+ # Non-stereo: raises
297
+ with self.assertRaises(ValueError):
298
+ mono.pan()
299
+ multi = Audio(np.zeros((10, 3), dtype=np.float32), sample_rate=44100)
300
+ with self.assertRaises(ValueError):
301
+ multi.pan()
302
+
245
303
  def test_segment(self):
246
304
  sr = 1000
247
305
  # 1s = 1000 samples; values 0..999 in channel 0
@@ -7,6 +7,7 @@ and audio loading lives in :mod:`pyquist.audio` / :mod:`pyquist.web`.
7
7
 
8
8
  import argparse
9
9
 
10
+ from . import __version__
10
11
  from .audio import Audio
11
12
  from .device import (
12
13
  play,
@@ -52,6 +53,9 @@ def _build_parser() -> argparse.ArgumentParser:
52
53
  parser = argparse.ArgumentParser(
53
54
  prog="pyquist", description="Pyquist command-line interface."
54
55
  )
56
+ parser.add_argument(
57
+ "--version", "-v", action="version", version=f"pyquist {__version__}"
58
+ )
55
59
  subparsers = parser.add_subparsers(dest="command", required=True)
56
60
 
57
61
  p_devices = subparsers.add_parser(
@@ -10,9 +10,9 @@ import sys
10
10
  import time
11
11
  from typing import Any, Optional, Tuple, Union
12
12
 
13
+ import numpy as np
13
14
  import sounddevice as sd
14
15
  import tqdm
15
- import numpy as np
16
16
 
17
17
  from .audio import Audio
18
18
  from .paths import CACHE_DIR
@@ -131,7 +131,13 @@ def play(
131
131
  when called from a notebook. Useful when you want the audio
132
132
  played through the OS audio output rather than as an inline
133
133
  player widget.
134
+
135
+ Raises:
136
+ ValueError: If ``audio.sample_rate`` is ``None``.
134
137
  """
138
+ if audio.sample_rate is None:
139
+ raise ValueError("sample_rate is None; cannot play audio.")
140
+
135
141
  if normalize:
136
142
  audio = audio.normalize(in_place=False)
137
143
  audio = audio.clip(in_place=False)
@@ -173,7 +179,7 @@ def record(
173
179
  # :mod:`sounddevice` is not available when running in browser, handle separately.
174
180
  if _in_browser():
175
181
  return _record_in_browser(duration)
176
-
182
+
177
183
  device_info = sd.query_devices(sd.default.device[0])
178
184
  sample_rate = round(device_info["default_samplerate"])
179
185
  num_channels = device_info["max_input_channels"]
@@ -3,10 +3,11 @@ import io
3
3
  import json
4
4
  import tempfile
5
5
  import unittest
6
- import numpy as np
7
6
  from pathlib import Path
8
7
  from unittest import mock
9
8
 
9
+ import numpy as np
10
+
10
11
  from . import device
11
12
 
12
13
 
@@ -446,6 +447,20 @@ class TestPlayDispatch(unittest.TestCase):
446
447
  fake_audio_widget.assert_called_once()
447
448
  fake_display.assert_called_once()
448
449
 
450
+ def test_missing_sample_rate_raises(self):
451
+ import numpy as np
452
+
453
+ from .audio import Audio
454
+
455
+ audio = Audio(np.zeros(100, dtype=np.float32)) # no sample_rate
456
+ with (
457
+ mock.patch.object(device, "_in_ipython_notebook", return_value=False),
458
+ mock.patch.object(device.sd, "play") as mock_play,
459
+ ):
460
+ with self.assertRaises(ValueError):
461
+ device.play(audio)
462
+ mock_play.assert_not_called()
463
+
449
464
  def test_force_sounddevice_overrides_notebook(self):
450
465
  audio = self._make_audio()
451
466
  with (
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyquist
3
- Version: 0.2.0
3
+ Version: 0.2.2
4
4
  Summary: Barebones utilities for low-level computer music programming in Python and NumPy. Teaching library for CMU 15-322.
5
5
  Author: Chris Donahue
6
6
  License-Expression: MIT
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes