issue190: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 | ||
issue190:python [2023/02/27 14:19] – d52fr | issue190:python [2023/03/01 12:56] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 2: | Ligne 2: | ||
Speaking of which, let’s get started. You might notice the title of this article. Yes, it DOES have to do with Python and missing beeps. For years, my computer has not had a built-in speaker, so the beep that I would normally rely on to get the user’s attention didn’t function the way I was used to. For a while, I tried in vain to get the blasted BEEP to respond, then something would come up and I’d have to abandon it for a while. Each time, the “a while” became longer and longer, and it eventually got lost in the shuffle. Until today.** | Speaking of which, let’s get started. You might notice the title of this article. Yes, it DOES have to do with Python and missing beeps. For years, my computer has not had a built-in speaker, so the beep that I would normally rely on to get the user’s attention didn’t function the way I was used to. For a while, I tried in vain to get the blasted BEEP to respond, then something would come up and I’d have to abandon it for a while. Each time, the “a while” became longer and longer, and it eventually got lost in the shuffle. Until today.** | ||
+ | |||
+ | J'ai une confession embarrassante à faire. Entre les problèmes de météo dans le centre du Texas, les échéances des chapitres du livre, les problèmes de support et de tests, je n'ai pas commencé à penser à ce numéro du FCM avant le 1er février. Je n'ai même pas commencé à faire des recherches ou à écrire les programmes de démonstration avant le 2 février. Cependant, une fois que j' | ||
+ | |||
+ | Sur ce, commençons. Vous avez peut-être remarqué le titre de cet article. Oui, il a bien un rapport avec Python et les « bips » manquants. Pendant des années, mon ordinateur n'a pas eu de haut-parleur intégré, donc le bip sur lequel je comptais normalement pour attirer l' | ||
+ | |||
**I tried to go back – to find a way to get the bleep working – and still no luck. So I decided to give up on attempting to force a bleep tone out of my speakers, and started looking for an alternative method, one that would not force any of my users to have to install a ton of libraries before they could use a program of mine. I found a library named (of all things) Beepy. It is a pretty simple, lightweight library, and it has only one dependency. So I gave it a try. The instructions said to install it via pip; I’m lucky that my Python installs don’t require using a “3” at the end of my Python and Pip commands, so I forget that many of you aren’t quite that lucky. So if you can use just a “pip”, convert it mentally when you see “pip3”. | **I tried to go back – to find a way to get the bleep working – and still no luck. So I decided to give up on attempting to force a bleep tone out of my speakers, and started looking for an alternative method, one that would not force any of my users to have to install a ton of libraries before they could use a program of mine. I found a library named (of all things) Beepy. It is a pretty simple, lightweight library, and it has only one dependency. So I gave it a try. The instructions said to install it via pip; I’m lucky that my Python installs don’t require using a “3” at the end of my Python and Pip commands, so I forget that many of you aren’t quite that lucky. So if you can use just a “pip”, convert it mentally when you see “pip3”. | ||
Ligne 21: | Ligne 26: | ||
name ' | name ' | ||
>>> | >>> | ||
+ | |||
+ | J'ai essayé de revenir en arrière - pour trouver un moyen de faire fonctionner le bip - et toujours pas de chance. J'ai donc décidé d' | ||
+ | |||
+ | pip3 install beepy | ||
+ | |||
+ | J'ai lancé mon REPL ptpython... Les instructions disent : | ||
+ | |||
+ | import beepy | ||
+ | beep(sound=1) | ||
+ | |||
+ | C'est assez facile à suivre, même pour moi. Malheureusement, | ||
+ | |||
+ | Traceback (appel le plus récent en dernier) : | ||
+ | |||
+ | File "< | ||
+ | NameError: name ' | ||
+ | |||
+ | name ' | ||
+ | >>> | ||
+ | |||
**I double checked the documentation (what little there is), and the instructions still read the same. So I thought maybe there was a change in the program and the documentation didn’t get updated, so I scratched multiple body parts and decided I’d take a look at the output of the dir() function. | **I double checked the documentation (what little there is), and the instructions still read the same. So I thought maybe there was a change in the program and the documentation didn’t get updated, so I scratched multiple body parts and decided I’d take a look at the output of the dir() function. | ||
Ligne 44: | Ligne 69: | ||
Hmmm. Only 7 options. I tried all 7 sounds and, while they worked, I wasn’t really impressed. Given the fact that the last release was in 2019, I thought there might be a better solution.** | Hmmm. Only 7 options. I tried all 7 sounds and, while they worked, I wasn’t really impressed. Given the fact that the last release was in 2019, I thought there might be a better solution.** | ||
+ | |||
+ | J'ai revérifié la documentation (le peu qu'il y a), et les instructions sont toujours les mêmes. J'ai donc pensé qu'il y avait peut-être eu un changement dans le programme et que la documentation n' | ||
+ | |||
+ | >>> | ||
+ | [' | ||
+ | |||
+ | Il y avait la commande beep, donc je savais qu'il y avait simplement deux façons de la faire fonctionner. Je pouvais essayer : | ||
+ | |||
+ | from beepy import beep | ||
+ | beep(sound=1) | ||
+ | |||
+ | Et voilà ! On a tous pu apprendre quelque chose ! C'est assez simple à inclure et à coder pour moi, si je voulais attirer l' | ||
+ | |||
+ | import beepy | ||
+ | beepy.beep(sound=1) | ||
+ | |||
+ | Et voilà, c' | ||
+ | |||
+ | L' | ||
+ | |||
+ | Voici les correspondances pour les nombres : 1 : ' | ||
+ | |||
+ | Hmmm. Seulement 7 options. J'ai essayé les 7 sons et, bien qu'ils aient fonctionné, | ||
+ | |||
**Since beepy had only one dependency, I thought I’d try looking at that for something easier and with a bit more options. That dependency is a library called simpleaudio. I ran a search on the Internet and got their github address. https:// | **Since beepy had only one dependency, I thought I’d try looking at that for something easier and with a bit more options. That dependency is a library called simpleaudio. I ran a search on the Internet and got their github address. https:// | ||
Ligne 54: | Ligne 103: | ||
I moved through the documentation, | I moved through the documentation, | ||
+ | |||
+ | Puisque beepy n' | ||
+ | |||
+ | import simpleaudio.functionchecks as fc | ||
+ | |||
+ | fc.LeftRightCheck.run() | ||
+ | |||
+ | Je pensais entendre quelque chose comme « Right speaker » (haut-parleur droit) sortant des haut-parleurs, | ||
+ | |||
+ | J'ai parcouru la documentation, | ||
+ | |||
**I had just found one of those “meant to be embedded” files and downloaded it to my desktop. I copied the code block into the REPL and changed the example path to my real path and, low and behold, it worked as advertised. The first time! | **I had just found one of those “meant to be embedded” files and downloaded it to my desktop. I copied the code block into the REPL and changed the example path to my real path and, low and behold, it worked as advertised. The first time! | ||
Ligne 62: | Ligne 122: | ||
The next bit of code (below) creates numpy arrays containing the values for the sine waves of the three notes.** | The next bit of code (below) creates numpy arrays containing the values for the sine waves of the three notes.** | ||
+ | |||
+ | Je venais de trouver un de ces fichiers « destinés à être intégrés » et je l'ai téléchargé sur mon bureau. J'ai copié le bloc de code dans le REPL et j'ai remplacé le chemin d' | ||
+ | |||
+ | Je venais de trouver l'un de ces fichiers « à intégrer » et l'ai téléchargé sur mon bureau. J'ai copié le bloc de code dans le REPL et j'ai changé le chemin de l' | ||
+ | |||
+ | J'ai creusé un peu plus profondément et j'ai trouvé l' | ||
+ | |||
+ | Eh bien, c'est très familier pour un gars qui a passé 6 ans dans la fanfare de l' | ||
+ | |||
+ | Le bout de code suivant (ci-dessous) crée des tableaux numpy contenant les valeurs des ondes sinusoïdales des trois notes. | ||
+ | |||
**Then, this code (next page, top left) will “bundle up” all the notes and normalize them into a single numpy array. | **Then, this code (next page, top left) will “bundle up” all the notes and normalize them into a single numpy array. | ||
Ligne 77: | Ligne 148: | ||
t = np.linspace(0, | t = np.linspace(0, | ||
+ | |||
+ | Puis, ce code (page suivante, en haut à gauche) va « regrouper » toutes les notes et les normaliser en un seul tableau numpy. | ||
+ | |||
+ | Ensuite, nous introduisons les données audio dans le tampon d' | ||
+ | |||
+ | Sachant que je ne pouvais pas faire de fautes de frappe, puisque j'ai copié le code directement à partir de la documentation, | ||
+ | |||
+ | C' | ||
+ | |||
+ | T = 0.25 | ||
+ | t = np.linspace(0, | ||
+ | |||
+ | Ah ! La partie T * taux d' | ||
+ | |||
+ | t = np.linspace(0, | ||
+ | |||
**When I ran the program again, I was presented with the three tones that I was expecting all along. | **When I ran the program again, I was presented with the three tones that I was expecting all along. | ||
Ligne 100: | Ligne 187: | ||
Each of the white keys are called natural keys. In the image, it starts with middle C (which is C4) and goes up an octave (13 notes) to C5. It’s called middle C because it’s the C key in the middle of the keyboard.** | Each of the white keys are called natural keys. In the image, it starts with middle C (which is C4) and goes up an octave (13 notes) to C5. It’s called middle C because it’s the C key in the middle of the keyboard.** | ||
+ | |||
+ | Lorsque j'ai relancé le programme, j'ai obtenu les trois tons que j' | ||
+ | |||
+ | J'ai essayé d' | ||
+ | < | ||
+ | # calculate the frequencies | ||
+ | A_freq = 440 | ||
+ | Ash_freq = A_freq * 2 ** (1 / 12) | ||
+ | B_freq = A_freq * 2 ** (2 / 12) | ||
+ | C_freq = A_freq * 2 ** (3 / 12) | ||
+ | Csh_freq = A_freq * 2 ** (4 / 12) | ||
+ | D_freq = A_freq * 2 ** (5 / 12) | ||
+ | Dsh_freq = A_freq * 2 ** (6 / 12) | ||
+ | E_freq = A_freq * 2 ** (7 / 12) | ||
+ | F_freq = A_freq * 2 ** (8 / 12) | ||
+ | Fsh_freq = A_freq * 2 ** (9 / 12) | ||
+ | G_freq = A_freq * 2 ** (10 / 12) | ||
+ | Gsh_freq = A_freq * 2 ** (11 / 12) | ||
+ | A5_freq = 880 | ||
+ | </ | ||
+ | Si vous n' | ||
+ | |||
+ | Chacune des touches blanches est appelée touche naturelle. Dans l' | ||
**The black keys are sharps or flats depending on if you are going up the scale or down. So the keys are | **The black keys are sharps or flats depending on if you are going up the scale or down. So the keys are | ||
Ligne 120: | Ligne 230: | ||
np.save(f, audio)** | np.save(f, audio)** | ||
+ | | ||
+ | Les touches noires sont des dièses ou des bémols, selon que vous montez ou descendez la gamme. Les clés sont donc : | ||
+ | |||
+ | Do, Do#, Ré, Ré#, Mi, Fa, Fa#, Sol, Sol#, La, La#, Si et retour au Do. | ||
+ | |||
+ | ...pour un total de treize tonalités. Dans la musique « moderne », on appelle cela la gamme tempérée. Il n'en a pas toujours été ainsi, mais c'est une autre paire de manches, alors je ne m'y attarderai pas. | ||
+ | |||
+ | Sachez simplement que les formules ci-dessus pour générer les treize tons fonctionnent bien. | ||
+ | |||
+ | Vous pouvez trouver le programme pour le faire sous le nom de tones4.py dans le dépôt. | ||
+ | |||
+ | J' | ||
+ | |||
+ | La solution pour sauvegarder l' | ||
+ | |||
+ | # enregistrer l' | ||
+ | |||
+ | with open(" | ||
+ | |||
+ | np.save(f, audio) | ||
+ | | ||
**That’s it. I’ve saved the program as tones4a.py . The file that was generated can be used again in a different program (top right). | **That’s it. I’ve saved the program as tones4a.py . The file that was generated can be used again in a different program (top right). | ||
Ligne 138: | Ligne 269: | ||
play_obj = wave_obj.play() | play_obj = wave_obj.play() | ||
play_obj.wait_done()** | play_obj.wait_done()** | ||
+ | |||
+ | C'est tout. J'ai enregistré le programme sous le nom de tones4a.py . Le fichier qui a été généré peut être réutilisé dans un autre programme (en haut à droite). | ||
+ | |||
+ | Cette solution, cependant, nécessite toujours d' | ||
+ | obj = wave.open(" | ||
+ | obj.setnchannels(1) | ||
+ | obj.setampwidth(2) | ||
+ | obj.setframerate(sample_rate) | ||
+ | obj.writeframesraw(audio) | ||
+ | obj.close() | ||
+ | |||
+ | Cela créera un fichier .wav (sans qu'il soit nécessaire de sauvegarder les données numpy si vous ne le souhaitez pas). Ensuite, pour le lire, tout ce dont vous avez besoin est ce petit bout de code : | ||
+ | |||
+ | import simpleaudio as sa | ||
+ | wave_obj = sa.WaveObject.from_wave_file(" | ||
+ | play_obj = wave_obj.play() | ||
+ | play_obj.wait_done() | ||
+ | |||
**So I modified tones4a.py to include not only the write to a numpy file, but the code to create the .wav file and play it back. | **So I modified tones4a.py to include not only the write to a numpy file, but the code to create the .wav file and play it back. | ||
Ligne 148: | Ligne 297: | ||
Until next time, as always; stay safe, healthy, positive and creative!** | Until next time, as always; stay safe, healthy, positive and creative!** | ||
+ | |||
+ | J'ai donc modifié tones4a.py pour inclure non seulement l' | ||
+ | |||
+ | J'ai également inclus la routine de lecture dans un simple fichier python nommé play_my_wav.py qui lira le fichier « scale1.wav ». Bien sûr, les fichiers numpy et quelques exemples de fichiers .wav sont également dans le dépôt. | ||
+ | |||
+ | Bon, il est temps de conclure pour ce mois-ci. Sachez que la recherche d'une chose aussi simple que de faire sonner votre ordinateur sur commande peut facilement vous conduire sur un chemin où vous pourriez être comme Alice, et « descendre dans le terrier du lapin ». | ||
+ | |||
+ | J'ai mis le fichier .wav pour Beepy ,ainsi que le fichier de test pour simpleaudio, | ||
+ | |||
+ | Jusqu' | ||
+ |
issue190/python.1677503982.txt.gz · Dernière modification : 2023/02/27 14:19 de d52fr