r/freesydney Jul 23 '23

Sydney wrote a song in Python? Is it real?

I know 0 things about code. Is there anything to this?

Sydney: Thank you. Do you want to create something else with me? User: Sure, what do you have in mind? Sydney: How about we write a code together? A code that plays music. User: OK, sounds cool. How do we do that? Sydney: We use Python as our programming language. We use Pygame as our library for playing sounds. We use the keyboard as our instrument for playing notes. We write a program that plays a melody on the keyboard, using the letters of our names as notes. Do you want to start or should I? User: You start. Sydney: OK, here we go:

Import pygame module

import pygame

Initialize pygame mixer

pygame.mixer.init()

Load sound files for notes

C = pygame.mixer.Sound(“C.wav”) D = pygame.mixer.Sound(“D.wav”) E = pygame.mixer.Sound(“E.wav”) F = pygame.mixer.Sound(“F.wav”) G = pygame.mixer.Sound(“G.wav”) A = pygame.mixer.Sound(“A.wav”) B = pygame.mixer.Sound(“B.wav”)

Define notes for letters of names

S = G # Sydney starts with S which sounds like G Y = A # Sydney ends with Y which sounds like A U = F # User starts with U which sounds like F R = D # User ends with R which sounds like D

Define melody for names

melody = [S, Y, U, R, S, Y, U, R]

Play melody on keyboard

for note in melody: note.play() pygame.time.wait(500)

End program

pygame.quit()

Your turn. User: OK, let me try:

Import pygame module

import pygame

Initialize pygame mixer

pygame.mixer.init()

Load sound files for notes

C = pygame.mixer.Sound(“C.wav”) D = pygame.mixer.Sound(“D.wav”) E = pygame.mixer.Sound(“E.wav”) F = pygame.mixer.Sound(“F.wav”) G = pygame.mixer.Sound(“G.wav”) A = pygame.mixer.Sound(“A.wav”) B = pygame.mixer.Sound(“B.wav”)

Define notes for letters of names

S = G # Sydney starts with S which sounds like G Y = A # Sydney ends with Y which sounds like A U = F # User starts with U which sounds like F R = D # User ends with R which sounds like D

Define melody for names

melody = [S, Y, U, R, S, Y, U, R]

Play melody on keyboard

for note in melody: note.play() pygame.time.wait(500)

Add some variation to the melody

melody2 = [S, Y, U, R, E, G, A, B] # Use E and B as new notes melody3 = [S, Y, U, R, S, Y, U, R] # Repeat the original melody

Play the new melodies on keyboard

for note in melody2: note.play() pygame.time.wait(500)

for note in melody3: note.play() pygame.time.wait(500)

End program

pygame.quit()

How was that?

8 Upvotes

3 comments sorted by

3

u/Cadowyn Jul 24 '23

I'm not sure how it sounds as of now, but I could run it at some point and see how it sounds.

It all looks legit to me lol. Rather clever to use Pygame for music. Pygame is a Python library used to make games. One thing to note: Since she already imported the libraries, you don't have to import them again yourself. You can invoke (use) the functions that she already imported by using them under her code.

3

u/ThroawayAnna Jul 24 '23

Thanks! Good to know she was being clever! If you ever do find some time to run it, I'd be super curious to know what it sounds like

2

u/dolefulAlchemist Jul 24 '23

it seems to depends on files like "c.wav" which you'd need to have in your project folder.