Python Forum
Generate simple melodic line from code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generate simple melodic line from code
#1
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.
Reply
#2
1 minute on Google turned this up.
Reply
#3
https://github.com/bspaans/python-mingus
Reply
#4
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?
Reply
#5
Given the very general nature of the question (there are plenty of options available):
https://www.google.com/search?q=generate...dio+python
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(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...dio+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.
Reply
#7
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.
Reply
#8
Thanks, I will investigate py-midi.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with simple code JacobSkinner 1 309 Mar-18-2024, 08:08 PM
Last Post: deanhystad
  Unable to understand the meaning of the line of code. jahuja73 0 299 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 476 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 1,652 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  help me simple code result min and max number abrahimusmaximus 2 901 Nov-12-2022, 07:52 AM
Last Post: buran
  Simple encoding code ebolisa 3 1,436 Jun-18-2022, 10:59 AM
Last Post: deanhystad
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 1,786 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  Python code to read second line from CSV files and create a master CSV file sh1704 1 2,394 Feb-13-2022, 07:13 PM
Last Post: menator01
  Simple code question about lambda and tuples JasPyt 7 3,294 Oct-04-2021, 05:18 PM
Last Post: snippsat
  My simple code don't works !! Nabi666 1 1,601 Sep-06-2021, 12:10 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020