Exercice generer un son et editer un graphique

david_3517 Messages postés 3 Date d'inscription   Statut Membre Dernière intervention   -  
david_3517 Messages postés 3 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour,

je suis sur jupyter et je n'arrive pas à crée mon graphique
- Etape 1 générer un son c'est ok
- Etape 2 l'enregistrer au forma "wav" c'est ok
- Etape 3 graphique

et là ça pose problème quelqu'un peut m'aider svp ?


import math        #import needed modules
import pyaudio #sudo apt-get install python-pyaudio
import wave
import numpy as np
import matplotlib.pyplot as plt

nomFichier = 'son.wav' # Nom du fichier à créer
monSon = wave.open( nomFichier, 'w') # Ouverture fichier en écriture
nbCanal = 1 # Son monophonique (un seul canal)
nbOctet = 2 # Chaque échantillon est quantifié sur 8 bits
fech = 44100 # On échantillone à 44,1 kHz
nbEchantillons = 44100
parametres = (nbCanal, nbOctet, fech, nbEchantillons, 'NONE', 'not compressed')
monSon.setparams(parametres)
fLa = 440
for i in range(nbEchantillons):
    val = wave.struct.pack('B',int( (255/2)*(1+math.sin(2*math.pi*fLa*i/fech))))
    monSon.writeframes(val)
monSon.close()


PyAudio = pyaudio.PyAudio     #initialize pyaudio

#See [https://en.wikipedia.org/wiki/Bit_rate#Audio]
BITRATE = 44100    #number of frames per second/frameset.      

FREQUENCY = 19000   #Hz, waves per second, 261.63=C4-note.
LENGTH = 1 #seconds to play sound

if FREQUENCY > BITRATE:
    BITRATE = FREQUENCY+100

NUMBEROFFRAMES = int(BITRATE * LENGTH)
RESTFRAMES = NUMBEROFFRAMES % BITRATE
WAVEDATA = ''    

#generating wawes
for x in range(NUMBEROFFRAMES):
    WAVEDATA = WAVEDATA+chr(int(math.sin(x/((BITRATE/FREQUENCY)/math.pi))*127+128))    

for x in range(RESTFRAMES): 
    WAVEDATA = WAVEDATA+chr(128)

print(WAVEDATA)

p = PyAudio()
stream = p.open(format = p.get_format_from_width(1), 
                channels = 1, 
                rate = BITRATE, 
                output = True)

stream.write(WAVEDATA)
stream.stop_stream()
stream.close()
p.terminate()


fig = plt.figure(figsize=(10,5))

z = np.arange(0,NUMBEROFFRAMES,1)
plt.plot(z,(255/2)*(1+math.sin(2*math.pi*fLa*i/fech)))
#plt.plot(LENGTH1,math.sin(x/((BITRATE1/FREQUENCY1)/math.pi))*127+128)
plt.axis([0,100,-1.1,1.1])
plt.show()#affichage du graphique
A voir également:

3 réponses

Phil_1857 Messages postés 1872 Date d'inscription   Statut Membre Dernière intervention   168
 
Bonjour,

L'indentation étant importante en Python, merci de re poster ton code avec les balises de code
mode d'emploi:
https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code
Visuellement, ça doit ressembler à ceci (avec la coloration syntaxique) :

def test():
    print('test')

test()
0
david_3517 Messages postés 3 Date d'inscription   Statut Membre Dernière intervention  
 
Excusez moi





import math        #import needed modules
import pyaudio #sudo apt-get install python-pyaudio
import wave
import numpy as np
import matplotlib.pyplot as plt

nomFichier = 'son.wav' # Nom du fichier à créer
monSon = wave.open( nomFichier, 'w') # Ouverture fichier en écriture
nbCanal = 1 # Son monophonique (un seul canal)
nbOctet = 2 # Chaque échantillon est quantifié sur 8 bits
fech = 44100 # On échantillone à 44,1 kHz
nbEchantillons = 44100
parametres = (nbCanal, nbOctet, fech, nbEchantillons, 'NONE', 'not compressed')
monSon.setparams(parametres)
fLa = 440
for i in range(nbEchantillons):
    val = wave.struct.pack('B',int( (255/2)*(1+math.sin(2*math.pi*fLa*i/fech))))
    monSon.writeframes(val)
monSon.close()


PyAudio = pyaudio.PyAudio     #initialize pyaudio

#See https://en.wikipedia.org/wiki/Bit_rate#Audio
BITRATE = 44100    #number of frames per second/frameset.      

FREQUENCY = 19000   #Hz, waves per second, 261.63=C4-note.
LENGTH = 1 #seconds to play sound

if FREQUENCY > BITRATE:
    BITRATE = FREQUENCY+100

NUMBEROFFRAMES = int(BITRATE * LENGTH)
RESTFRAMES = NUMBEROFFRAMES % BITRATE
WAVEDATA = ''    

#generating wawes
for x in range(NUMBEROFFRAMES):
    WAVEDATA = WAVEDATA+chr(int(math.sin(x/((BITRATE/FREQUENCY)/math.pi))*127+128))    

for x in range(RESTFRAMES): 
    WAVEDATA = WAVEDATA+chr(128)

print(WAVEDATA)

p = PyAudio()
stream = p.open(format = p.get_format_from_width(1), 
                channels = 1, 
                rate = BITRATE, 
                output = True)

stream.write(WAVEDATA)
stream.stop_stream()
stream.close()
p.terminate()


fig = plt.figure(figsize=(10,5))

z = np.arange(0,NUMBEROFFRAMES,1)
plt.plot(z,(255/2)*(1+math.sin(2*math.pi*fLa*i/fech)))
#plt.plot(LENGTH1,math.sin(x/((BITRATE1/FREQUENCY1)/math.pi))*127+128)
plt.axis([0,100,-1.1,1.1])
plt.show()#affichage du graphique
0
Phil_1857 Messages postés 1872 Date d'inscription   Statut Membre Dernière intervention   168
 
tu as du te tromper: on ne voit pas les couleurs

pas grave, baladur a modifié ton message initial ...
0
david_3517 Messages postés 3 Date d'inscription   Statut Membre Dernière intervention  
 
Vous pensez que le probleme vient d'ou ?
c'est un sac de noeuds non ?
0