Real-time sonification/el: Difference between revisions
(Created page with "Στην επιστήμη των υπολογιστών, ο τύπος δεδομένων Bool, ή λογικής, είναι μια θεμελιώδης πρωτότυπη τιμή που μπορεί να λάβει μία από τις δύο πιθανές τιμές: αληθής ή ψευδής, που συχνά αναπαρίστανται ως 1 ή 0. Για να απεικονίσουμε αυτή την έννοια, θα μετατρέψουμε σε ήχο...") |
(Created page with "Το παρακάτω υλοποιεί τη μετατροπή ενός αισθητήρα Bool σε ήχο χρησιμοποιώντας το micro:Bit, εστιάζοντας συγκεκριμένα στο κουμπί Α. Όταν πατηθεί το κουμπί, θα ακούσουμε τη νότα Ντο, και όταν απελευθερωθεί, η νότα θα αλλάξει σε Φα. Αυτή η ακουστική ανατροφοδότηση παρέχε...") |
||
| Line 71: | Line 71: | ||
Στην επιστήμη των υπολογιστών, ο τύπος δεδομένων Bool, ή λογικής, είναι μια θεμελιώδης πρωτότυπη τιμή που μπορεί να λάβει μία από τις δύο πιθανές τιμές: αληθής ή ψευδής, που συχνά αναπαρίστανται ως 1 ή 0. Για να απεικονίσουμε αυτή την έννοια, θα μετατρέψουμε σε ήχο τον απλούστερο τύπο δεδομένων, δεδομένων λογικής (του Bool). Συνηθισμένα παραδείγματα αισθητήρων που παράγουν λογικά δεδομένα περιλαμβάνουν αισθητήρες παρουσίας, αισθητήρες επαφής, διακόπτες και κουμπιά. | Στην επιστήμη των υπολογιστών, ο τύπος δεδομένων Bool, ή λογικής, είναι μια θεμελιώδης πρωτότυπη τιμή που μπορεί να λάβει μία από τις δύο πιθανές τιμές: αληθής ή ψευδής, που συχνά αναπαρίστανται ως 1 ή 0. Για να απεικονίσουμε αυτή την έννοια, θα μετατρέψουμε σε ήχο τον απλούστερο τύπο δεδομένων, δεδομένων λογικής (του Bool). Συνηθισμένα παραδείγματα αισθητήρων που παράγουν λογικά δεδομένα περιλαμβάνουν αισθητήρες παρουσίας, αισθητήρες επαφής, διακόπτες και κουμπιά. | ||
Το παρακάτω υλοποιεί τη μετατροπή ενός αισθητήρα Bool σε ήχο χρησιμοποιώντας το micro:Bit, εστιάζοντας συγκεκριμένα στο κουμπί Α. Όταν πατηθεί το κουμπί, θα ακούσουμε τη νότα Ντο, και όταν απελευθερωθεί, η νότα θα αλλάξει σε Φα. Αυτή η ακουστική ανατροφοδότηση παρέχει μια σαφή αναπαράσταση της κατάστασης του κουμπιού, ενισχύοντας την κατανόησή μας για τα δεδομένα Bool σε ένα πρακτικό πλαίσιο.<ref name="code" group="Note"/>. | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
Revision as of 19:54, 1 April 2026
Η ηχοποίηση σε πραγματικό χρόνο είναι μια συναρπαστική τεχνική που μπορεί να ενισχύσει σημαντικά τη συμμετοχή των μαθητών στους τομείς STEAM. Η ηχοποίηση σε πραγματικό χρόνο σημαίνει ότι, λόγω της ταχύτητας της διαδικασίας, δεν μπορούμε να αντιληφθούμε το χρονικό διάστημα που μεσολαβεί μεταξύ της συλλογής των δεδομένων και της αντίστοιχης ήχου που παράγεται από τη συσκευή ηχοποίησης. Επιπλέον, οι μέθοδοι δημιουργίας ηχητικών αναπαραστάσεων των δεδομένων καθορίζονται ταυτόχρονα με τη συλλογή των δεδομένων (σε «πραγματικό χρόνο»).
Πριν ξεκινήσουμε, θέλουμε να τονίσουμε ότι η ποιότητα του ήχου, η οποία είναι υποκειμενική και επομένως εξαρτάται από το γούστο του χρήστη, πρέπει να είναι τέτοια ώστε τουλάχιστον να μην ενοχλεί τον χρήστη. Αντίθετα, θα είναι καλύτερα, αν είναι αρκετά ελκυστική ώστε να προσελκύει την προσοχή τού χρήστη. Από την άλλη πλευρά, όταν προσπαθούμε να κάνουμε κάτι «ευχάριστο», υπάρχει ο κίνδυνος να δημιουργηθούν ηχητικά αποτελέσματα που δεν εκπληρώνουν τον στόχο της καλής περιγραφής της συμπεριφοράς των δεδομένων εισόδου. Είναι επομένως απαραίτητο να βρεθεί ένας συμβιβασμός: ο ήχος πρέπει να είναι αρκετά ευχάριστος, αλλά και πλήρως ενημερωτικός.
Συσκευές Ηχοποίησης σε πραγματικό χρόνο

Για τη δημιουργία μιας συσκευής ηχοποίησης σε πραγματικό χρόνο, είναι χρήσιμο να χρησιμοποιηθεί ένας μικροελεγκτής. Αυτοί είναι σαν «μικροί και απλοί υπολογιστές» με μία μόνο μονάδα επεξεργαστή. Ωστόσο, δεν είναι υπολογιστές. Η αρχιτεκτονική τους είναι πολύ απλούστερη και δεν μπορούν να εκτελέσουν λειτουργικό σύστημα. Παρ’ όλα αυτά, μπορούν να προγραμματιστούν ώστε να εκτελούν ένα μόνο πρόγραμμα κάθε φορά, το οποίο μπορεί να εκτελεί πολλαπλές εργασίες, αλλά διαδοχικά, σύμφωνα με τη σειρά των εντολών που αναφέρονται στο πρόγραμμα. Υπάρχουν διάφοροι τύποι μικροελεγκτών, με τον Arduino (arduino.cc) να είναι ο πιο δημοφιλής.
Αρχικά, το έργο SoundScapes προτείνει τη χρήση του μικροελεγκτή BBC micro:bit. Αυτό το εργαλείο είναι πολύ απλό στη χρήση, ευέλικτο και περιλαμβάνει διάφορους ενσωματωμένους αισθητήρες έτοιμους προς χρήση, εξαλείφοντας την ανάγκη κατασκευής ενός συγκεκριμένου ηλεκτρικού κυκλώματος για τη λειτουργία του. Το micro:bit μπορεί να προγραμματιστεί διαδικτυακά με το Makecode (χρησιμοποιώντας τον πλοηγό Chrome browser για καλύτερη συμβατότητα) σε python, javascript ή μπλοκς.
Ηχοποίηση με micro:bit
Πριν ξεκινήσετε την ηχοποίηση με το micro:bit, πρέπει πρώτα να εξοικειωθείτε με το περιβάλλον προγραμματισμού Makecode. Στην κεντρική σελίδα υπάρχουν διάφορα εκπαιδευτικά βίντεο, όπως το «Flashing Heart», το «Name Tag» κ.λπ., από τα οποία μπορείτε να επιλέξετε για να ξεκινήσετε. Εάν εγγραφείτε στην πλατφόρμα, τα έργα σας θα αποθηκευτούν στον λογαριασμό σας και θα μπορείτε να έχετε πρόσβαση σε αυτά από οποιαδήποτε συσκευή, αρκεί να συνδεθείτε. Διαφορετικά, αποθηκεύονται ούτως ή άλλως ως cookies, ωστόσο, μπορεί να τα χάσετε εάν καθαρίσετε την προσωρινή μνήμη του προγράμματος περιήγησής σας.
Έννοιες ήχου στο micro:bit
Στον επεξεργαστή Makecode, Makecode editor, υπάρχει μια χρήσιμη και ελκυστική βιβλιοθήκη αφιερωμένη στη μουσική, ειδικά για νεαρούς μαθητές. Αυτή η μουσική βιβλιοθήκη music library προσφέρει διάφορα μπλοκς που διευκολύνουν τη δημιουργία ήχων και μελωδιών. Υπάρχουν πολλά μπλοκς και συνδυασμοί μπλοκς που μπορείτε να χρησιμοποιήσετε για να δημιουργήσετε διαφορετικά είδη ήχων. Εδώ σας παρουσιάζουμε τα πιο βασικά μπλοκς και προχωράμε σε πιο σύνθετα παραδείγματα. Είναι μια καλή άσκηση να πειραματιστείτε με τα διαφορετικά μπλοκς και να ακούσετε τι συμβαίνει για να εξοικειωθείτε μαζί τους.

Δημιουργία ενός μεμονωμένου ήχου
Ο παρακάτω κώδικας δημιουργεί έναν μεμονωμένο ήχο με προκαθορισμένη συχνότητα «Μεσαίο Ντο» και διάρκεια 1 κτύπου όταν πατηθεί το κουμπί Α, ή έναν συνεχή ήχο «Μεσαίο Μι» όταν πατηθεί το κουμπί Β. Είναι δυνατό να αλλάξετε τη συχνότητα των τόνων, κάνοντας κλικ στα λευκά πεδία εισαγωγής με τις τιμές «Μεσαίο Ντο» και «Μεσαίο Μι». Από τα βέλη του αναπτυσσόμενου μενού, είναι επίσης δυνατό να αλλάξετε τη διάρκεια του παλμού του «Μεσαίου Ντο» καθώς και το αν ο ήχος θα αναπαραχθεί διαδοχικά με άλλα μπλοκς, στο παρασκήνιο ή σε επανάληψη. [Note 1].
Αναπαραγωγή μελωδίας
Για να αναπαράγετε μια μελωδία, χρησιμοποιήστε το παρακάτω σύστημα και κάντε κλικ πάνω του για να δημιουργήσετε τη μελωδία:

To παρακάτω παράδειγμα κώδικα αναπαράγει δύο μελωδίες με διαφορετικές τιμές χτύπων ανά λεπτό για τα κουμπιά A και B και σταματά όλους τους ήχους όταν πατηθούν ταυτόχρονα τα κουμπιά A και B. Μπορείτε να αλλάξετε τις μελωδίες κάνοντας κλικ στα λευκά πεδία εισαγωγής με τις πολύχρωμες μουσικές νότες. Όπως και στο προηγούμενο παράδειγμα, μπορείτε επίσης να αλλάξετε τη διάρκεια του ρυθμού και να ορίσετε αν ο ήχος θα αναπαραχθεί διαδοχικά με άλλα μπλοκς, στο παρασκήνιο ή σε επανάληψη.[Note 1]
Επεξεργασία της μεταβολής της συχνότητας, της κυματομορφής, της έντασης και της διάρκειας
Είναι επίσης δυνατό να δημιουργηθούν πιο σύνθετοι ήχοι μέσω της επεξεργασίας της μεταβολής της συχνότητας, της κυματομορφής, της έντασης και της διάρκειας με το ακόλουθο σύστημα:

Το παρακάτω παράδειγμα αναπαράγει δύο σύνθετους ήχους διαδοχικά, χωρίς διακοπή [Note 1]:
Ηχοποίηση μιας λογικής τιμής
Στην επιστήμη των υπολογιστών, ο τύπος δεδομένων Bool, ή λογικής, είναι μια θεμελιώδης πρωτότυπη τιμή που μπορεί να λάβει μία από τις δύο πιθανές τιμές: αληθής ή ψευδής, που συχνά αναπαρίστανται ως 1 ή 0. Για να απεικονίσουμε αυτή την έννοια, θα μετατρέψουμε σε ήχο τον απλούστερο τύπο δεδομένων, δεδομένων λογικής (του Bool). Συνηθισμένα παραδείγματα αισθητήρων που παράγουν λογικά δεδομένα περιλαμβάνουν αισθητήρες παρουσίας, αισθητήρες επαφής, διακόπτες και κουμπιά.
Το παρακάτω υλοποιεί τη μετατροπή ενός αισθητήρα Bool σε ήχο χρησιμοποιώντας το micro:Bit, εστιάζοντας συγκεκριμένα στο κουμπί Α. Όταν πατηθεί το κουμπί, θα ακούσουμε τη νότα Ντο, και όταν απελευθερωθεί, η νότα θα αλλάξει σε Φα. Αυτή η ακουστική ανατροφοδότηση παρέχει μια σαφή αναπαράσταση της κατάστασης του κουμπιού, ενισχύοντας την κατανόησή μας για τα δεδομένα Bool σε ένα πρακτικό πλαίσιο.[Note 1].
Detailed explanation of the code:
The blocks are evaluated sequentially from the top to the bottom within the loop block forever which repeats the following evaluation sequence until something stops the program:
- Set the variable X to the button state (true or false whether the button is pressed by the time of the pink block button A is pressed evaluation)
- If the variable/condition X holds true (the button was pressed), ring tone (Hz) Middle C, else, ring tone (Hz) Middle E
Sonification of a range of values (using input sensors)
Most sensors provide a range of values, not just 0 or 1, in which case we must first find out what the lowest and highest possible values are before defining the mapping for sonification. This variable input from the sensor can originate from the light level sensor, the accelerometer, the magnetometer, the intensity of the sound captured by the microphone, or other sensors connected to the micro:bit through the pins. This data can easily be collected by the microcontroller.
Change pith with fixed rhythm
In this example, we show how to map the light level to a frequency range. The internal light sensor of the micro:bit provides a value between 0 (dark) and 255 (very bright). We call this input value variable x. We also define the variables x-Min and x-Max with the minimum and maximum values of our sensor. For the purpose of sonifying the measured light level, we will map the value of the light level to a pitch between 200 Hz (minimum value) and 2000 Hz (maximum value), played at a fixed rhythm [Note 1].
Detailed explanation of the code:
The blocks within the on start block are evaluated sequentially before anything else in the program when the micro:bit is turned on.
- Set the x-Min variable to the light level lowest possible measured value 0.
- Set the x-Max variable to the light level highest possible measured value 255.
The blocks within the block forever are evaluated sequentially in a loop from top to bottom after the on start sequence:
- Set the x variable to the measured light level
- Play a one 1 beat tone with a frequency resulting from mapping the x value (in the x-Min to x-Max range) to the chosen frequency range in the map block.
Change rhythm with fixed pitch
Another option is to maintain a fixed pitch while varying the rhythm based on the light level. We can achieve this by playing a short-duration note and introducing pauses that vary in length, ranging from 1000 ms (for dark conditions) to 20 ms (for very bright conditions). This approach allows for a dynamic auditory representation of the changing light levels [Note 1].
Detailed explanation of the code:
The blocks within the on start block are evaluated sequentially before anything else in the program when the micro:bit is turned on.
- Set the x-Min variable to the light level lowest possible measured value 0.
- Set the x-Max variable to the light level highest possible measured value 255.
The blocks within the block forever are evaluated sequentially in a loop from top to bottom after the on start sequence:
- Set the x variable to the measured light level
- Play a one 1 beat High D tone.
- Pause for a period calculated from mapping the x value (in the x-Min to x-Max range) to the chosen time range in the map block.
Reminder: You can replace the light level input block with any other micro:bit sensor input block (or any other sensors connected to the micro:bit through the pins) that provide a range of values. Just be sure, to redefine the x-Min and x-Max values accordingly, as the accelerometer and the compass heading, for instance, work on a different range.
Using external input sensors
To use an external digital/analog sensor on a micro pin or using for instance the I2C protocol (all of these blocks can be found under the advanced categories) you can use the same programs but simply replace the light level input block with the corresponding block as follows:



Attention to the pin number or the i2c address!
Multiple inputs mapped to a single sound
Sonification systems often serve to provide more than one piece of information. We can map as many variables as the amount of sound parameters we can control. As long as the sound does not become confusing due to the multiple sound layers playing simultaneously. If we consider that a philharmonic orchestra can have over one hundred elements we have some room for overlaying several sounds. Opposite to the visual stimuli where we cannot exceed a certain number, usually inferior to that of audio stimuli. Finally, like in the orchestra, the sounds have to be carefully arranged together in case of large numbers.
The following sonifies the light level mapped to pith with a pause detefined by the compass heading mapped to milliseconds [Note 1].
The SoundScapes sonification extension for micro:bit
In all the previous examples, numbers were mapped to a continuous range of frequencies, which is great! But does it sound appealing? To enhance the auditory experience, you can map numbers to a musical scale. The SoundScapes sonification extension for MakeCode micro:bit makes this type of mapping easy and accessible.
The following shows how to install the extension:

Map and play directly from a micro:bit sensor
To map and play directly from a micro:bit sensor you can use the following block with a dropdown menu for choosing the sensor. The input range is automatically selected to match the minimum and maximum values that can be obtained from the micro:bit sensors.
Although the hard work is behind the curtains, this makes it more challenging for you to innovate in sonification :)
This example is equivalent to the real-time sonification example using the sonification map function for single value as follows.
Map and play a single value on a music scale
The map function returns an integer number from mapping a number on a certain range [low, high] to a specified music scale on a specified number of octaves. For instance, the following example maps the light level value on the range [0,255] to Middle C Major on 1 octave and plays it for 500 ms forever:
Other sensors (including external sensors connected through pins to the micro:bit) and different input ranges can be used as well. This is useful for real-time sonification, when you sonify the data at the same time you collect it.
For instance, the following example maps the light level value on the range [0,255] to Middle C Major on 1 octave and plays it for 500 ms forever:
Other sensors (including external sensors connected through pins to the micro:bit) and different input ranges can be used as well. This is useful for real-time sonification, when you sonify the data at the same time you collect it.
Map and play on a custom scale
You can easily create your own music scales with arrays and serve them as input to the map functions to map and play any number value on your custom scale. The input array must contain the frequency ratios relative to the root frequency.
For instance, the following maps the light level value on the range [0,255] to Middle C harmonic on 1 octave and plays it for 500 ms:
where harmonic is an array of numbers containing the frequency ratios of the harmonic scale. Since each tone in the harmonic scale is exactly one octave apart from the previous tone, changing the octave number in this particular case will just expand the range of the harmonic series.
Sonification via MIDI (The micro:bit as a MIDI instrument)
The sound produced by the speaker (buzzer) of the micro:bit has little power and does not play low frequencies. The micro:bit is also very limited in its capacity to generate multiple sounds simultaneously and sounds with more complex timbres. In the last example, we used a "trick" to sonify values of multiple inputs. We used the pause (duration of silence between consequent sounds) as a sonification output. Smart but what we would really enjoy would be several sounds simultaneously playing and expressing several layers of data. We can obtain better sound quality and play more instruments at the same time using the midi protocol.
MIDI is a protocol that facilitates real-time communication between electronic musical instruments. MIDI stands for Musical Instrument Digital Interface and it was developed in the early ’80s for storing, editing, processing, and reproducing sequences of digital events connected to sound-producing electronic instruments, especially those using the 88-note chromatic compass of a piano-keyboard. We can roughly, but easily, understand MIDI as the advanced successor of the “piano rolls”, which, more than a century ago, were perforated papers or pinned cylinders, in which music performances were either recorded (in real-time) or notated (in step time). These paper-rolls were then played automatically by specially designed mechanical instruments, the mechanical pianos (pianolas) or music machines, using them as their “program”.
Setup the MIDI
The following video explains in detail how to connect the micro:bit to your DAW (Digital Audio Workstation) or digital synthesizer through MIDI on Windows:
Step-by-step instructions (see the video):
- Install the MIDI Extension for Makecode.
- Create a very basic program using the MIDI extension to test your setup.
- Install Hairless MIDI, open it, and from serial port drop-down menu select the com port (USB port) to which the micro:bit is connected to.
- Install loopMIDI, open it, and click the + button at the bottom-left corner to create a new virtual port.
- Go back to the HairlessMIDI window and on the MIDI out drop-down menu select loopMIDI port
- You might need to unplug and plug in the micro:bit again for it to work.
- You are ready to play!
How it works: The micro:bit sends MIDI messages through serial communication. These messages are then received by Hairless MIDI, which forwards them to LoopMIDI. Acting as a virtual MIDI port, LoopMIDI makes the MIDI messages accessible to computer software/web apps (like DAWs or digital synthesizers) that receive these messages and generate the corresponding sounds, completing the connection.
There are plenty of free (and some open-source, cross-platform) DAW stations like LMMS that you can download and configure to play MIDI input. The easiest method is to play directly from the browser through a web app such as midi.city, the Online Sequencer and many others to discover online. In principle, web apps such as midi.city will readily detect your midi instrument (the micro:bit in this case) and you are ready to play after giving the browser permissions to access your device (which you will be asked to do).
MIDI is a powerful tool for sonification because it allows you to control a wide range of sound parameters, such as pitch, volume, and timbre. This setup allows for multiple Microbits to send MIDI data to a single synthesizer, enabling synchronized sonification of multiple data streams. It also allows a single micro:bit to send MIDI data over multiple MIDI chanels.
Note: On Linux install ttymidi instead of hairlesMIDI and loopMIDI.
Sensor data over MIDI
Previous examples using sensor data can be adapted to send data over MIDI with the Makecode MIDI extension, meaning that the sounds will play not on the micro:bit but through a properly configured computer software/web application. The following example maps the light level to MIDI notes and sends them through MIDI channel 1 [Note 1].
Detailed explanation of the code:
The blocks inside the on start block are evaluated sequentially before anything else in the program when the micro:bit is turned on.
- Show a fancy musical note icon on the LED screen just to make it nicer.
- Set the Instrument_1 variable to midi channel 1. Thus any changes to the variable Instrument_1 are actions on the MIDI channel 1.
- midi use raw serial is what will get the micro:bit to "talk" to the MIDI output device.
The blocks within the block forever are evaluated sequentially in a loop from top to bottom after the on start sequence:
- Set the Note variable to a MIDI note by mapping the light level range of possible values to the chosen MIDI range 40 to 85 (within 0 and 128) using the map block.
- Set the sound volume of Instrument_1 (on MIDI channel 1) to 100.
- Play MIDI note Note (measured light level mapped to MIDI) with Instrument_1 (on MIDI channel 1).
- Pause for 250 ms.
- Stop playing the MIDI note Note.
- Pause for 100 ms.
Using multiple MIDI channels
This example maps the light level to MIDI and uses multiple MIDI channels allowing one to choose to play the notes either with a button or by shaking the micro:bit [Note 1].
Detailed explanation of the code:
The logic behind this example is very similar to the previous one. However, an extra MIDI channel 10 (it could have been any other number between 1 and 16) is set on start as variable Instrument_2. Thus, any changes on this variable are actions on the MIDI channel 10. The mapping of the light level to MIDI is still set within the loop, but the Instrument_1 related blocks and pauses were moved to the input block on button B pressed. The input block on shake just repeats the same code for Instrument_2. Note, that when you play a note, irrespectively of the instrument chosen, a musical note appears and disappears from the LED screen.