![]() #Simple desktops sine code#I know, I know, I realize that you came here probably wanting to learn about synths, and now maybe flustered seeing the eighty something lines of OOP code I have thrown at you, but just bear with me it gets interesting after the OOP hurdle is crossed. A piano is a keyboarded acoustic instrument. Synthesizers □Ī synthesizer is an electric instrument. Using generators and iterators we can generate infinite streams of integers which can be fed to some output like a speaker or a file, without these features the memory requirements would be very high. I won’t be covering the intricacies of the two cause the article may end up being too long, this Stack Overflow post explains the difference between the two here’s a link that explains generators, and another that explains iterators quite well. #Simple desktops sine generator#Once you have a generator or an iterator a value can be obtained from it by calling the next function on it like so: processed_values_gen = generator_function() next(processed_values_gen) Which is a function that has a yield instead of a return which is what makes it a generator. Which looks like list comprehension but with regular brackets, or a generator function, such as: def generator_function(): for i in range(30): yield some_function(i) A generator can be implemented using a generator expression, such as: processed_values_gen = (some_function(i) for i in range(30)) ![]() Lazy iteration is like normal iteration, but a value is evaluated only when we need it, for example we need a note to be played only when we press a key, so the values of the note can be evaluated when we press the key rather than before it.įor lazy iteration of a sequence we can use two different Python featuresĪll generators are iterators, and an object is an iterator only if it implements the _next_ and the _iter_ functions in it’s class definition. The nuances of this pointing can be handled by libraries. So to generate sound all we need to do is create a stream of sensible integers and point the stream to a speaker or a file. If one is talking about CD Audio (which no one talks about now days), 44,100 numbers each having a value between -32,768 and 32,767 are being fed to the speaker through some circuitry (a DAC) every second which means that CD Audio has a sample rate of 44,100Hz and a bit depth of 16 bits (signed integers). Digital sound is just a sequence of numbers being fed to a speaker from a computer at a certain speed, I’m oversimplifying but that’s basically what happens. Digital Sound □įirst, a note on digital sound because that’s what’s gonna be generated. This is a skippable section that glosses over digital sound, lazy iteration and synths before the main thing: oscillators. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |