UltrasonicPlayer: Difference between revisions

From RevSpace
Jump to navigation Jump to search
Line 73: Line 73:


Challenges:
Challenges:
* Starting playback upon USB stick plugin, using udev?
* Starting playback upon USB stick plugin, using udev, udisk, systemd?
* Stopping playback upon USB stick plugout, using udev?
* Stopping playback upon USB stick plugout, using udev?
* Linux experts saying: "You can't pull the stick without unmounting! That would be bad!". Careful with this, they get VERY excited about this.
* Linux experts saying: "You can't pull the stick without unmounting! That would be bad!". Careful with this, they get VERY excited about this.

Revision as of 08:22, 5 November 2017

Project UltrasonicPlayer
Sa9227.jpg
An inexpensive DIY ultrasonic audio player
Status In progress
Contact bertrik
Last Update 2017-11-05

Introduction

This project is about a do-it-yourself portable ultrasonic audio player, built out of inexpensive modules available on sites like AliExpress.

A typical use case for this is a kind of "lure" for biology researchers to improve the results of trying to catch bats in a net. The player emulates (social) bat calls which attracts the bats to the net, increasing the chance for them to be caught.

Another use case is to use it as an educational tool, to train people in the use of a bat detector. The player makes it easy to play the exact same sound again and again, allowing people to experiment with finding the best setting for their equipment and to improve their determination skills.

The ultrasonic player consists of a USB sound card with high sampling rate plus a speaker to turn it into actual ultrasonic audio. A built-in Linux single-board-computer (something like a Raspberry Pi) controls playback of audio files. The idea is that you prepare one or more USB sticks with bat calls and just plug one in the player, then the player automatically plays them in a loop

Status

 2017-10-10: basically I'm stuck on the software part, unable to decide how I should solve how to play audio files on USB stick plugin, I've got several hints but no definite solution. Getting more hints like "why *don't* you try X" are confusing the issue more. Also a bit stuck on the construction part (housing, etc.)
 2017-06-24: successfully played audio back from a raspberry pi 3, running the amplifier from one of the USB ports. The rpi3 shows a little lightning bolt indicating undervoltage while playing.
 2017-06-18: realized that an orange pi zero is not going to work: it doesn't have enough USB ports
 2017-06-16: playing a bit with udev scripts to automate actions upon USB stick plugin: I can do short actions (like mount/unmount) but no longer running actions from udev!
 2017-05-01: replaced a feedback resistor on the amplifier, to reduce the gain from about 30 to 3, this also makes it easier to adjust the gain using the potmeter
 2017-04-30: found the schematic for the amplifier, plan to modify it for lower gain (better level control and higher bandwidth)
 2017-04-16: created a video of current progress.
 2016-10-11: received the amplifier boards.
 2016-10-02: ordered the parts (USB audio, amplifier, a few step-up circuits).

Next steps:

  • work on automatically mounting USB stick and playing the file on it
    • simple alternative: just put in a USB stick and reboot
    • simplest alternative: just put the audio files on the sd card and forget about the USB stick for now
  • design a simple lasercut case, possibly just a designed basic on a simple makercase box
    • customised with extra holes for USB battery, USB stick speaker
    • drill holes for tie-wraps to hold everything together on the inside of the box

Hardware

block diagram
50 kHz sine wave
100 kHz sine wave
150 kHz sine wave
TDA2030 amplifier schematic
speaker dimensions

The hardware consists of the following parts:

  1. Some kind of media player which takes care of the storage and playback of the ultrasonic files, for example a single-board computer like a Raspberry Pi
  2. A USB audio card to create the analog ultrasonic signal
  3. An amplifier to amplify the ultrasonic signal
  4. A speaker to turn the signal into actual ultrasonic audio

media player

As a media player, I'm using a raspberry pi, in particular a raspberry pi 3.

The player hardware needs at least two high-speed USB 2.0 ports, one for the USB stick containing the wav files and another one for the USB audio card. Bandwidth needed over USB is equivalent to 2-channel 16-bit audio at 384 kHz, which is 12.28 mbps, exceeding USB 1.0 full-speed throughput of 12 mbps.

The main use case of the entire player is as follows:

  • user switches on the ultrasonic player
  • the user plugs in a USB stick (formatted as FAT32 for windows compatibility) with some wav files
  • the player automatically mounts the USB stick as a read-only device, e.g. using udev. Mounting it read-only should prevent corruption of file data on the USB stick.
  • the player automatically plays all files from the USB stick, on repeat.
  • when done playing, the user just pulls out the USB stick, the playback process is stopped automatically.
  • user switches the ultrasonic player off

See also:

Challenges:

  • Starting playback upon USB stick plugin, using udev, udisk, systemd?
  • Stopping playback upon USB stick plugout, using udev?
  • Linux experts saying: "You can't pull the stick without unmounting! That would be bad!". Careful with this, they get VERY excited about this.

USB sound card

I'm considering this one: USB sound card based on a SA9227+PCM5102 chip.

It allows a maximum sample rate of up to 384 kHz, or equivalently audio up to about 170 kHz. Price: about E30,- On the right, from top to bottom: the signal at 50 kHz, 100 kHz, 150 kHz respectively

Measurements of amplitude vs frequency:

  • on the scope you can clearly see the sampling steps: at 100 kHz one wave is sampled using only 3.84 samples.
  • the amplitude drops a bit when going higher, but only like 30% at 150 kHz compared to 50 kHz

Modifications:

  • removed the yellow audio connector, soldered on a simple 3-pin 2.54 mm pin header.

Amplifier

I'm considering this TDA2030-based module: amplifier board based on a TDA2030 chip.

The TDA2030 chip has a claimed audio bandwidth of up to 140 kHz. Price: about E1,-

Measurements with a separate power supply for the amplifier:

  • seems to be able to handle 50 kHz and 150 kHz audio input equally
  • additionally, it seems the amplifier still works down to 5 or 6V power supply voltage.

Modification: replace R5 (150k) with a 15k resistor. This reduces the gain of the amplifier from about 30 to 3 times, making adjustment of input level easier and also improves bandwidth. We don't need a gain of 30, the input signal is already at about 1V level.

Speaker

I'm considering this one: Vifa/Tymphany XT25SC90-04.

This speaker is also used in other products that produce ultrasonic audio.

Price: about E22,-

Power

Waveform of AL697 running into 1 kOhm load

I'm thinking of using a 5V USB battery. There are plenty of models to choose from, in varying capacity ranges and prices.

To supply the amplifier with 12V, I'm considering this voltage converter: 5V-to-12V step-up cable or possibly this USB 5V to 12V converter. A consideration for the step-up converter is that the switching frequency is considerably higher than any ultrasonics frequencies we are interested in.

Measurement results for the "AL519" converter, running into a 1 kOhm load:

  • switching period is about 134 us (7.5 kHz)
  • peak-peak ripple of about 200 mV

Measurement for board with "AL697"-chip into a 1 kOhm load

  • switching frequency is about 15 kHz
  • peak-peak ripple of about 200 mV

Current measurement (total current over USB)

  • normally 0.36A (USB audio + step-up + amplifier)
  • when playing: 0.39A
  • current consumed by USB audio: 0.18A idle, 0.19 when playing

Mounting it in a case

parts to be integrated

The various parts have the following dimensions (length x width x height) approximately:

  • USB battery: 111x68x?? mm (without plugs)
  • USB audio: 66x51 mm (PCB only), 77x51 mm (including connectors, without plugs)
  • speaker: 66 mm (outside diameter), 53 mm (mounting hole diameter). Hole coordinates (mm): (0, 53) / (45.9, -26.5) / (-45.9, -26.5), big mounting hole: 47 mm diameter
  • raspberry pi 3
  • amplifier: 32x25x24mm
  • step-up converter: 42x15x12 mm (without plugs)

Challenges:

  • Put it all in a practical case. For the first prototype, I'm thinking about just laser-cutting a basic enclosure, then use zip-ties to tie stuff to the inside of the box. Make part of the USB battery stick out, so we can use it as an on/off switch and allow access to the charge port.

Perhaps I can use a raspberry pi to solve the USB port problem, see this table on wikipedia for comparison. For example the model B, generation 1+ has four USB port. Also it has modest power requirements.

Software

As an operating system, I prefer Debian Linux, because I'm familiar with it on the desktop. This distribution allows a small basic minimal image without a graphical environment and systemd support.

Software will be added to my batplayer github archive, things like:

  • udev/systemd plugin script: mounts the USB stick (read-only), invokes the playback script
  • udev/systemd plugout script: stops the playback script, forces unmount of USB stick
  • playback script, probably written in python, gets the mounted path as argument, scans the path for wav files and tries to play each file using aplay.
  • udev scripts, to automatically mount and unmount USB sticks
  • etc.

To play back an audio file, I just use aplay (from package alsa-utils), for example:

aplay -D plughw:CARD=Audio,DEV=0 noise.wav -v

Current plan:

  • use udev to detect when a USB stick is inserted, automatically mount it read-only and run an 'at' command
  • the 'at' command is started at time 'now+0' (as soon as possible) and starts a python script
  • the python script takes as argument the path to the mounted stick, it recursively searches for wav-files and runs 'aplay' on each wav-file.
  • use udev to detect when the USB stick in removed, force kill the python script + any child commands, automatically force unmount it

Can we use systemd to automate part of the steps above?

udev dump

Events detected by udevadm monitor when plugging in a USB stick into my laptop:

KERNEL[1792.773451] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1 (usb)
KERNEL[1792.773723] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0 (usb)
KERNEL[1792.774053] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/host2 (scsi)
KERNEL[1792.774231] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/host2/scsi_host/host2 (scsi_host)
KERNEL[1792.774317] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0 (usb)
KERNEL[1792.774417] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-1 (usb)
UDEV  [1792.780766] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1 (usb)
UDEV  [1792.786816] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0 (usb)
UDEV  [1792.786872] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/host2 (scsi)
UDEV  [1792.787432] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/host2/scsi_host/host2 (scsi_host)
UDEV  [1792.789019] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0 (usb)
UDEV  [1792.790123] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-1 (usb)
KERNEL[1793.789325] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/host2/target2:0:0 (scsi)
KERNEL[1793.789431] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/host2/target2:0:0/2:0:0:0 (scsi)
KERNEL[1793.789537] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/host2/target2:0:0/2:0:0:0/scsi_disk/2:0:0:0 (scsi_disk)
KERNEL[1793.789599] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/host2/target2:0:0/2:0:0:0 (scsi)
KERNEL[1793.789635] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/host2/target2:0:0/2:0:0:0/scsi_device/2:0:0:0 (scsi_device)
KERNEL[1793.789958] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/host2/target2:0:0/2:0:0:0/scsi_generic/sg1 (scsi_generic)
KERNEL[1793.790118] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/host2/target2:0:0/2:0:0:0/bsg/2:0:0:0 (bsg)
KERNEL[1793.790712] add      /devices/virtual/bdi/8:16 (bdi)
UDEV  [1793.791661] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/host2/target2:0:0 (scsi)
UDEV  [1793.792455] add      /devices/virtual/bdi/8:16 (bdi)
UDEV  [1793.793191] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/host2/target2:0:0/2:0:0:0 (scsi)
UDEV  [1793.794019] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/host2/target2:0:0/2:0:0:0/scsi_disk/2:0:0:0 (scsi_disk)
UDEV  [1793.794587] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/host2/target2:0:0/2:0:0:0 (scsi)
UDEV  [1793.796165] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/host2/target2:0:0/2:0:0:0/scsi_device/2:0:0:0 (scsi_device)
UDEV  [1793.796387] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/host2/target2:0:0/2:0:0:0/scsi_generic/sg1 (scsi_generic)
UDEV  [1793.796528] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/host2/target2:0:0/2:0:0:0/bsg/2:0:0:0 (bsg)
KERNEL[1793.796590] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/host2/target2:0:0/2:0:0:0/block/sdb (block)
KERNEL[1793.796630] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/host2/target2:0:0/2:0:0:0/block/sdb/sdb1 (block)
UDEV  [1793.913501] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/host2/target2:0:0/2:0:0:0/block/sdb (block)
UDEV  [1794.008034] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/host2/target2:0:0/2:0:0:0/block/sdb/sdb1 (block)

system log dump

What I see in the system log when plugging in a USB stick into my laptop:

Nov  4 23:46:01 zenbook kernel: [ 3550.158592] usb 1-1: new high-speed USB device number 14 using xhci_hcd
Nov  4 23:46:02 zenbook kernel: [ 3550.307449] usb 1-1: New USB device found, idVendor=08ec, idProduct=0020
Nov  4 23:46:02 zenbook kernel: [ 3550.307454] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
Nov  4 23:46:02 zenbook kernel: [ 3550.307457] usb 1-1: Product: Store'n'go
Nov  4 23:46:02 zenbook kernel: [ 3550.307460] usb 1-1: Manufacturer: Verbatim
Nov  4 23:46:02 zenbook kernel: [ 3550.307463] usb 1-1: SerialNumber: 0E21E5708251F9A3
Nov  4 23:46:02 zenbook kernel: [ 3550.308029] usb-storage 1-1:1.0: USB Mass Storage device detected
Nov  4 23:46:02 zenbook kernel: [ 3550.308281] scsi host2: usb-storage 1-1:1.0
Nov  4 23:46:02 zenbook upowerd[1116]: unhandled action 'bind' on /sys/devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0
Nov  4 23:46:02 zenbook upowerd[1116]: unhandled action 'bind' on /sys/devices/pci0000:00/0000:00:14.0/usb1/1-1
Nov  4 23:46:03 zenbook kernel: [ 3551.311276] scsi 2:0:0:0: Direct-Access     VBTM     Store'n'go       6.51 PQ: 0 ANSI: 0 CCS
Nov  4 23:46:03 zenbook kernel: [ 3551.311935] sd 2:0:0:0: Attached scsi generic sg1 type 0
Nov  4 23:46:03 zenbook kernel: [ 3551.312081] sd 2:0:0:0: [sdb] 1952767 512-byte logical blocks: (1000 MB/953 MiB)
Nov  4 23:46:03 zenbook kernel: [ 3551.312258] sd 2:0:0:0: [sdb] Write Protect is off
Nov  4 23:46:03 zenbook kernel: [ 3551.315984]  sdb: sdb1
Nov  4 23:46:03 zenbook kernel: [ 3551.317012] sd 2:0:0:0: [sdb] Attached SCSI removable disk

Audio files

Suitable audio files for playback:

  • the 'synthesized' versions from batcalls.com by AviSoft, these are mostly mono 16-bit PCM, sampled at 250000 Hz