Python Forum
Generate simple melodic line from code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Generate simple melodic line from code (/thread-23595.html)



Generate simple melodic line from code - QuarterMissiv - Jan-07-2020

Could anyone recommend where I could learn how to code and play a sound in Python.

For example, a variable holds a number. When run, that number is passed to an audio generator that plays a sound using the variable's value for pitch and then the code finishes. Maybe two variables could be used, one for pitch and another for duration.

Another example. There is an array of 10 integers. A loop moves through the array and the values are sent to the audio generator at each iteration of the loop. The code then finishes at the completion of the loop.

Maybe a clearer way to explain what I am thinking is to take a MIDI file as the input and pass its data to a player object that converts the MIDI data into pitch and duration.

But rather than using the data from the MIDI file, I want to use arrays and iteration in Python code as data to pass to the player object.


RE: Generate simple melodic line from code - Clunk_Head - Jan-07-2020

1 minute on Google turned this up.


RE: Generate simple melodic line from code - Axel_Erfurt - Jan-07-2020

https://github.com/bspaans/python-mingus


RE: Generate simple melodic line from code - QuarterMissiv - Jan-07-2020

Thank you for the link, Clunk_Head.

The majority of the article deals with creating sine waves and writing them to a wav file. The last section contains a melody section, but from what I can tell, that is written as a wav and not played as the code executes.

Is there a way in Python that I can do something like:

arrayPitch = [1, 5, 3, 7, 5, 8, 5]
arrayDuration = [1, 2, 1, 4, 1, 2, 2]

for x in arrayPitch:
playToComputerSpeakers(arrayPitches, arrayDuration)

Something like that without needing to worry about generating the sound itself, just the pitch and duration.

(Jan-07-2020, 09:38 PM)Axel_Erfurt Wrote: https://github.com/bspaans/python-mingus

Thanks, that's looking more like it.

I can't work out if Foxdot and Supercollider can be used in the way that I've described. Does anyone know?


RE: Generate simple melodic line from code - buran - Jan-08-2020

Given the very general nature of the question (there are plenty of options available):
https://www.google.com/search?q=generate+and+play+audio+python


RE: Generate simple melodic line from code - QuarterMissiv - Jan-08-2020

(Jan-08-2020, 07:08 AM)buran Wrote: Given the very general nature of the question (there are plenty of options available):
https://www.google.com/search?q=generate+and+play+audio+python

Thank you for the Google search criteria. I am afraid it has not helped. I apologise if I am missing something fundamental.

Let me try and be as specific as I can.

I do not want to generate the audio signal, or use audio files (wav, mp3 etc.)

I would like to send data to an pre-made audio module that will play the sound over my speakers. I write the code that contains, say, an array, which contains the data (pitch, duration etc.), then run the code, the code executes and the data is read by the audio module and those pitches and durations are played on my speakers. The code then stops executing.


RE: Generate simple melodic line from code - jefsummers - Jan-08-2020

Are you opposed to using midi? The midi spec does what you want, for timing you can use a sleep (for monophonic) or other timers to wait for turning the notes off. Midi message to turn a note on is [0x91,60,127] for middle C (note on for channel 1, middle C=60, 127 is velocity to hit the note as if playing on a keyboard, range 0-127

Check py-midi from pypi if MIDI is an acceptable route.


RE: Generate simple melodic line from code - QuarterMissiv - Jan-09-2020

Thanks, I will investigate py-midi.