Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How can I make music with python/pygame?
#1
I'm almost done with my degree, and now I'm teaching myself pygame. I was amazed when I discovered the pygame.image module which allows one to load and save images. I figured I could make my own pixel-art program with it for my games. I was planning on making such a program, but realized that I would need to know a lot more about pygame to make it. I ended up settling instead for a function that can draw sprites from a list I input. I was able to make my program draw one of the aliens from space invaders, without using loose files or the .blit command. However, pygame apparently doesn't come with such diverse tools for sound manipulation. Literally all I can do is load and play images. I don't understand the point in having such a thing when you can't make your own sound files with pygame. You can easily make an art program with pygame, but not a music editor.

I've tried to look for free music editors online, but they all look like they either require a degree in acoustics, or knowledge on how sound files from the 8-bit era worked. The only music-making programs I have had any experience with are simtunes and some music maker program I think was made by mtv. Both of these just had you placing colored blocks in a row to generate music. I can't seem to find a program that works the same way, and can save the files in a format pygame will accept. I would just make my own, but pygame doesn't give me the tools for doing that. And I'm tired of all my programs being completely silent. I can't even so much as make a program play an error sound effect. I figure I could get something similar if I had a library of sound effects to choose from, then I could write a function that could play these sound effects based on a custom list (like how my sprite function draws sprites from data in a list). But I have no clue where I could get such files, and frankly, I'd rather be able to generate these files myself.

To be more specific, I'd prefer to use chiptune music. Aka, music like computers and consoles from the 80s and early 90s could produce. I don't even know where I could find a program that do music like that, plus with a simple interface I could use despite my lack of knowledge on acoustics or old-school sound files, and can save the files in .ogg or .wav format.

Any help would be greatly appreciated.

Oh, and I've already tried winsound. That apparently isn't very good for making music though. When I tried to use it, it kept putting a full second pause between each note that I can't remove for some reason.
Quote
#2
(Sep-23-2019, 05:20 PM)xBlackHeartx Wrote: And I'm tired of all my programs being completely silent. I can't even so much as make a program play an error sound effect. I figure I could get something similar if I had a library of sound effects to choose from, then I could write a function that could play these sound effects based on a custom list. But I have no clue where I could get such files
I use to use pygame quite often...but i always used pre-existing free sound effects and music. You should be able to find something relevant with enough research. We have a list of resources for gaming. I have also grabbed resources from others' games on pyweek.

(Sep-23-2019, 05:20 PM)xBlackHeartx Wrote: The only music-making programs I have had any experience with are simtunes and some music maker program I think was made by mtv. Both of these just had you placing colored blocks in a row to generate music. I can't seem to find a program that works the same way, and can save the files in a format pygame will accept.
Why can't you develop music/sound with these programs and output them in pygame?
Quote
#3
I don't own either anymore for one. Besides, the simtunes one wasn't able to actually create sound files, you could only save and play your creations within the program itself (it was actually more of a hybrid between an art program and a music maker). As for that mtv music maker, I don't even know if it could create files. All the music I made with it I only ran from within the program itself. Mostly though, I just listened to the sample songs it came with.
Quote
#4
I dont create sound effects or music. So i dont really know of what is out there availability-wise. I have used Audacity before to clip sound effects for my games. But that is about hte extent i went.

Im sure there is a free version of any paid for software (there always is). It might just not be as well advertised because it is free and thus has no money ot pay for advertisements.

Here is a list i got from Google. It states "linux" because linux often has free open-sourced clones of paid for software. I would be very surprised if one of these does not create music or sound effects, or at least modify sound effects to make them what you want.

You may just have to bite the bullet and learn an extensive program to make sound effects/music. When i started making games i didnt want to use PhotoShop because i dont want to pay for it. So i used GIMP. But that was a learning curve. Its just one of those things you have to learn to get your project done.
Quote
#5
Is there just some other language I could use that would be better for this? I've seen guides on youtube that tell you how to make equations that will generate wave patterns, but they never say how on earth you convert those into sound.

My professor has actually told me that I would probably be happier with c++, c#, or java. Could I use code from those languages to make a custom python module that will let me do what I want? I think the pygame module itself is partially written in c++. And I do have some formal education in both c++ and java (and I mean java, NOT javascript). I never liked c++ because it was a bit too low-level for me (even if python is a bit higher level than I would normally like), and java needs an external program to run, and seeing what's happened to flash, I'd rather avoid languages like that.

Looking through soundjay's library, there does appear to a few things here and there I could use, but the fact that they claim copyright over the sound effects bothers me. If I use those, it means I will never be able to share my creations online. I'd rather be able to completely develop my own sound effects.
Quote
#6
This is a good starting point:
Python In Music Wiki.

As always Google is your friend.
There are also several youtube videos in the results...
metulburr likes this post
Quote
#7
Python and Pygame are both written in C, not C++. You can make C extensions, but there is probably no point unless you find a library in C or want to speed up python (which is not the issue).

Did you check and use some of the software i linked to? I would bet that at least one of those creates music files in which pygame could play. You dont always have to make it 100% in python. For example when i make a game i use Tiled to create the map layout files. These files are interpreted by pyTMX and drawn to the screen by pygame. I could do the same with pygame alone, but is much faster to do with Tiled.

In your situation, there is no option to create music in Pygame (as far as i am aware of). So you must use another software to create your files.

Googling around more i found this which has a lot of links to things to try. Things like pyo or PySyth look promising. But again if your making a game, you dont really need all this, you only need the final output file to play with pygame. I would not worry yet about whether pygame can play it or not, i would first worry about creating the files. You can always convert them to another format later. You can use whatever advanced software there is to produce the file giving you all the amenities of making music.

I checked out pysynth as i thought it was kinda neat.
>>> import pysynth as ps
Piano key frequencies (for equal temperament):
Key number	Scientific name	Frequency (Hz)
         1	             A0	         27.50
         2	            A#0	         29.14
         3	             B0	         30.87
         4	             C1	         32.70
         5	            C#1	         34.65
         6	             D1	         36.71
         7	            D#1	         38.89
         8	             E1	         41.20
         9	             F1	         43.65
        10	            F#1	         46.25
        11	             G1	         49.00
        12	            G#1	         51.91
        13	             A1	         55.00
        14	            A#1	         58.27
        15	             B1	         61.74
        16	             C2	         65.41
        17	            C#2	         69.30
        18	             D2	         73.42
        19	            D#2	         77.78
        20	             E2	         82.41
        21	             F2	         87.31
        22	            F#2	         92.50
        23	             G2	         98.00
        24	            G#2	        103.83
        25	             A2	        110.00
        26	            A#2	        116.54
        27	             B2	        123.47
        28	             C3	        130.81
        29	            C#3	        138.59
        30	             D3	        146.83
        31	            D#3	        155.56
        32	             E3	        164.81
        33	             F3	        174.61
        34	            F#3	        185.00
        35	             G3	        196.00
        36	            G#3	        207.65
        37	             A3	        220.00
        38	            A#3	        233.08
        39	             B3	        246.94
        40	             C4	        261.63
        41	            C#4	        277.18
        42	             D4	        293.66
        43	            D#4	        311.13
        44	             E4	        329.63
        45	             F4	        349.23
        46	            F#4	        369.99
        47	             G4	        392.00
        48	            G#4	        415.30
        49	             A4	        440.00
        50	            A#4	        466.16
        51	             B4	        493.88
        52	             C5	        523.25
        53	            C#5	        554.37
        54	             D5	        587.33
        55	            D#5	        622.25
        56	             E5	        659.26
        57	             F5	        698.46
        58	            F#5	        739.99
        59	             G5	        783.99
        60	            G#5	        830.61
        61	             A5	        880.00
        62	            A#5	        932.33
        63	             B5	        987.77
        64	             C6	       1046.50
        65	            C#6	       1108.73
        66	             D6	       1174.66
        67	            D#6	       1244.51
        68	             E6	       1318.51
        69	             F6	       1396.91
        70	            F#6	       1479.98
        71	             G6	       1567.98
        72	            G#6	       1661.22
        73	             A6	       1760.00
        74	            A#6	       1864.66
        75	             B6	       1975.53
        76	             C7	       2093.00
        77	            C#7	       2217.46
        78	             D7	       2349.32
        79	            D#7	       2489.02
        80	             E7	       2637.02
        81	             F7	       2793.83
        82	            F#7	       2959.96
        83	             G7	       3135.96
        84	            G#7	       3322.44
        85	             A7	       3520.00
        86	            A#7	       3729.31
        87	             B7	       3951.07
        88	             C8	       4186.01
>>> test = (('c', 4), ('e', 4), ('g', 4), ('c5', -2), ('e6', 8), ('d#6', 2))
>>> ps.make_wav(test, fn = "test.wav")
Writing to file test.wav
[1/6]	
[5/6]	

>>> exit()

metulburr@ubuntu:~$ vlc test.wav
VLC media player 2.2.2 Weatherwax (revision 2.2.2-0-g6259d80)

buran likes this post
Quote
#8
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.

As for game files, honestly, I prefer everything to be contained within the code of the game. For instance, my sprite function draws a sprite from a list of values (simply put, each value indicates a color, while its placement in the list is used to determine where its drawn on the screen). Here's the code I came up with that does this (I'm planning on making it into a custom class so I don't have to copy-paste all of this into each program):

import pygame
pygame.init()

mainWindow=pygame.display.set_mode((500,500))

x=200
y=250
pixel=10

row1=[0,0,0,1,1,0,0,0]
row2=[0,0,1,1,1,1,0,0]
row3=[0,1,1,1,1,1,1,0]
row4=[1,1,0,1,1,0,1,1]
row5=[1,1,1,1,1,1,1,1]
row6=[0,0,1,0,0,1,0,0]
row7=[0,1,0,1,1,0,1,0]
row8=[1,0,1,0,0,1,0,1]

sprite=row1+row2+row3+row4+row5+row6+row7+row8

def makeSprite():
    pixelx=x
    pixely=y
    r=0
    g=0
    b=0

    for a in sprite:
        if a==1:
            r=255
            g=255
            b=255
            pygame.draw.rect(mainWindow, (r,g,b),(pixelx,pixely,pixel,pixel))
        pixelx=pixelx+pixel
        if (pixelx-x)%(8*pixel)==0:
            pixelx=x
            pixely=pixely+pixel

def main():
    global x
    global y
    while True:
        pygame.time.delay(100)

        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                quit()
        key=pygame.key.get_pressed()
        if key[pygame.K_UP]:
            y=y-pixel
        if key[pygame.K_DOWN]:
            y=y+pixel
        if key[pygame.K_RIGHT]:
            x=x+pixel
        if key[pygame.K_LEFT]:
            x=x-pixel

        mainWindow.fill((0,0,0))
        makeSprite()
        pygame.display.update()

main()
Of course, this is an overly simplified version I made trying to figure out how to do this. I plan to have the thing be able to display more than 2 different colors, and also for the final version it will be possible to adjust both the dimensions of a sprite in pixels, and the exact dimensions of each pixel. Essentially, the class will be able to take 6 values: the x and y coordinates, the width and height of the sprite, and the width and height of the pixels. In my example program, the pixels are 10 screen pixels square, and the sprite is made up of an 8x8 grid of these. Doing this btw, I found that the pixel dimensions of the aliens in space invaders apparently aren't all 8x8 (one of them is 11x8), which is what inspired me to make it so that you can actually customize the dimensions of each sprite.

Anyway, as we see all the code for the sprite itself is stored within the program itself. I would also prefer to be able to synthesize music from within my programs themselves. When I do get around to making a sprite and music maker, honestly I'll probably have them output the files as a list that I can copy-paste into my programs, rather than having them make actual files.

Oh, and for the record, I've since learned to use set a framerate without using the time.delay command. I haven't bothered to do it with this program though since I only made it to learn how to draw sprites; I don't intend to actually use any of this in a program.
Quote
#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
Quote
#10
He imports something called 'scipy' into the program, which I don't seem to have. Is that really necessary for turning sine waves into sound?
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)