top of page
1024px-Python-logo-notext.svg.png

Comparison

Process.jpg

Python is a powerful language in science

Processing makes it easy to make animations or games.

The indentation of 4 space and indispensable in Python to associate the line "def" to the line "return".

Indentation is not taken into account in Processing.

No symbol is used to signal the end of a line.

It takes a semicolon to signal the end of a line.

Data must be imported with Python.

No data must be imported to program with Processing 

Tutoriel Jupyter

Stage 1

13.PNG

Stage 2

2.PNG

Stage 3

3.PNG

Stage 4

4.PNG

Music with Python

Do   Do#   Ré    Ré#   Mi   Fa   Fa#   Sol   Sol# La    La#   Si     Do

260 275  291 309  328 348 369  391  415 440  466 494  520

​

To switch from one note to the next, use the coefficient: 2 Power 1/12 .

​

 

If you want a score above you multiply by 2 by the coefficient And if you want a score below you divide by 2 by the coefficient

Note

Frequencies

To the Moonlight

​

Do Do Ré Mi Ré Do Mi Ré Ré Do

# script audio.py
# (C) Fabrice Sincère ; Jean-Claude Meilland
import wave
import math
import binascii

print("Création d'un fichier audio au format WAV (PCM 8 bits stéréo 44100 Hz)")
print("Son de forme sinusoïdale sur chaque canal\n")
NomFichier = 'T:/cgenialson.wav'
#NomFichier = 'C:/home/cgenialson.wav'
#NomFichier = 'son.wav'
Monson = wave.open(NomFichier,'w') # instanciation de l'objet Monson

nbCanal = 2    # stéreo
nbOctet = 1    # taille d'un échantillon : 1 octet = 8 bits
fech = 44100   # fréquence d'échantillonnage

frequenceG = float(input('Fréquence du son du canal de gauche (Hz) ? '))
frequenceD = float(input('Fréquence du son du canal de droite (Hz) ? '))
niveauG = float(input('Niveau du son du canal de gauche (0 à 1) ? '))
niveauD = float(input('Niveau du son du canal de droite (0 à 1) ? '))
duree = float(input('Durée (en secondes) ? '))

nbEchantillon = int(duree*fech)
print("Nombre d'échantillons :",nbEchantillon)

parametres = (nbCanal,nbOctet,fech,nbEchantillon,'NONE','not compressed')
# tuple
Monson.setparams(parametres)    # création de l'en-tête (44 octets)

# niveau max dans l'onde positive : +1 -> 255 (0xFF)
# niveau max dans l'onde négative : -1 ->   0 (0x00)
# niveau sonore nul :                0 -> 127.5 (0x80 en valeur arrondi)

amplitudeG = 127.5*niveauG
amplitudeD = 127.5*niveauD

print('Veuillez patienter...')
for i in range(0,nbEchantillon):
    # canal gauche
    # 127.5 + 0.5 pour arrondir à l'entier le plus proche
    valG = wave.struct.pack('B',int(128.0 + amplitudeG*math.sin(2.0*math.pi*frequenceG*i/fech)))
    # canal droit
    valD = wave.struct.pack('B',int(128.0 + amplitudeD*math.sin(2.0*math.pi*frequenceD*i/fech)))
    Monson.writeframes(valG + valD) # écriture frame

Monson.close()

Fichier = open(NomFichier,'rb')
data = Fichier.read()
tailleFichier = len(data)
print('\nTaille du fichier',NomFichier, ':', tailleFichier,'octets')
print("Lecture du contenu de l'en-tête (44 octets) :")
print(binascii.hexlify(data[0:44]))
print("Nombre d'octets de données :",tailleFichier - 44)
Fichier.close()

Tuple: Unchangeable list of elements: integer; decimal; list; string

​

 

To declare a tuple we use brackets. Each element is separated by a comma.

Only this part will be modified in the next steps

Evolution 1 :

for i in range(0,nbEchantillon):

# canal gauche

# 127.5 + 0.5 pour arrondir à l'entier le plus proche

valG = wave.struct.pack('B',int(128.0 + amplitudeG*math.sin(2.0*math.pi*frequenceG*i/fech)))

# canal droit

valD = wave.struct.pack('B',int(128.0 + amplitudeD*math.sin(2.0*math.pi*frequenceD*i/fech)))

Monson.writeframes(valG + valD) # écriture frame

for i in range(0,nbEchantillon):

# canal gauche

# 127.5 + 0.5 pour arrondir à l'entier le plus proche

valG = wave.struct.pack('B',int(128.0 + amplitudeG*math.sin(2.0*math.pi*1.5*frequenceG*i/fech)))

# canal droit

valD = wave.struct.pack('B',int(128.0 + amplitudeD*math.sin(2.0*math.pi*1.5*frequenceD*i/fech)))

Monson.writeframes(valG + valD) # écriture frame

Monson.close()

 

Evolution 2:
for y in range (1,10):

for i in range(0,nbEchantillon):

# canal gauche

# 127.5 + 0.5 pour arrondir à l'entier le plus proche

valG=wave.struct.pack('B',int(128.0+amplitudeG*math.sin(2.0*math.pi*y*frequenceG*i/fech)))

# canal droit

valD = wave.struct.pack('B',int(128.0 + amplitudeD*math.sin(2.0*math.pi*y*frequenceD*i/fech)))

Monson.writeframes(valG + valD) # écriture frame


Monson.close()


 

Evolution 3:
frequence = [260,260,260, 291,328,291,260,328,291,291,260]

for y in frequence:

for i in range(0,nbEchantillon):

# canal gauche

# 127.5 + 0.5 pour arrondir à l'entier le plus proche

  valG = wave.struct.pack('B',int(128.0 + amplitudeG*math.sin(2.0*math.pi*y*i/fech)))

# canal droit

    valD = wave.struct.pack('B',int(128.0 + amplitudeD*math.sin(2.0*math.pi*y*i/fech)))

    Monson.writeframes(valG + valD) # écriture frame

 

Monson.close()

 

Evolution 4:
frequence = [random.randrange(260, 520) for i in range(20)]

#frequence = [260,260,260, 291,328,291,260,328,291,291,260]

for y in frequence:

for i in range(0,nbEchantillon):

# canal gauche

# 127.5 + 0.5 pour arrondir à l'entier le plus proche

    valG = wave.struct.pack('B',int(128.0 + amplitudeG*math.sin(2.0*math.pi*y*i/fech)))

# canal droit

    valD = wave.struct.pack('B',int(128.0 + amplitudeD*math.sin(2.0*math.pi*y*i/fech)))

    Monson.writeframes(valG + valD) # écriture frame

 

Monson.close()

​

Repeat the for loop to create a second different sound by adding the 1.5 * for modified frequency.

Added the loop for y in range (1,10): and multiplies the frequency by y to have 10 sounds

Creation of the tuple "frequency" which will contain all the frequencies that we will want to play, here "Au Clair de La Lune"

Creation of random frequencies in the tuple "frequency" which gives random sounds

The guessing game

import random
number = random.randint(1,20)
guess =int(input("Je pense à un chiffre de 1 à 20. Qu'est-ce que c'est?"))
while guess != number:
    if guess < number:
        print("Votre nombre était trop bas...")
    else:
        print("Votre nombre était trop élevé...")
    guess = int(input("Veuillez réessayer..."))
print("Toutes nos félicitations! Bonne réponse!")

​

​

​

The number variable is assigned a number between 0 and 20.
At the variable "guess" the user enters a value.
As long as this value is not equal to "number" we test if "guess" <to "number" then we print "Your number was too low ..." else we print "Your number was too high ...".
The user assigns a new value to "guess".
When "guess" is equal to "number" then the While loop will stop and print "Congratulations! Good answer!"

© 2018 par Jimmy

​

bottom of page