issue124:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue124:python [2017/08/28 12:24] – auntiee | issue124:python [2017/08/30 11:42] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Last month, I gave you some basic information about the GPIOZERO library in preparation for the music box program that uses the RPi, the MCP3008 from last month, three potentiometers and a number of switches to make a simple music player. | + | **Last month, I gave you some basic information about the GPIOZERO library in preparation for the music box program that uses the RPi, the MCP3008 from last month, three potentiometers and a number of switches to make a simple music player. |
- | Michael’s project includes a case, amplifier and speaker, but I’m not going to deal with those, since this is a discussion on using the RPi. I’ll leave those items to you to figure out. I’m using the HDMI audio out and it works just fine, with the exception of being tethered to the monitor by the HDMI cable. | + | Michael’s project includes a case, amplifier and speaker, but I’m not going to deal with those, since this is a discussion on using the RPi. I’ll leave those items to you to figure out. I’m using the HDMI audio out and it works just fine, with the exception of being tethered to the monitor by the HDMI cable.** |
- | The first thing you need to do is install Fluidsynth from the Add/Remove program on the RPi. Next, you need to add the pyfluidsynth python library. | + | Le mois dernier, je vous ai donné des informations de base sur la bibliothèque GPIOZERO en préparation du programme de boîte à musique qui utilise le RPi, le MCP3008 du mois dernier, trois potentiomètres et un certain nombre d' |
+ | |||
+ | Le projet de Michaël comprend un boîtier, un amplificateur et un haut-parleur, | ||
+ | |||
+ | **The first thing you need to do is install Fluidsynth from the Add/Remove program on the RPi. Next, you need to add the pyfluidsynth python library. | ||
pip install pyfluidsynth | pip install pyfluidsynth | ||
Ligne 9: | Ligne 13: | ||
I’m going to assume that you installed the GPIOZERO library that we used last month. | I’m going to assume that you installed the GPIOZERO library that we used last month. | ||
- | Hopefully, you still have the MCP3008 Analogue to Digital converter wired up from last month’s potentiometer project. | + | Hopefully, you still have the MCP3008 Analogue to Digital converter wired up from last month’s potentiometer project. |
+ | |||
+ | La première chose à faire est d' | ||
+ | |||
+ | pip install pyfluidsynth | ||
+ | |||
+ | Je pars du principe que vous avez installé la bibliothèque GPIOZERO le mois dernier. | ||
+ | |||
+ | J' | ||
- | Parts List | + | **Parts List |
For this project you will need: | For this project you will need: | ||
Ligne 24: | Ligne 36: | ||
• 9 momentary pushbuttons | • 9 momentary pushbuttons | ||
- | The potentiometers all have one lead that connects to ground, one that connects to 3.3vDC and the wiper connects to the MCP3008 pins 1, 2 and 3. The switches all have one lead connecting to ground and the others connect to GPIO pins 5, 23, 13, 6, 12, 16, 19, 20 and 21 (physical pins 29, 16, 33, 31, 32, 36, 35, 38 and 40) . The LED cathode connects to ground and the anode connects to one side of the 220 ohm resistor while the other side of the resistor connects to GPIO pin 24 (physical pin 18) | + | The potentiometers all have one lead that connects to ground, one that connects to 3.3vDC and the wiper connects to the MCP3008 pins 1, 2 and 3. The switches all have one lead connecting to ground and the others connect to GPIO pins 5, 23, 13, 6, 12, 16, 19, 20 and 21 (physical pins 29, 16, 33, 31, 32, 36, 35, 38 and 40) . The LED cathode connects to ground and the anode connects to one side of the 220 ohm resistor while the other side of the resistor connects to GPIO pin 24 (physical pin 18)** |
- | The Code | + | Liste de pièces |
+ | |||
+ | Pour ce projet, vous avez besoin : | ||
+ | |||
+ | • du Raspberry Pi, | ||
+ | • d'une plaque d' | ||
+ | • de cavaliers, | ||
+ | • du MCP3008, | ||
+ | • d'une LED, | ||
+ | • d'une résistance de 220 Ω, | ||
+ | • de 3 potentiomètres de 10 kΩ, | ||
+ | • de 9 boutons-poussoirs. | ||
+ | |||
+ | Les potentiomètres ont tous une patte reliée à la masse, une au 3,3 vDC et le curseur connecté aux bornes 1, 2, 3 du MCP3008. Les poussoirs ont tous une sortie branchée à la masse, et les autres connectées aux picots 5, 23, 13, 6, 12, 16, 19, 20 et 21 du GPIO (bornes physiques 29, 16, 33, 31, 32, 36, 35, 38 et 40). La cathode de la LED est connectée à la masse et l' | ||
+ | |||
+ | **The Code | ||
The code is fairly long, so I won’t include it all here. I will, however, pull out various snippets to discuss some of the less mundane segments. | The code is fairly long, so I won’t include it all here. I will, however, pull out various snippets to discuss some of the less mundane segments. | ||
Ligne 41: | Ligne 68: | ||
import fluidsynth | import fluidsynth | ||
- | The only library that you might not be familiar with is the ‘glob’ library. | + | The only library that you might not be familiar with is the ‘glob’ library. |
- | Next he loads up FluidSynth. | + | Le code |
+ | |||
+ | Le code est assez long ; aussi, je ne vais pas tout inclure ici. Cependant, j'en montrerai divers extraits pour présenter certains des segments les moins usuels. Je me baladerai aussi dans le code pour mettre en avant certains points. Je suggère fortement que vous récupériez le code de la page github de Michaël. Il comprend aussi des polices sonores pour le tester. | ||
+ | |||
+ | Bien sûr, la première chose que fait Michaël, ce sont les imports : | ||
+ | |||
+ | from __future__ import division | ||
+ | |||
+ | from gpiozero import Button, MCP3008, LED | ||
+ | |||
+ | import glob | ||
+ | import os | ||
+ | import re | ||
+ | import time | ||
+ | import fluidsynth | ||
+ | |||
+ | La seule bibliothèque qui pourrait ne pas vous être connue est « glob ». Cette bibliothèque vous permet de faire correspondre les noms de fichiers à un modèle. C'est simple mais très puissant. | ||
+ | |||
+ | **Next he loads up FluidSynth. | ||
# Start up the Synth and load the sound font | # Start up the Synth and load the sound font | ||
Ligne 67: | Ligne 112: | ||
pinky_finger = Button(21) | pinky_finger = Button(21) | ||
- | Now he defines which potentiometer is on which channel of the MCP3008. | + | Now he defines which potentiometer is on which channel of the MCP3008. |
- | + | ||
- | # Define potentiometers | + | |
- | + | ||
- | pot0 = MCP3008(channel=2) # Volume Control | + | |
- | + | ||
- | pot1 = MCP3008(channel=1) # Instrument Select | + | |
- | + | ||
- | pot2 = MCP3008(channel=0) # Additor | + | |
- | + | ||
- | The load_soundfonts() routine (top right) will walk through the soundfonts directory, getting the file names of the soundfonts into a usable list. Michael includes 15 sample soundfonts in his source files. | + | |
- | + | ||
- | This next routine (previous page, bottom right) will set the instrument for the synth based on the value of potentiometer #1 which as we saw above is on channel number 1 of the MCP3008. | + | |
- | + | ||
- | Here is where Michael sets up the actions for each of the music buttons. | + | |
- | + | ||
- | # Assign actions to when_pressed for each button | + | |
- | thumb_bottom.when_pressed = thumb_bottom_start | + | |
- | thumb_bottom.when_released = thumb_bottom_stop | + | |
- | thumb_right.when_pressed = thumb_right_start | + | |
- | thumb_right.when_released = thumb_right_stop | + | |
- | thumb_top.when_pressed = thumb_top_start | + | |
- | thumb_top.when_released = thumb_top_stop | + | |
- | index_finger.when_pressed = index_finger_start | + | |
- | index_finger.when_released = index_finger_stop | + | |
- | middle_finger.when_pressed = middle_finger_start | + | |
- | middle_finger.when_released = middle_finger_stop | + | |
- | ring_finger.when_pressed = ring_finger_start | + | |
- | ring_finger.when_released = ring_finger_stop | + | |
- | pinky_finger.when_pressed = pinky_finger_start | + | |
- | pinky_finger.when_released = pinky_finger_stop | + | |
- | + | ||
- | Glancing at index finger button action routine sets we see what's shown top right… | + | |
- | + | ||
- | The way things work on a synth is that when a note key is pressed a noteon event is called and when it is released, a noteoff event is then called. | + | |
- | + | ||
- | In the following lines, Michael sets the note definitions for each button. | + | |
- | + | ||
- | thumb_bottom_note = 54 | + | |
- | thumb_right_note = 56 | + | |
- | thumb_top_note = 58 | + | |
- | index_finger_note = 60 | + | |
- | middle_finger_note = 62 | + | |
- | ring_finger_note = 64 | + | |
- | pinky_finger_note = 66 | + | |
- | + | ||
- | I think that this gives you a basic understanding of what the code does, so you can continue on and play with the project. | + | |
- | + | ||
- | Once again, a huge thanks to Michael for this project. | + | |
- | + | ||
- | Until next time, have fun. | + | |
- | + | ||
- | Python dans le monde réel | + | |
- | + | ||
- | Le mois dernier, je vous ai donné des informations de base sur la bibliothèque GPIOZERO en préparation du programme de boîte à musique qui utilise le RPi, le MCP3008 du mois dernier, trois potentiomètres et un certain nombre d' | + | |
- | + | ||
- | + | ||
- | Le projet de Michaël comprend un boîtier, un amplificateur et un haut-parleur, | + | |
- | + | ||
- | La première chose que vous devez faire est d' | + | |
- | + | ||
- | pip install pyfluidsynth | + | |
- | + | ||
- | Je pars du principe que vous avez installé la bibliothèque GPIOZERO le mois dernier. | + | |
- | + | ||
- | J' | + | |
- | + | ||
- | Liste de pièces | + | |
- | + | ||
- | Pour ce projet, vous avez besoin : | + | |
- | + | ||
- | • du Raspberry Pi | + | |
- | • d'une plaque d' | + | |
- | • de cavaliers | + | |
- | • du MCP3008 | + | |
- | • d'une LED | + | |
- | • d'une résistance de 220 Ω | + | |
- | • de 3 potentiomètres de 10 kΩ | + | |
- | • de 9 boutons-poussoirs | + | |
- | + | ||
- | Les potentiomètres ont tous une patte reliée à la masse, une au 3,3 Vdc et le curseur connecté aux bornes 1, 2, 3 du MCP3008. Les poussoirs ont tous une sortie branchée à la masse, et les autres connectées aux picots 5, 23, 13, 6, 12, 16, 19, 20 et 21 du GPIO | + | |
- | | + | |
- | + | ||
- | Le code | + | |
- | + | ||
- | Le code est assez long ; aussi, je ne vais pas l' | + | |
- | + | ||
- | Bien sûr, la première chose que fait Michaël, ce sont les imports ... | + | |
- | + | ||
- | from __future__ import division | + | |
- | + | ||
- | from gpiozero import Button, | + | |
- | MCP3008, LED | + | |
- | + | ||
- | import glob | + | |
- | import os | + | |
- | import re | + | |
- | import time | + | |
- | import fluidsynth | + | |
- | + | ||
- | La seule bibliothèque qui pourrait ne pas vous être connue est celle de « glob ». Cette bibliothèque vous permet de faire correspondre les noms de fichiers à un modèle. C'est simple mais très puissant. | + | |
Ensuite, il charge FluidSynth. | Ensuite, il charge FluidSynth. | ||
Ligne 176: | Ligne 121: | ||
fs.start(driver=' | fs.start(driver=' | ||
- | Maintenant, voici la section de définition | + | Maintenant, voici la section de définition |
- | Souvenez-vous que les numéros de picots | + | |
# Paramétrage des poussoirs pour la remise à zéro et l' | # Paramétrage des poussoirs pour la remise à zéro et l' | ||
Ligne 194: | Ligne 138: | ||
pinky_finger = Button(21) | pinky_finger = Button(21) | ||
- | Maintenant, il définit quel potentiomètre est actif sur quel canal du MCP3008. (J'ai ajouté les commentaires de cette partie.) | + | **# Define potentiometers |
+ | |||
+ | pot0 = MCP3008(channel=2) # Volume Control | ||
+ | |||
+ | pot1 = MCP3008(channel=1) # Instrument Select | ||
+ | |||
+ | pot2 = MCP3008(channel=0) # Additor | ||
+ | |||
+ | The load_soundfonts() routine (top right) will walk through the soundfonts directory, getting the file names of the soundfonts into a usable list. | ||
+ | |||
+ | This next routine (previous page, bottom right) will set the instrument for the synth based on the value of potentiometer #1 which as we saw above is on channel number 1 of the MCP3008. | ||
+ | |||
+ | Here is where Michael sets up the actions for each of the music buttons. | ||
# Définition des potentiomètres | # Définition des potentiomètres | ||
- | pot0 = MCP3008(channel=2) # | + | pot0 = MCP3008(channel=2) # Contrôle du volume. |
- | Contrôle du volume | + | |
- | pot1 = MCP3008(channel=1) # | + | pot1 = MCP3008(channel=1) # Sélection de l' |
- | Sélection de l' | + | |
- | pot2 = MCP3008(channel=0) # | + | pot2 = MCP3008(channel=0) # Changement d' |
- | Changement d' | + | |
- | La routine load_soundfonts() (en haut à droite) va traverser | + | La routine load_soundfonts() (en haut à droite) va parcourir |
- | La routine suivante (page précédente, | + | La routine suivante (page précédente, |
- | Voici là où Michaël paramètre les actions pour chacun des poussoirs musicaux. De base, il y a les actions « when-pressed | + | C'est ici que Michaël paramètre les actions pour chacun des poussoirs musicaux. De base, il y a les actions « when_pressed |
+ | |||
+ | **# Assign actions to when_pressed for each button | ||
+ | thumb_bottom.when_pressed = thumb_bottom_start | ||
+ | thumb_bottom.when_released = thumb_bottom_stop | ||
+ | thumb_right.when_pressed = thumb_right_start | ||
+ | thumb_right.when_released = thumb_right_stop | ||
+ | thumb_top.when_pressed = thumb_top_start | ||
+ | thumb_top.when_released = thumb_top_stop | ||
+ | index_finger.when_pressed = index_finger_start | ||
+ | index_finger.when_released = index_finger_stop | ||
+ | middle_finger.when_pressed = middle_finger_start | ||
+ | middle_finger.when_released = middle_finger_stop | ||
+ | ring_finger.when_pressed = ring_finger_start | ||
+ | ring_finger.when_released = ring_finger_stop | ||
+ | pinky_finger.when_pressed = pinky_finger_start | ||
+ | pinky_finger.when_released = pinky_finger_stop | ||
+ | |||
+ | Glancing at index finger button action routine sets we see what's shown top right…** | ||
# Assignation des actions quand un des poussoirs est appuyé | # Assignation des actions quand un des poussoirs est appuyé | ||
Ligne 230: | Ligne 201: | ||
pinky_finger.when_released = pinky_finger_stop | pinky_finger.when_released = pinky_finger_stop | ||
- | En examinant ce que fait la routine d' | + | En examinant ce que fait la routine d' |
- | La façon dont les choses fonctionnent sur un synthétiseur est que, quand une touche | + | **The way things work on a synth is that when a note key is pressed a noteon event is called and when it is released, a noteoff event is then called. |
+ | |||
+ | In the following lines, Michael sets the note definitions for each button.** | ||
+ | |||
+ | La façon dont les choses fonctionnent sur un synthétiseur est que, quand on appuie sur une touche, un événement noteon est appelé et quand elle est relâchée, un événement noteoff est appelé. (C'est pourquoi, parfois, quand vous écoutez un fichier midi sur un ordinateur, vous avez des notes qui semblent être accidentellement en appui permanent, les laissant jouer sans fin. L' | ||
Dans les lignes suivantes, Michaël définit la note de chaque poussoir. | Dans les lignes suivantes, Michaël définit la note de chaque poussoir. | ||
+ | |||
+ | **thumb_bottom_note = 54 | ||
+ | thumb_right_note = 56 | ||
+ | thumb_top_note = 58 | ||
+ | index_finger_note = 60 | ||
+ | middle_finger_note = 62 | ||
+ | ring_finger_note = 64 | ||
+ | pinky_finger_note = 66 | ||
+ | |||
+ | I think that this gives you a basic understanding of what the code does, so you can continue on and play with the project. | ||
+ | |||
+ | Once again, a huge thanks to Michael for this project. | ||
+ | |||
+ | Until next time, have fun.** | ||
thumb_bottom_note = 54 | thumb_bottom_note = 54 | ||
Ligne 245: | Ligne 234: | ||
- | Je pense que ceci vous donne une compréhension de base de ce que fais le code ; aussi, vous pouvez poursuivre et jouer avec le projet. | + | Je pense que ceci vous donne une compréhension de base de ce que fait le code ; aussi, vous pouvez poursuivre et jouer avec le projet. |
- | Une fois encore, merci à Michaël pour ce projet. | + | Une fois encore, |
Jusqu' | Jusqu' |
issue124/python.1503915862.txt.gz · Dernière modification : 2017/08/28 12:24 de auntiee