Python Forum
How can I make music with python/pygame?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I make music with python/pygame?
#9
(Sep-24-2019, 03:25 PM)xBlackHeartx Wrote: This pysynth looks interesting, but which file am I supposed to download? The only module I've ever installed is pygame, and with that I had to enter a command in my computer's command prompt.
install the same way
pip install pysynth
(Sep-24-2019, 03:25 PM)xBlackHeartx Wrote: As for game files, honestly, I prefer everything to be contained within the code of the game.
(Sep-24-2019, 03:25 PM)xBlackHeartx Wrote: I would also prefer to be able to synthesize music from within my programs themselves.
I would much prefer to use GIMP to draw images at a faster rate than coding them and load the images. Same with music and sounds. That is about what most people do that i know of. 99 out of 100 people do it this way in a game. The only time i have seen people do it the other way was software, not a game. And even then it has a save function to write it to disk. I have also found out that when passing programs to the public requires building exe's. And the more things you import the more problems that occur in building exe's. So i try to reduce the number of 3rd party libraries as uch as possible. One of those is just including files with the program such as images, sounds, and music.

But i am sure what you want is doable. You just need to find the right software to do what you want. Im sure there is a 3rd party lib somewhere in pypi. There has to have been someone who made a package that wants to do similar to what you are doing. Got this off another Google search for python synthesizer

A video using Numpy for a synthesizer. His first video in the series create a wav files, but maybe he does not later in the series.
https://www.youtube.com/watch?v=6nzN7xnDSLU
https://github.com/akey7/numpy-audio-synth

You can use NumPy and sounddevice 3rd party library installed the same way with pip.
import numpy as np # download
import sounddevice as sd # download
import time

stream = []

# Main Controls
sps = 44100 # DON'T CHANGE

carrier_hz = 440.0

duration_s = 1.0

atten = 0.3

def amplitudeMod(t_samples, carrier):
    # Modulate the amplitude of the carrier
    ac = 0.2 # amplitude 0 = min, 1 = max
    ka = 1.0 # range of change 0.1 = less, 1.0 = most
    modulator_hz = 0.0 # frequency of modulation 20hz max
    modulator = np.sin(2 * np.pi * modulator_hz * t_samples / sps)
    envelope = ac * (1.0 + ka * modulator)
    return carrier * envelope

def frequencyMod(t_samples, sps):
    # Modulate the frequency of the carrier
    k = 50.0 # range of change 0.1 = less, ?? = most
    modulator_hz = 10.0 # frequency of modulation
    carrier2 = 2 * np.pi * t_samples * carrier_hz / sps
    modulator = k * np.sin(2 * np.pi * t_samples * modulator_hz / sps)
    return np.cos(carrier2 + modulator)

# Create carrier wave
t_samples = np.arange(duration_s * sps)
carrier = np.sin(2 * np.pi * carrier_hz * t_samples / sps)

choice = input("1) Play sine\n2) Play amplitude modulation\n3) Play frequency modulation\n;")
if choice == "1":
    output = carrier
if choice == "2":
    output = amplitudeMod(t_samples, carrier)
if choice == "3":
    output = frequencyMod(t_samples, sps)

# Output

output *= atten

sd.play(output, sps)
sd.wait()
sd.stop()
Another option....
https://ipython-books.github.io/117-crea...-notebook/

another option....
https://github.com/nwhitehead/pyfluidsynth
Recommended Tutorials:
Reply


Messages In This Thread
RE: How can I make music with python/pygame? - by metulburr - Sep-24-2019, 06:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to make enemy chase me in pygame? Jan_97 3 4,607 Nov-23-2021, 05:08 PM
Last Post: Jan_97
  How to make an image move in relation to another in PYGAME CompleteNewb 1 2,371 Nov-10-2021, 03:38 PM
Last Post: metulburr
  [PyGame] How to loop music with pygame finndude 2 4,086 Nov-09-2021, 10:54 AM
Last Post: metulburr
  cant make a door automatically close a few seconds after i open it in pygame cooImanreebro 2 2,306 Dec-15-2020, 08:40 PM
Last Post: michael1789
  Trying to make boundries for the pygame bluewing101 2 2,103 Mar-23-2020, 01:35 AM
Last Post: Windspar
  Problem with music - Pygame.error GaseBall 1 3,265 Nov-28-2019, 07:46 PM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

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