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 09:19] – d52fr | issue124:python [2017/08/30 11:42] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Python dans le monde réel | + | **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. |
- | 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' | + | 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. |
+ | 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, | + | 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' | + | **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 | ||
+ | |||
+ | 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. | ||
+ | |||
+ | La première chose à faire est d' | ||
pip install pyfluidsynth | pip install pyfluidsynth | ||
Ligne 13: | Ligne 22: | ||
J' | J' | ||
+ | |||
+ | **Parts List | ||
+ | |||
+ | For this project you will need: | ||
+ | |||
+ | • Raspberry Pi | ||
+ | • Breadboard | ||
+ | • Jumpers | ||
+ | • MCP3008 | ||
+ | • 1 LED | ||
+ | • 1 220 Ohm Resistor | ||
+ | • 3 10K potentiometers | ||
+ | • 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)** | ||
Liste de pièces | Liste de pièces | ||
Ligne 18: | Ligne 42: | ||
Pour ce projet, vous avez besoin : | Pour ce projet, vous avez besoin : | ||
- | • du Raspberry Pi | + | • du Raspberry Pi, |
- | • d'une plaque d' | + | • d'une plaque d' |
- | • de cavaliers | + | • de cavaliers, |
- | • du MCP3008 | + | • du MCP3008, |
- | • d'une LED | + | • d'une LED, |
- | • d'une résistance de 220 Ω | + | • d'une résistance de 220 Ω, |
- | • de 3 potentiomètres de 10 kΩ | + | • de 3 potentiomètres de 10 kΩ, |
- | • de 9 boutons-poussoirs | + | • 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 | + | 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' |
- | (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. | ||
+ | Of course, the very first thing that Michael does is his imports… | ||
+ | |||
+ | from __future__ import division | ||
+ | |||
+ | from gpiozero import Button, MCP3008, LED | ||
+ | |||
+ | import glob | ||
+ | import os | ||
+ | import re | ||
+ | import time | ||
+ | import fluidsynth | ||
+ | |||
+ | The only library that you might not be familiar with is the ‘glob’ library. | ||
Le code | Le code | ||
- | Le code est assez long ; aussi, je ne vais pas l'inclure ici. Cependant, | + | Le code est assez long ; aussi, je ne vais pas tout inclure ici. Cependant, |
- | Bien sûr, la première chose que fait Michaël, ce sont les imports | + | Bien sûr, la première chose que fait Michaël, ce sont les imports |
from __future__ import division | from __future__ import division | ||
- | from gpiozero import Button, | + | from gpiozero import Button, MCP3008, LED |
- | MCP3008, LED | + | |
import glob | import glob | ||
Ligne 47: | Ligne 86: | ||
import fluidsynth | 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. | + | 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 | ||
+ | |||
+ | fs = fluidsynth.Synth() | ||
+ | fs.start(driver=' | ||
+ | |||
+ | Now, here is the button definition section. | ||
+ | |||
+ | # Set-up buttons for reset and shutdown | ||
+ | |||
+ | button_reset = Button(5) | ||
+ | button_shutdown = Button(23) | ||
+ | |||
+ | # Set-up buttons for keyboard input | ||
+ | |||
+ | thumb_bottom = Button(13) | ||
+ | thumb_top = Button(6) | ||
+ | thumb_right = Button(12) | ||
+ | index_finger = Button(16) | ||
+ | middle_finger = Button(19) | ||
+ | ring_finger = Button(20) | ||
+ | pinky_finger = Button(21) | ||
+ | |||
+ | Now he defines which potentiometer is on which channel of the MCP3008. | ||
Ensuite, il charge FluidSynth. | Ensuite, il charge FluidSynth. | ||
Ligne 56: | 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 74: | 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, |
+ | |||
+ | C'est ici que Michaël paramètre les actions pour chacun des poussoirs musicaux. De base, il y a les actions « when_pressed » (si appuyé) et « when_released » (si relâché) qui appellent une routine quand l' | ||
+ | |||
+ | **# 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 | ||
- | Voici là où Michaël paramètre les actions pour chacun des poussoirs musicaux. De base, il y a les actions « when-pressed » (si appuyé) et « when-released » (si relâché) qui appellent une routine | + | Glancing at index finger button action |
# Assignation des actions quand un des poussoirs est appuyé | # Assignation des actions quand un des poussoirs est appuyé | ||
Ligne 110: | 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 125: | 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.1503904782.txt.gz · Dernière modification : 2017/08/28 09:19 de d52fr