| Ligne 34 : |
Ligne 34 : |
| | | | |
| | == Le code == | | == Le code == |
| − | xxx
| + | Voici un script élémentaire qui permet de jouer une mélodie. |
| | + | |
| | + | La mélodie est encodée dans une chaîne de caractère. |
| | + | |
| | + | <nowiki>from pyb import Pin, Timer, udelay |
| | + | |
| | + | # Configurer les broches PWM pour la sortie sur buzzer |
| | + | p2 = Pin("Y2") # Broche Y2 avec timer 8 et Channel 2 |
| | + | tim = Timer(8, freq=3000) |
| | + | ch = tim.channel(2, Timer.PWM, pin=p2) |
| | + | |
| | + | # Notes/tonalite simple avec frequence correspondantes |
| | + | notes = { ' ' : 0, # silence, |
| | + | 'c' : 261, # Do |
| | + | 'd' : 294, # Ré |
| | + | 'e' : 329, # Mi |
| | + | 'f' : 349, # Fa |
| | + | 'g' : 392, # Sol |
| | + | 'a' : 440, # La |
| | + | 'b' : 493, # Si |
| | + | 'C' : 523 # Do |
| | + | } |
| | + | |
| | + | # Jouer une mélodie: |
| | + | # Liste de Note + Rythme (separe par des virgules) |
| | + | # Premier caractere = la note/tonalite |
| | + | # Second caractere = rythme (manquant=defaut=1) |
| | + | tune = "c,c,g,g,a,a,g2,f,f,e,e,d,d,c2, 4" |
| | + | tune2 = "c2,c,d3,c3,f3,e3,c2,c,d3,c3,g3,f3, 4" |
| | + | tempo = 300 |
| | + | |
| | + | # Jouer une frequence donnee (donc une note) |
| | + | def play_freq( freq ): |
| | + | if freq == 0: |
| | + | ch.pulse_width_percent( 0 ) |
| | + | else: |
| | + | tim.freq( freq ) |
| | + | ch.pulse_width_percent( 30 ) |
| | + | |
| | + | # jouer les notes abcdef... pendant une duree donnee |
| | + | def play_note( note, duration ): |
| | + | # Transforme la note en frequence |
| | + | freq = notes[ note ] |
| | + | play_freq( freq ) |
| | + | # Transforme la duree en temp (1=1000micros, 2=2000micros, etc) |
| | + | udelay( duration * 1000 ) # temps en micro-second |
| | + | |
| | + | # Joue toutes les notes connues (du dictionnaire, test play_tone) |
| | + | def test_notes(): |
| | + | note_duration = 2 |
| | + | for note, freq in notes.items(): |
| | + | print(note) |
| | + | play_note( note, tempo * note_duration ) |
| | + | pyb.delay(300) |
| | + | |
| | + | # Joue une mélodie encodée dans une chaine de caractere |
| | + | def play_tune( tune_string ): |
| | + | tune_list = tune_string.split( ',' ) |
| | + | duration = 1 |
| | + | for tune_item in tune_list: |
| | + | # print( tune_item ) |
| | + | if len( tune_item )>1: |
| | + | try: |
| | + | duration = int( tune_item[1:] ) |
| | + | except: |
| | + | raise ValueError( 'Invalid duration %s for note %s' % (tune_item[1:], tune_item[0]) ) |
| | + | else: |
| | + | duration = 1 |
| | + | |
| | + | # Jouer la note |
| | + | play_note( tune_item[0], tempo * duration ) |
| | + | # Pause entre 2 notes |
| | + | play_freq( 0 ) |
| | + | udelay( (tempo * 1000) // 2 ) |
| | + | |
| | + | # test_notes() |
| | + | play_tune( tune ) # A vous dirais-je maman |
| | + | # play_tune( tune2 ) # Joyeux anniversaire |
| | + | |
| | + | # Pour forcer le silence |
| | + | # play_note( ' ', 1 )</nowiki> |
| | | | |
| | == Cela ne fonctionne pas? == | | == Cela ne fonctionne pas? == |