@dotpi/tools 2.6.1 → 2.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (27) hide show
  1. package/.history/dotpi_root/dist/@dotpi/javascript/index_20250224110150.js +0 -0
  2. package/.history/dotpi_root/dist/@dotpi/javascript/index_20250429123336.js +6 -0
  3. package/.history/dotpi_root/dist/@dotpi/javascript/package_20250224111859.json +15 -0
  4. package/.history/dotpi_root/dist/@dotpi/javascript/package_20250429101520.json +16 -0
  5. package/.history/dotpi_root/share/dotpi_functions/dotpi_functions_audio_device_20250117162542.bash +556 -0
  6. package/.history/dotpi_root/share/dotpi_functions/dotpi_functions_audio_device_20250428182930.bash +556 -0
  7. package/.history/dotpi_root/share/dotpi_functions/dotpi_functions_audio_device_20250428183019.bash +561 -0
  8. package/.history/dotpi_root/share/dotpi_functions/dotpi_functions_audio_device_20250428183434.bash +563 -0
  9. package/.history/dotpi_root/share/dotpi_functions/dotpi_functions_audio_device_20250428183737.bash +563 -0
  10. package/.history/dotpi_root/share/dotpi_functions/dotpi_functions_audio_device_20250428183826.bash +563 -0
  11. package/.history/test/utils.spec_20241114125309.js +48 -0
  12. package/.history/test/utils.spec_20250428183107.js +50 -0
  13. package/.history/test/utils.spec_20250428183349.js +50 -0
  14. package/.history/test/utils.spec_20250428183620.js +49 -0
  15. package/.history/test/utils.spec_20250428183631.js +50 -0
  16. package/.history/test/utils.spec_20250428183753.js +50 -0
  17. package/.history/test/utils.spec_20250428183813.js +50 -0
  18. package/dotpi_root/dist/@dotpi/javascript/LICENSE +28 -0
  19. package/dotpi_root/dist/@dotpi/javascript/index.js +6 -0
  20. package/dotpi_root/dist/@dotpi/javascript/package.json +7 -2
  21. package/dotpi_root/dist/@dotpi/module/LICENSE +28 -0
  22. package/dotpi_root/dist/@dotpi/module/package.json +4 -3
  23. package/dotpi_root/dist/@dotpi/module-module/package.json +3 -3
  24. package/dotpi_root/etc/dotpi_modules.json +2 -1
  25. package/dotpi_root/share/dotpi_functions/dotpi_functions_audio_device.bash +25 -18
  26. package/package.json +3 -3
  27. package/test/utils.spec.js +15 -13
@@ -0,0 +1,561 @@
1
+ #!/bin/bash
2
+
3
+ dotpi_audio_device_supported=(
4
+ 'default'
5
+ 'none'
6
+ 'headphones'
7
+ 'hifiberry dac+ adc pro'
8
+ 'hifiberry dac+ adc'
9
+ 'hifiberry dac+ standard'
10
+ 'hifiberry dac+ light'
11
+ 'hifiberry dac zero'
12
+ 'hifiberry miniamp'
13
+ 'hifiberry beocreate'
14
+ 'hifiberry dac+ dsp'
15
+ 'hifiberry dac+ rtc'
16
+ 'hifiberry pro'
17
+ 'hifiberry amp2'
18
+ 'hifiberry dac2 hd'
19
+ 'hifiberry digi+'
20
+ 'hifiberry digi+ pro'
21
+ 'hifiberry amp+'
22
+ 'hifiberry amp3'
23
+ 'hifiberry dac for raspberry pi 1'
24
+ 'raspberry pi dac pro'
25
+ 'raspberry pi dac+'
26
+ 'raspberry pi digiamp+'
27
+ 'raspberry pi codec zero'
28
+ 'iqaudio dac pro'
29
+ 'iqaudio dac+'
30
+ 'iqaudio digiamp+'
31
+ 'iqaudio codec zero'
32
+ 'bluetooth'
33
+ 'ue boom 2'
34
+ 'ue boom 3'
35
+ 'ue megaboom 2'
36
+ 'ue megaboom 3'
37
+ )
38
+
39
+ dotpi_audio_device_is_analog() {
40
+ audio_device="${1:-$dotpi_audio_device}"
41
+ case "$audio_device" in
42
+ 'headphones')
43
+ return 0
44
+ ;;
45
+ *)
46
+ return 1
47
+ ;;
48
+ esac
49
+ }
50
+
51
+ _dotpi_audio_device_get_options() {
52
+
53
+ _echo_empty_option_error() {
54
+ dotpi_echo_error "$1 requires a non-empty option argument."
55
+ }
56
+
57
+ # From http://mywiki.wooledge.org/BashFAQ/035
58
+
59
+ while true ; do
60
+ case $1 in
61
+
62
+ -h|-\?|--help)
63
+ _dotpi_usage # Display a usage synopsis.
64
+ return 0
65
+ ;;
66
+
67
+ -m|--model) # Takes an option argument; ensure it has been specified.
68
+ if [ -z "$2" ] ; then
69
+ _dotpi_usage
70
+ _echo_empty_option_error "$1"
71
+ return 1
72
+ else
73
+ model="$2"
74
+ shift
75
+ fi
76
+ ;;
77
+
78
+ -c|--config) # Takes an option argument; ensure it has been specified.
79
+ if [ -z "$2" ] ; then
80
+ _dotpi_usage
81
+ _echo_empty_option_error "$1"
82
+ return 1
83
+ else
84
+ config_file="$2"
85
+ shift
86
+ fi
87
+ ;;
88
+
89
+ --) # End of all options.
90
+ shift
91
+ break
92
+ ;;
93
+
94
+ -?*) # Unknown
95
+ _dotpi_usage
96
+ dotpi_echo_error "Unknown option: $1"
97
+ return 1
98
+ ;;
99
+
100
+ *) # Default case: No more options, so break out of the loop.
101
+ break
102
+
103
+ esac
104
+
105
+ shift
106
+ done
107
+
108
+ }
109
+
110
+ dotpi_audio_device_model_normalise() {
111
+ echo "$1" | tr '[:upper:]' '[:lower:]'
112
+ }
113
+
114
+ # Usage: sudo dotpi audio_device_select --model <model> [--config <config_file_path>]
115
+ # if --config is not provided, it will use the system default config file
116
+ dotpi_audio_device_select() (
117
+ # sub-shell to keep everything local (functions and variables)
118
+
119
+ _dotpi_command="$(basename -- "${FUNCNAME[0]:-"$0"}")"
120
+ _dotpi_usage() {
121
+ echo "Usage: ${_dotpi_command} --model <model> [--config <config_file_path>]"
122
+ }
123
+
124
+ # Initialize all the option variables.
125
+ # This ensures we are not contaminated by variables from the environment.
126
+ model=
127
+ config_file=
128
+
129
+ if ! _dotpi_audio_device_get_options "${@}" ; then
130
+ return $?
131
+ fi
132
+
133
+ if [ -z "$model" ] ; then
134
+ _dotpi_usage
135
+ dotpi_echo_error "missing model option"
136
+ return 1
137
+ fi
138
+
139
+ if [ -z "$config_file" ] ; then
140
+ config_path="$(dotpi_system_get_bootfs_path)"
141
+ if [ -z "$config_path" ] ; then
142
+ dotpi_echo_error "Unable to get default system path for config file"
143
+ _dotpi_usage
144
+ return 1
145
+ fi
146
+ config_filename='config.txt'
147
+ config_file="${config_path}/${config_filename}"
148
+ dotpi_echo_warning "Using default config file ${config_file}"
149
+ fi
150
+
151
+
152
+ if ! [ -w "$config_file" ] ; then
153
+ dotpi_echo_error "Unable to access config file ${config_file} for writing"
154
+ return 1
155
+ fi
156
+
157
+ # lowercase everything
158
+ model_normalised="$(dotpi_audio_device_model_normalise "$model")"
159
+
160
+ case "$model_normalised" in
161
+
162
+ ######## Generic devices
163
+
164
+ 'default')
165
+ dotpi_echo_warning "Not installing a specific audio device, keeping defaults"
166
+ ;;
167
+
168
+ 'none')
169
+ _dotpi_audio_device_disable_all
170
+ ;;
171
+
172
+ 'headphones')
173
+ _dotpi_audio_device_select_headphones
174
+ ;;
175
+
176
+ 'bluetooth')
177
+ dotpi_echo_info "Installing generic bluetooth device"
178
+ _dotpi_audio_device_select_bluetooth
179
+ ;;
180
+
181
+ ######## Specific devices
182
+
183
+ # Raspberry
184
+ # Cf. https://www.raspberrypi.com/documentation/accessories/audio.html#configuration
185
+
186
+ 'raspberry pi dac pro'|'iqaudio dac pro')
187
+ dtoverlay=rpi-dacpro
188
+ _dotpi_audio_device_select_raspberry_pi
189
+ ;;
190
+
191
+ 'raspberry pi dac+'|'iqaudio dac+')
192
+ dtoverlay='rpi-dacplus,auto_mute_amp'
193
+ _dotpi_audio_device_select_raspberry_pi
194
+ ;;
195
+
196
+ 'raspberry pi digiamp+'|'iqaudio digiamp+')
197
+ dtoverlay='rpi-digiampplus,auto_mute_amp'
198
+ _dotpi_audio_device_select_raspberry_pi
199
+ ;;
200
+
201
+ 'raspberry pi codec zero'|'iqaudio codec zero')
202
+ dtoverlay=rpi-codeczero
203
+ _dotpi_audio_device_select_raspberry_pi
204
+ ;;
205
+
206
+ # HiFiBerry
207
+ # Cf. https://www.hifiberry.com/docs/software/configuring-linux-3-18-x/
208
+
209
+ 'hifiberry dac for raspberry pi 1'|'hifiberry dac+ light'|'hifiberry dac zero'|'hifiberry miniamp'|'hifiberry beocreate'|'hifiberry dac+ dsp'|'hifiberry dac+ rtc')
210
+ dtoverlay=hifiberry-dac
211
+ _dotpi_audio_device_select_hifiberry
212
+ ;;
213
+
214
+ 'hifiberry dac+ standard'|'hifiberry pro'|'hifiberry amp2'|'hifiberry amp4')
215
+ dtoverlay=hifiberry-dacplus
216
+ _dotpi_audio_device_select_hifiberry
217
+ ;;
218
+
219
+ 'hifiberry dac2 hd')
220
+ dtoverlay=hifiberry-dacplushd
221
+ _dotpi_audio_device_select_hifiberry
222
+ ;;
223
+
224
+ 'hifiberry dac+ adc')
225
+ dtoverlay=hifiberry-dacplusadc
226
+ _dotpi_audio_device_select_hifiberry
227
+ ;;
228
+
229
+ 'hifiberry dac+ adc pro')
230
+ dtoverlay=hifiberry-dacplusadcpro
231
+ _dotpi_audio_device_select_hifiberry
232
+ ;;
233
+
234
+ 'hifiberry digi+')
235
+ dtoverlay=hifiberry-digi
236
+ _dotpi_audio_device_select_hifiberry
237
+ ;;
238
+
239
+ 'hifiberry digi+ pro')
240
+ dtoverlay=hifiberry-digi-pro
241
+ _dotpi_audio_device_select_hifiberry
242
+ ;;
243
+
244
+ 'hifiberry amp+')
245
+ dtoverlay=hifiberry-amp
246
+ _dotpi_audio_device_select_hifiberry
247
+ ;;
248
+
249
+ 'hifiberry amp3')
250
+ dtoverlay=hifiberry-amp3
251
+ _dotpi_audio_device_select_hifiberry
252
+ ;;
253
+
254
+ 'hifiberry amp4 pro')
255
+ dtoverlay=hifiberry-amp4pro
256
+ _dotpi_audio_device_select_hifiberry
257
+ ;;
258
+
259
+ ####### Ultimate Ears (UE)
260
+
261
+ 'ue boom 2'|'ue boom 3'|'ue megaboom 2'|'ue megaboom 3')
262
+ _dotpi_audio_device_select_bluetooth
263
+ ;;
264
+
265
+
266
+ ########### Unknown device
267
+
268
+ *) # other
269
+ _dotpi_usage
270
+ dotpi_echo_error "Unknown audio device model: ${model}"
271
+ return 1
272
+ ;;
273
+
274
+ esac
275
+
276
+ # use xargs to unquote value
277
+ model_project="$(dotpi_configuration_read \
278
+ --file "${DOTPI_ROOT}/etc/dotpi_environment_project.bash" \
279
+ --key dotpi_audio_device \
280
+ | xargs)"
281
+
282
+
283
+ if [[ "$model_normalised" == "$(dotpi_audio_device_model_normalise "$model_project")" ]] ; then
284
+ # no need to update environment
285
+ return 0;
286
+ fi
287
+
288
+ dotpi_echo_warning 'Audio device model is specific to this instance'
289
+
290
+ # comment setting in project
291
+ dotpi_configuration_comment --file "${DOTPI_ROOT}/etc/dotpi_environment_project.bash" \
292
+ --key dotpi_audio_device
293
+
294
+ # escape double-quote for now, to quote when writing later
295
+ model_value="$(echo "$model" | dotpi_sed 's/"/\\"/g')"
296
+
297
+ # write changed setting in instance
298
+ # be sure to quote value
299
+ dotpi_configuration_write --file "${DOTPI_ROOT}/etc/dotpi_environment_instance.bash" \
300
+ --key dotpi_audio_device \
301
+ --value \""${model_value}"\"
302
+ )
303
+
304
+ _dotpi_audio_device_disable_all() (
305
+ exception_family="$1"
306
+
307
+ if [[ "$exception_family" = "" ]] ; then
308
+ dotpi_echo_info "Disabling all audio devices"
309
+ else
310
+ dotpi_echo_info "Disabling all audio devices except ${exception_family}"
311
+ fi
312
+
313
+ if [[ ! "$exception_family" = "headphones" ]] ; then
314
+ _dotpi_audio_device_disable_headphones
315
+ fi
316
+
317
+ if [[ ! "$exception_family" = "hdmi" ]] ; then
318
+ _dotpi_audio_device_disable_hdmi
319
+ fi
320
+
321
+ if [[ ! "$exception_family" = "raspberry_pi" ]] ; then
322
+ _dotpi_audio_device_disable_raspberry_pi
323
+ fi
324
+
325
+ if [[ ! "$exception_family" = "hifiberry" ]] ; then
326
+ _dotpi_audio_device_disable_hifiberry
327
+ fi
328
+
329
+ )
330
+
331
+ _dotpi_audio_device_select_headphones() (
332
+ dotpi_echo_info "Installing Headphones (default) audio device"
333
+
334
+ _dotpi_audio_device_disable_all headphones
335
+
336
+ # un-comment dtparam=audio as it must be set first
337
+ # (setting at the end the the file fails)
338
+ pattern="$(dotpi_configuration_get_pattern --prefix "# dtparam=audio")"
339
+ perl -pe "s/${pattern}/"'dtparam=audio=on/' -i -- "$config_file"
340
+ )
341
+
342
+ _dotpi_audio_device_select_bluetooth() (
343
+ _dotpi_audio_device_disable_all bluetooth
344
+ )
345
+
346
+ _dotpi_audio_device_select_raspberry_pi() (
347
+ dotpi_echo_info "Configuring Raspberry Pi audio device '${model}'"
348
+
349
+ _dotpi_audio_device_disable_all raspberry_pi
350
+
351
+ mkdir -p "$(dirname -- "$config_file")"
352
+ cat >> "$config_file" << EOF
353
+
354
+ # dotpi: overlay for audio device '${model}'
355
+ dtoverlay=${dtoverlay}
356
+
357
+ EOF
358
+ )
359
+
360
+
361
+ _dotpi_audio_device_select_hifiberry() (
362
+ dotpi_echo_info "Configuring HiFiBerry audio device '${model}'"
363
+
364
+ _dotpi_audio_device_disable_all hifiberry
365
+
366
+ mkdir -p "$(dirname -- "$config_file")"
367
+ cat >> "$config_file" << EOF
368
+
369
+ # dotpi: overlay for audio device '${model}'
370
+ dtoverlay=${dtoverlay}
371
+
372
+ EOF
373
+ )
374
+
375
+ _dotpi_audio_device_disable_headphones() (
376
+ dotpi_echo_info "Disabling headphones"
377
+ # disable internal audio device
378
+ dotpi_configuration_comment --file "$config_file" --key 'dtparam=audio'
379
+ )
380
+
381
+ _dotpi_audio_device_disable_hdmi() (
382
+ dotpi_echo_info "Disabling HDMI audio"
383
+ # disable HDMI audio
384
+
385
+ # new systems
386
+ pattern="$(dotpi_configuration_get_pattern --prefix "dtoverlay=vc4-kms-v3d")"
387
+ perl -pe "s/${pattern}/"'${1}${2},noaudio/' -i -- "$config_file"
388
+
389
+ # old systems
390
+ pattern="$(dotpi_configuration_get_pattern --prefix "dtoverlay=vc4-fkms-v3d")"
391
+ perl -pe "s/${pattern}/"'${1}${2},audio=off/' -i -- "$config_file"
392
+ )
393
+
394
+ _dotpi_audio_device_disable_raspberry_pi() (
395
+ dotpi_echo_info "Disabling Raspberry Pi audio"
396
+
397
+ # disable hifiberry
398
+ pattern="$(dotpi_configuration_get_pattern --prefix "dtoverlay=rpi-")"
399
+ perl -pe "s/${pattern}/"'${1}# ${2}${3}/' -i -- "$config_file"
400
+ )
401
+
402
+ _dotpi_audio_device_disable_hifiberry() (
403
+ dotpi_echo_info "Disabling HiFiBerry audio"
404
+
405
+ # disable hifiberry
406
+ pattern="$(dotpi_configuration_get_pattern --prefix "dtoverlay=hifiberry-")"
407
+ perl -pe "s/${pattern}/"'${1}# ${2}${3}/' -i -- "$config_file"
408
+ )
409
+
410
+ _dotpi_audio_device_volume_set_alsa() (
411
+ amixer -- sset "$control" "$volume"
412
+ alsactl store
413
+
414
+ if [[ "$volume" == "$dotpi_audio_volume" ]] ; then
415
+ # no change to write
416
+ return 0
417
+ fi
418
+
419
+ # comment setting in project
420
+ dotpi_configuration_comment --file "${DOTPI_ROOT}/etc/dotpi_environment_project.bash" \
421
+ --key dotpi_audio_volume
422
+
423
+ # escape double-quote for now, to quote when writing later
424
+ model_value="$(echo "$model" | dotpi_sed 's/"/\\"/g')"
425
+
426
+ # write changed setting in instance
427
+ dotpi_configuration_write --file "${DOTPI_ROOT}/etc/dotpi_environment_instance.bash" \
428
+ --key dotpi_audio_volume \
429
+ --value "$volume"
430
+ )
431
+
432
+ dotpi_audio_device_init() (
433
+
434
+ if [[ -z "$dotpi_audio_device" ]] ; then
435
+ dotpi_echo_warning "dotpi_audio_device not set, keeping default audio device"
436
+ return 0
437
+ fi
438
+
439
+ model_normalised="$(dotpi_audio_device_model_normalise "$dotpi_audio_device")"
440
+
441
+ # hardware
442
+ case "$model_normalised" in
443
+ 'raspberry pi codec zero'|'iqaudio codec zero')
444
+
445
+ usr_repository="raspberrypi/Pi-Codec"
446
+ usr_directory="${DOTPI_ROOT}/usr/${usr_repository}"
447
+
448
+ # clone once to be able to init offline
449
+ if ! [[ -d "${usr_directory}" ]] ; then
450
+ mkdir -p "${usr_directory}"
451
+ git clone "https://github.com/${usr_repository}.git" "${usr_directory}" || {
452
+ dotpi_echo_error "Unable to clone repository ${usr_repository}"
453
+ return 1
454
+ }
455
+ fi
456
+
457
+ alsactl restore -f "${usr_directory}/Codec_Zero_StereoMIC_record_and_HP_playback.state"
458
+ ;;
459
+
460
+ esac
461
+
462
+ # alsa
463
+ case "$model_normalised" in
464
+
465
+ 'headphones')
466
+
467
+ amixer -D sysdefault sset -- 'PCM' 'on'
468
+ amixer -D sysdefault sset -- 'PCM' '0dB'
469
+ alsactl store
470
+ ;;
471
+
472
+ 'raspberry pi dac pro'|'iqaudio dac pro' \
473
+ | 'raspberry pi dac+'|'iqaudio dac+' \
474
+ | 'raspberry pi digiamp+'|'iqaudio digiamp+' \
475
+ | 'raspberry pi codec zero'|'iqaudio codec zero')
476
+
477
+ amixer -D sysdefault sset -- 'Headphone' 'on'
478
+ amixer -D sysdefault sset -- 'Headphone' '0dB'
479
+
480
+ amixer -D sysdefault sset -- 'Lineout' 'on'
481
+ amixer -D sysdefault sset -- 'Lineout' '0dB'
482
+
483
+ alsactl store
484
+ ;;
485
+
486
+ esac
487
+
488
+ )
489
+
490
+ # set and store volume for the current audio device
491
+ dotpi_audio_device_volume_set() (
492
+ volume="$1"
493
+
494
+ model="$dotpi_audio_device"
495
+ # lowercase everything
496
+ model_normalised="$(dotpi_audio_device_model_normalise "$model")"
497
+
498
+ case "$model_normalised" in
499
+
500
+ ####### Generic devices
501
+
502
+ 'headphones')
503
+ control='PCM'
504
+ _dotpi_audio_device_volume_set_alsa
505
+ ;;
506
+
507
+ 'bluetooth')
508
+ dotpi_audio_bluetooth_destination_volume_set "$volume"
509
+ ;;
510
+
511
+
512
+ ####### Specific devices
513
+
514
+ # HiFiBerry
515
+ # Cf. https://www.hifiberry.com/docs/archive/mixer-controls-on-the-hifiberry-boards/
516
+
517
+
518
+ 'hifiberry dac+ dsp'|'hifiberry dac+ rtc'|'hifiberry dac+ standard'|'hifiberry amp2'|'hifiberry dac+ adc'|'hifiberry dac+ adc pro'|'hifiberry amp3')
519
+ control='Digital'
520
+ _dotpi_audio_device_volume_set_alsa
521
+ ;;
522
+
523
+ 'hifiberry dac2 hd')
524
+ control='DAC'
525
+ _dotpi_audio_device_volume_set_alsa
526
+ ;;
527
+
528
+ 'hifiberry amp+')
529
+ control='Master'
530
+ _dotpi_audio_device_volume_set_alsa
531
+ ;;
532
+
533
+ ####### Ultimate Ears (UE)
534
+
535
+ 'ue boom 2'|'ue boom 3'|'ue megaboom 2'|'ue megaboom 3')
536
+ dotpi_audio_bluetooth_destination_volume_set "$volume"
537
+ ;;
538
+
539
+ ########### Unknown device
540
+
541
+ *) # other
542
+ dotpi_echo_error "Unable to set volume for unknown audio device model: ${model}"
543
+ return 1
544
+ ;;
545
+
546
+
547
+ esac
548
+ )
549
+
550
+ # restore volume for the current audio device
551
+ dotpi_audio_device_volume_init() (
552
+ if [[ -z "$dotpi_audio_volume" ]] ; then
553
+ dotpi_echo_warning "dotpi_audio_volume not set, keeping default volume"
554
+ return 0
555
+ fi
556
+
557
+ destination_volume="$dotpi_audio_volume"
558
+ dotpi_audio_device_volume_set "$destination_volume"
559
+ )
560
+
561
+