Replies: 3 comments 3 replies
-
Hello and welcome! This is actually two questions, in a sense, so I'll try to answer them separately, then suggest some solutions. The first, relating to the playback method: currently Gensound relies on SimpleAudio for playback, which was chosen for being lightweight and cross-platform. I don't imagine Gensound will ever try to handle the hardware aspects of the playback by itself, but the plan is to make it as easy as possible for users to delegate the playback and I/O into whatever other library they may have available, so in theory the user could easily choose to direct the audio to any of simpleaudio, PyGame, etc., and also supply any library-specific arguments on the way (allowing the audio to loop, if the library supports it). However, that may still take a while to implement, and currently it doesn't seem like there's a convenient way to loop directly from simpleaudio (currently the audio plays exactly once, and may be interrupted). Second, I agree that there's a need for an easier interface to get the actual resulting audio so it can be passed elsewhere in the code. In fact, I'm working on that right now, and it will be part of version 0.4 that I hope to release within a week. Although this feature would help, it still wouldn't make it as convenient as you'd hope, but I can offer these options which should be already functional right now:
Example code for the 2nd suggestion: s = Sine("A4", 1e3)
audio = s.realise(sample_rate=44100)
bytes = audio._prepare_buffer(byte_width=2, max_amplitude=0.9) The variables Please let me know if this helps! |
Beta Was this translation helpful? Give feedback.
-
In case you're interested, here's a quick example of using gensound as part of a pygame.mixer.Sound object. Might be fodder for an examples section of your wiki? # An example class to use with the pygame sound system, the mixer.
# When an object of this class is instantiated, it will be able
# to be '.play()'ed, as other example Sounds would be.
import pygame as pg
from gensound import Sine, Sawtooth
from gensound.curve import SineCurve
from gensound.transforms import ADSR, Amplitude
class ExampleGSTone(pg.mixer.Sound):
def __init__(self):
m_freq = pg.mixer.get_init()[0]
# Initial Chord
signal = 0.4*Sawtooth('C4', duration=1e3)
signal += 0.3*Sawtooth('E4', duration=1e3)
signal += 0.3*Sawtooth('G4', duration=1e3)
# Some low Freq Modulation
signal *= Amplitude(SineCurve(frequency=2, depth=0.2, baseline=0.7, duration=1e3))
# An ADSR Envelope.
signal *= ADSR(attack=0.002e3, decay=0.03e3, sustain=0.8, release=0.1e3)
# 'Realise' the signal into an actual audio sample object.
audio = signal.realise(sample_rate=m_freq)
audio._prepare_buffer(byte_width=2, max_amplitude=0.9)
# We can now use the prepared buffer as the input to the
# pygame mixer Sound class.
pg.mixer.Sound.__init__(self, buffer=audio.buffer) |
Beta Was this translation helpful? Give feedback.
-
Hi @wpower12 , In case it's still relevant for you (or for anybody else reading), the new version 0.5 supports playback directly to PyGame. You don't have to do anything - just call Any keyword arguments you add will be carried over to PyGame as well, so you can use the Have fun! |
Beta Was this translation helpful? Give feedback.
-
Hi! Just started using gensound as part of a demo project. I was trying to use it as part of a 'procedurally generated sound effects' system, and was wondering if you could expose more control in the play method. Right now it just loops, I think? and I was hoping to have an interface similar to PyGames mixer, where you can pass a value to play of -1 to loop, or some integer for 'number of plays'.
Right now to get it to work, im trying to pull the actual audio numpy array out of the resulting Signal object, and pass that into mixer, but that's leading to some weird issues I can't figure out. Being able to control all the playback just through gensound would be amazing.
Beta Was this translation helpful? Give feedback.
All reactions