Procházet zdrojové kódy

add osci music player project

Thomas Buck před 9 měsíci
rodič
revize
fe3a19de69

+ 280
- 0
input/projects/osci_music_player.md Zobrazit soubor

@@ -0,0 +1,280 @@
1
+title: Osci Music Player
2
+description: Properly playing sound files for visualization on an oscilloscope
3
+parent: projects
4
+git: https://git.xythobuz.de/thomas/osci-music-player
5
+date: 2024-02-11
6
+comments: true
7
+---
8
+
9
+Soon our local hackerspace [Toolbox Bodensee](https://toolbox-bodensee.de/) has its [10-year anniversary](https://toolbox-bodensee.de/blog/ankuendigung-tag-der-offenen-tuer-2024/), and it will also be present at the [IBO fair 2024 in Friedrichshafen](https://www.ibo-messe.de/).
10
+Some five or six years ago I already prepared a small setup at one of the Toolbox open-door-days with an oscilloscope and a laptop playing [Jerobeam Fendersons Oscilloscope Music](https://oscilloscopemusic.com/).
11
+But it never looked quite right.
12
+So I decided to tackle the topic again this year.
13
+
14
+<!--%
15
+lightgallery([
16
+    [ "img/osci_music_7.jpg", "'Reconstruct' with the Osci Music Player" ],
17
+    [ "img/osci_music_2.jpg", "Front of Osci Music Player" ],
18
+    [ "img/osci_music_8.mp4", "video/mp4", "", "", "'Function' with the Osci Music Player" ],
19
+])
20
+%-->
21
+
22
+### Table Of Contents
23
+
24
+Want to build your own player?
25
+Skip to the [interesting part](osci_music_player.html#hardware) then!
26
+
27
+* [Introduction](osci_music_player.html#introduction)
28
+* [Artists](osci_music_player.html#artists)
29
+* [Playback](osci_music_player.html#playback)
30
+* [Oscilloscope Settings](osci_music_player.html#oscisettings)
31
+* [Hardware](osci_music_player.html#hardware)
32
+* [Software](osci_music_player.html#software)
33
+
34
+## Introduction
35
+<a class="anchor" name="introduction"></a>
36
+
37
+An oscilloscope usually displays one or more waveforms of an electrical signal.
38
+To achieve this a single dot is moved across the screen, with the voltage or amplitude of the signal controlling the vertical deflection (Y axis), and time controlling the horizontal deflection (X axis), meaning the dot automatically moves from left to right with a pre-configured speed.
39
+But most oscilloscopes can also be configured in something called `XY mode`, where the horizontal deflection is no longer controlled by time, but instead by another voltage.
40
+This turnes the oscilloscope into a vector display, where a single dot can be moved across the screen.
41
+Note that the brightness can not be controlled, it is fixed.
42
+Some oscilloscopes have a third input that can be used for that, but this is not needed here.
43
+
44
+A common way to achieve digital to analog conversion (DAC) of a signal, with hardware usually available in every household, is using the soundcard of a PC.
45
+The stereo line output channels are basically connected directly to a DAC chip, with only some filtering hardware in between.
46
+
47
+Now combining these concepts, one can connect the two audio output channels of a PC or similar device to the two inputs of an oscilloscope in XY mode.
48
+This enables a kind of visualization of music, and was also used in the past in something called "audio vectorscope" to check the balance of the channels.
49
+If you have a mono signal, with both channels having the exact same contents, the scope will display a 45-degree angled line.
50
+If only one channel has contents, and the other is silent, you will see either a horizontal or a vertical line, depending on which channel is active.
51
+When one channel outputs a sine, and the other a cosine, with the same frequency and phase, you will see a circle.
52
+
53
+Of course with normal music this doesn't look that interesting.
54
+But what if you were to specifically compose music to look good on such a device?
55
+
56
+That's what **Oscilloscope Music** is.
57
+
58
+<!--%
59
+lightgallery([
60
+    [ "img/osci_music_9.mp4", "video/mp4", "", "", "'Reconstruct' with the Osci Music Player" ],
61
+])
62
+%-->
63
+
64
+## Artists
65
+<a class="anchor" name="artists"></a>
66
+
67
+To generate the proper sounds you usually need some kind of software or self-written code.
68
+Many people have already experimented with this, with small scripts running on a PC, or directly on a microcontroller.
69
+
70
+In 2016 [Jerobeam Fenderson released an album](https://oscilloscopemusic.com/watch/oscilloscope_music) based on this concept.
71
+He also collaborated with [Hansi Raber](https://github.com/kritzikratzi/) and they released [some software to play and generate this music](https://oscilloscopemusic.com/software/).
72
+You can also find high-quality recordings on [his YouTube channel](https://www.youtube.com/@jerobeamfenderson1).
73
+
74
+<!--%
75
+lightgallery([
76
+    [ "img/osci_music_cats_1.mp4", "video/mp4", "", "", "Cats listening to Jerobeam Fenderson" ],
77
+    [ "https://www.youtube.com/watch?v=5WBWIKnr0Os", "Reconstruct - Jerobeam Fenderson" ],
78
+    [ "https://www.youtube.com/watch?v=ywdRQ3zU6Uc", "Function - Jerobeam Fenderson" ],
79
+])
80
+%-->
81
+
82
+I also found [Chris Allen on YouTube](https://www.youtube.com/channel/UCSb9_amN9Oh2WJhDTwnG3NA), another artist that offers a [download of the original files](https://bit.ly/OscMusic).
83
+
84
+<!--%
85
+lightgallery([
86
+    [ "img/osci_music_cats_2.mp4", "video/mp4", "", "", "Cats listening to Chris Allen" ],
87
+    [ "https://www.youtube.com/watch?v=FTHoDWog8qQ", "Moon Patrol De-Rastered - C. Allen" ],
88
+    [ "https://www.youtube.com/watch?v=_6a_nz4uRd0", "72 Pantera - C. Allen" ],
89
+])
90
+%-->
91
+
92
+## Playback
93
+<a class="anchor" name="playback"></a>
94
+
95
+So there are some technical challenges involved in playing these properly on an oscilloscope.
96
+
97
+First, it is not enough to play these from YouTube or small MP3 files, as the compression algorithms remove too much of the visual contents of the music.
98
+So original WAV file downloads are required.
99
+
100
+Second, most audio outputs are `AC coupled` instead of `DC coupled`.
101
+What does that mean?
102
+Audio is normally an AC signal without any DC bias.
103
+The spekaer membrane is more or less constantly moving around it's resting position.
104
+If there were a DC offset, this would mean the membrane has a constant offset to one side, but would then vibrate there as usual.
105
+So the sound waves generated are the same.
106
+That's why normally theres a capacitor in line with the audio signal, blocking the DC contents.
107
+But when you consider our oscilloscope, if you want to draw a straight line with an offset from the center of the screen, this looks like a DC bias on the signal, which would be filtered out by the capacitor, causing the line to appear in the middle of the screen instead.
108
+So the impact of this depends on the image shown. If it is symmetrical around the center of the screen, not much will be distorted, but in other cases the image can be completely unrecognizable.
109
+
110
+Third, the audio files have a sample rate of 192kHz.
111
+It's usually assumed that average humans don't hear sounds above 20kHz, so according to Shannon, a sample rate of 40kHz is enough to capture all sounds interesting for humans.
112
+But to draw many interesting shapes in a short time, higher frequencies are needed.
113
+So you need a fast 192kHz DAC and the proper settings in your OS or player software to actually output this without downsampling happening somewhere in the chain.
114
+
115
+So how can you achieve these in practice?
116
+Interestingly, most Apple devices have a DC coupled output.
117
+So when using these, you already get pretty close to the intended output.
118
+
119
+We also tried a "proper" [Behringer UMC404HD](https://www.behringer.com/product.html?modelCode=P0BK1) USB audio interface from the [Toolbox Sound Studio](https://toolbox-bodensee.de/projekte/tonstudio/), but the output was the worst of all devices tried.
120
+Even using random cheap AC coupled phone or laptop DACs looked way better.
121
+
122
+But the best solution came in the form of the [Hifiberry DACs](https://www.hifiberry.com/dacs).
123
+These are DC coupled, support 192kHz and are cheap and easy to get.
124
+
125
+Now the image finally looks like in the YouTube videos.
126
+The only problem is, this is supposed to be used and operated at Toolbox events, so it needs to be easy to use and kind of sturdy.
127
+
128
+## Oscilloscope Settings
129
+<a class="anchor" name="oscisettings"></a>
130
+
131
+To get the proper picture out of your oscilloscope you need to set it up correctly.
132
+Every oscilloscope has its own kind of front panel buttons, but in general they all have more or less the same kind of settings.
133
+
134
+You need to set the voltage range of both inputs you're using, usually `CH1` and `CH2`, to the same value, something like `0.5V` or `0.2V`.
135
+This changes the size of the image on the screen, just like changing the volume on the Raspberry Pi does, too.
136
+My script has an option on top for the volume percentage.
137
+You need to adjust these values so the image properly fills your screen, without drawing outside of its borders.
138
+The output of the Pi should be put as high as possible, to reduce the influence of electrical noise.
139
+Then set the proper range of the oscilloscope, which usually has pretty big steps, so do the final fine-adjustment with the percentage in the script.
140
+
141
+Both input channels should be set to `DC coupling`.
142
+
143
+And you need to put the scope in `XY mode`.
144
+
145
+Put the intensity as high as needed, but as low as possible, to maximize the lifetime of the phosphor coating of the tube.
146
+
147
+Adjust the focus so you have the sharpest possible image.
148
+
149
+Use the horizontal and vertical position controls to center the image on the screen.
150
+
151
+In the case of my Tektronix 2215A, the `XY` mode hides as one of the steps of the right-most rotational input, the timebase or `A and B SEC/DIV`.
152
+In the picture below I marked all settings you may need to adjust.
153
+
154
+<!--%
155
+lightgallery([
156
+    [ "img/osci_music_6.jpg", "My Tektronix 2215A set up for Oscilloscope Music" ],
157
+])
158
+%-->
159
+
160
+## Hardware
161
+<a class="anchor" name="hardware"></a>
162
+
163
+We need to consider the environments this device will be used in.
164
+Both at the Toolbox anniversary, as well as the IBO fair, it will be displayed as part of a public show, with many people walking by and maybe stopping for a short time.
165
+And I will not be there all the time, so it will be operated by other Toolbox members (setting it up, turning it on or off), or even the visitors (switching tracks).
166
+
167
+With the part selection outlined above, the solution was kind of obvious.
168
+Have a script on the Pi, automatically starting playback of the music files, with a small OLED and some buttons to control playback, and ideally with some kind of UPS for ease of powering the device and properly shutting down to not corrupt the SD card.
169
+
170
+<!--%
171
+lightgallery([
172
+    [ "img/osci_music_1.jpg", "Top of Osci Music Player" ],
173
+    [ "img/osci_music_3.jpg", "Insides of Osci Music Player" ],
174
+])
175
+%-->
176
+
177
+Here are the parts I used and the prices I paid at the time of building this.
178
+
179
+<!--%
180
+tableHelper([ "align-right", "align-last-right", "align-right monospaced"],
181
+    [ "Part", "Description", "Cost" ], [
182
+        [ "Pi", ("Raspberry Pi Zero 2 W", "https://www.raspberrypi.com/products/raspberry-pi-zero-2-w/"), "24.49€" ],
183
+        [ "DAC", ("HiFiBerry DAC+ Zero", "https://www.hifiberry.com/shop/boards/hifiberry-dac-zero/"), "30.99€" ],
184
+        [ "Batt", ("PiSugar 2", "https://www.pisugar.com/"), "35.99€" ],
185
+        [ "OLED", ("SSD1306 I2C 128x64", "https://www.az-delivery.de/en/products/0-96zolldisplay"), "4.40€" ],
186
+        [ "Btns", ("2x 12mm momentary switch", "https://www.amazon.de/dp/B0811QKG1R"), "1.60€" ],
187
+        [ "Conn", ("2x Cinch panel mount", "https://www.amazon.de/dp/B0B4SG1JM6"), "1.60€" ],
188
+        [ "Amp", ("Stereo amplifier module", "https://www.amazon.de/dp/B07KQCKWF8"), "1.40€" ],
189
+        [ "Spkr", ("Stereo speakers", "https://www.amazon.de/dp/B08QFTYB9Z"), "5.00€" ],
190
+        [ "Ext", ("Micro-USB panel mount", "https://www.amazon.de/dp/B06XZ2NFP1"), "7.69€" ],
191
+        [ "BNC", ("Cinch to BNC adapter", "https://www.amazon.de/dp/B08L8YBWMY"), "0.50€" ],
192
+        [ "Pot", ("Volume knob", "https://www.amazon.de/dp/B08214YZDS"), "0.85€" ],
193
+        [ "Cable", "Cinch stereo cable", "~2.00€" ],
194
+        [ "Case", "Lunchbox from supermarket", "~5.00€" ],
195
+        [ "", "Sum", "85.52€" ]
196
+    ]
197
+)
198
+%-->
199
+
200
+Of course you also need some bits of copper wire for the internal connections inside the device.
201
+I also used a bunch of male and female 2.54mm pin headers to be able to disconnect stuff.
202
+Also M2.5 spacers to properly mount the PCBs around the Pi Zero, M2 spacers for the OLED, as well as a couple of cable-ties.
203
+
204
+<!--%
205
+lightgallery([
206
+    [ "img/osci_music_4.jpg", "Audio output of Osci Music Player" ],
207
+    [ "img/osci_music_5.jpg", "Power input of Osci Music Player" ],
208
+])
209
+%-->
210
+
211
+For this project I decided to use a cheap plastic lunchbox as a case.
212
+This worked relatively well.
213
+Just take care with the lid, it was quite hard in my case, causing it to crack when I tried to drill a hole that was too big.
214
+But some superglue saved the day.
215
+
216
+## Software
217
+<a class="anchor" name="software"></a>
218
+
219
+I've been using the [Raspberry Pi OS (Legacy, 32bit) Lite](https://downloads.raspberrypi.com/raspios_oldstable_lite_armhf/images/raspios_oldstable_lite_armhf-2023-12-06/2023-12-05-raspios-bullseye-armhf-lite.img.xz) image.
220
+Install it [as usual](https://www.raspberrypi.com/software/), set up a user account, wireless network connection and SSH login.
221
+
222
+Prepare the environment by installing all required dependencies:
223
+
224
+<pre class="sh_sh">
225
+sudo sh -c 'echo "\n[all]\ndtoverlay=hifiberry-dac" >> /boot/config.txt'
226
+sudo apt-get update
227
+sudo apt-get upgrade
228
+sudo apt-get install python3 python3-pip python3-pil libjpeg-dev zlib1g-dev libfreetype6-dev liblcms2-dev libopenjp2-7 libtiff5 ffmpeg
229
+curl "http://cdn.pisugar.com/release/pisugar-power-manager.sh" | sudo bash
230
+pip install pisugar luma.oled psutil
231
+sudo usermod -a -G spi,gpio,i2c $USER
232
+</pre>
233
+
234
+Reboot after the last command so the new settings take effect and the power manager can do its thing.
235
+If in doubt, also take a look at the [PiSugar 2 manual](https://github.com/PiSugar/PiSugar/wiki/PiSugar2).
236
+
237
+<!-- https://clay-atlas.com/us/blog/2021/06/30/html-en-copy-text-button/ -->
238
+<script>
239
+function copyEvent(id) {
240
+    var str = document.getElementById(id);
241
+    window.getSelection().selectAllChildren(str);
242
+    document.execCommand("Copy")
243
+}
244
+</script>
245
+
246
+Next put the script that controls playback on the device.
247
+Here is [`~/osci-pi.py`](https://git.xythobuz.de/thomas/osci-music-player/raw/branch/master/osci-pi.py).
248
+<button type="button" onclick="copyEvent('oscipipy')" class="clip-btn">Copy to clipboard</button>
249
+
250
+<pre id="oscipipy" class="sh_python">
251
+<!--%
252
+include_url("https://git.xythobuz.de/thomas/osci-music-player/raw/branch/master/osci-pi.py")
253
+%-->
254
+</pre>
255
+
256
+And you'll also need [`/etc/systemd/system/osci.service`](https://git.xythobuz.de/thomas/osci-music-player/raw/branch/master/osci.service) to start the script automatically.
257
+Adjust the username and path accordingly.
258
+<button type="button" onclick="copyEvent('osciservice')" class="clip-btn">Copy to clipboard</button>
259
+
260
+<pre id="osciservice" class="sh_desktop">
261
+<!--%
262
+include_url("https://git.xythobuz.de/thomas/osci-music-player/raw/branch/master/osci.service")
263
+%-->
264
+</pre>
265
+
266
+The `ExecStart` line has the parameters that are passed to the Python script.
267
+This is the path of the directory that contains all the `.wav` files for the songs.
268
+In this top-level folder, I called it `~/music`, put another level of directories named after the artist, then place the files in there.
269
+
270
+Finally enable and start the new unit.
271
+
272
+<pre class="sh_sh">
273
+sudo systemctl daemon-reload
274
+sudo systemctl enable --now osci.service
275
+</pre>
276
+
277
+The device should now start playing the music as soon as the power is turned on.
278
+Before switching the device off, use the on-board button of the PiSugar to properly shutdown the OS, and only then move the power switch to the `Off` position.
279
+
280
+You can also check out [the code in its Git repository](https://git.xythobuz.de/thomas/osci-music-player).

binární
static/img/osci_music_1.jpg Zobrazit soubor


binární
static/img/osci_music_1_small.jpg Zobrazit soubor


binární
static/img/osci_music_2.jpg Zobrazit soubor


binární
static/img/osci_music_2_small.jpg Zobrazit soubor


binární
static/img/osci_music_3.jpg Zobrazit soubor


binární
static/img/osci_music_3_small.jpg Zobrazit soubor


binární
static/img/osci_music_4.jpg Zobrazit soubor


binární
static/img/osci_music_4_small.jpg Zobrazit soubor


binární
static/img/osci_music_5.jpg Zobrazit soubor


binární
static/img/osci_music_5_small.jpg Zobrazit soubor


binární
static/img/osci_music_6.jpg Zobrazit soubor


binární
static/img/osci_music_6_small.jpg Zobrazit soubor


binární
static/img/osci_music_7.jpg Zobrazit soubor


binární
static/img/osci_music_7_small.jpg Zobrazit soubor


binární
static/img/osci_music_8.mp4 Zobrazit soubor


binární
static/img/osci_music_8_poster.png Zobrazit soubor


binární
static/img/osci_music_8_thumb.png Zobrazit soubor


binární
static/img/osci_music_9.mp4 Zobrazit soubor


binární
static/img/osci_music_9_poster.png Zobrazit soubor


binární
static/img/osci_music_9_thumb.png Zobrazit soubor


binární
static/img/osci_music_cats_1.mp4 Zobrazit soubor


binární
static/img/osci_music_cats_1_poster.png Zobrazit soubor


binární
static/img/osci_music_cats_1_thumb.png Zobrazit soubor


binární
static/img/osci_music_cats_2.mp4 Zobrazit soubor


binární
static/img/osci_music_cats_2_poster.png Zobrazit soubor


binární
static/img/osci_music_cats_2_thumb.png Zobrazit soubor


Loading…
Zrušit
Uložit