|
| 1 | +# Sound Generator Brick |
| 2 | + |
| 3 | +Sound Generator is a lightweight and expressive audio generation brick that lets you create, manipulate, and play sounds programmatically. |
| 4 | +You can write musical notes, generate tones, and compose melodies — all while shaping the sound through custom waveforms and effects. |
| 5 | + |
| 6 | +Features: |
| 7 | +* *Generate tones and melodies from notes or frequencies. |
| 8 | +* Choose your waveform — sine, square, triangle, sawtooth. |
| 9 | +* Add sound effects such as chorus, overdrive, delay, vibrato, or distortion. |
| 10 | +* Compose procedural music directly from code. |
| 11 | +* Real-time playback over speaker |
| 12 | + |
| 13 | +## Code example and usage |
| 14 | + |
| 15 | +```python |
| 16 | +from arduino.app_bricks.sound_generator import SoundGenerator, SoundEffect |
| 17 | +from arduino.app_utils import App |
| 18 | + |
| 19 | +player = SoundGenerator(sound_effects=[SoundEffect.adsr()]) |
| 20 | + |
| 21 | +fur_elise = [ |
| 22 | + ("E5", 1/4), ("D#5", 1/4), ("E5", 1/4), ("D#5", 1/4), ("E5", 1/4), |
| 23 | + ("B4", 1/4), ("D5", 1/4), ("C5", 1/4), ("A4", 1/2), |
| 24 | + |
| 25 | + ("C4", 1/4), ("E4", 1/4), ("A4", 1/4), ("B4", 1/2), |
| 26 | + ("E4", 1/4), ("G#4", 1/4), ("B4", 1/4), ("C5", 1/2), |
| 27 | + |
| 28 | + ("E4", 1/4), ("E5", 1/4), ("D#5", 1/4), ("E5", 1/4), ("D#5", 1/4), ("E5", 1/4), |
| 29 | + ("B4", 1/4), ("D5", 1/4), ("C5", 1/4), ("A4", 1/2), |
| 30 | + |
| 31 | + ("C4", 1/4), ("E4", 1/4), ("A4", 1/4), ("B4", 1/2), |
| 32 | + ("E4", 1/4), ("C5", 1/4), ("B4", 1/4), ("A4", 1.0), |
| 33 | +] |
| 34 | +for note, duration in fur_elise: |
| 35 | + player.play(note, duration) |
| 36 | + |
| 37 | +App.run() |
| 38 | +``` |
| 39 | + |
| 40 | +waveform can be customized to change effect. For example, for a retro-gaming sound, you can configure "square" wave form. |
| 41 | + |
| 42 | +```python |
| 43 | +player = SoundGenerator(wave_form="square") |
| 44 | +``` |
| 45 | + |
| 46 | +instead, to have a more "rock" like sound, you can add effects like: |
| 47 | + |
| 48 | +```python |
| 49 | +player = SoundGenerator(sound_effects=[SoundEffect.adsr(), SoundEffect.overdrive(drive=180.0), SoundEffect.chorus(depth_ms=15, rate_hz=0.2, mix=0.4)]) |
| 50 | +``` |
0 commit comments