# BPI-R1 MIC

**thank chaos.dennis do this test:**

i write about this, because i found very few sources and had to tumble a lot around to get it working. This is a quick walk through to give you a hint where to start. I asume for simplicity that you stay always in /root directory as root user. If something fails, look if the files are written and paths are right. It is adressed for more advanced users who know what they are basically doing.

This is done with bananian 15.08 on Kernel 3.4. Other kernels < 4.4 do not work. It is a headless server with no HDMI pluged in.

&#x20;**A. Basic Setup**

* Install alsa (renove pulseaudio if present):

  `# apt-get install alsa-base alsa-utils`
* Configure asound.conf

  `# nano /etc/asound.conf`
* Should look like:

  ```
  pcm.!default {
        type hw
        card 0 # for headphone, turn 1 to 0
        device 0
  }
  ctl.!default {
        type hw
        card 0 # for headphone, turn 1 to 0
  }
  ```
* Shutdown, make a cold boot
* Call the alsamixer

  `# alsamixer`

  Press F5. Your channels should be setup like the attached file at the bottom of this post, or compare it to amixer output:

  ```
  # amixer
  Simple mixer control 'Master',0
  Capabilities: pvolume pvolume-joined
  Playback channels: Mono
  Limits: Playback 0 - 63
  Mono: Playback 45 [71%]
  Simple mixer control 'Master Capture Mute',0
  Capabilities: pswitch pswitch-joined
  Playback channels: Mono
  Mono: Playback [off]
  Simple mixer control 'LineL',0
  Capabilities: pswitch pswitch-joined
  Playback channels: Mono
  Mono: Playback [on]
  Simple mixer control 'LineR',0
  Capabilities: pswitch pswitch-joined
  Playback channels: Mono
  Mono: Playback [on]
  Simple mixer control 'Linein Pre-AMP',0
  Capabilities: volume volume-joined
  Playback channels: Mono
  Capture channels: Mono
  Limits: 0 - 7
  Mono: 7 [100%]
  Simple mixer control 'Mic Input Mux',0
  Capabilities: volume volume-joined
  Playback channels: Mono
  Capture channels: Mono
  Limits: 0 - 15
  Mono: 15 [100%]
  Simple mixer control 'Mic Power',0
  Capabilities: pswitch pswitch-joined
  Playback channels: Mono
  Mono: Playback [on]
  Simple mixer control 'Mic1 Boost',0
  Capabilities: pswitch pswitch-joined
  Playback channels: Mono
  Mono: Playback [on]
  Simple mixer control 'Mic1 gain',0
  Capabilities: volume volume-joined
  Playback channels: Mono
  Capture channels: Mono
  Limits: 0 - 3
  Mono: 3 [100%]
  Simple mixer control 'Mic2 Boost',0
  Capabilities: pswitch pswitch-joined
  Playback channels: Mono
  Mono: Playback [on]
  Simple mixer control 'Mic2 gain',0
  Capabilities: volume volume-joined
  Playback channels: Mono
  Capture channels: Mono
  Limits: 0 - 7
  Mono: 0 [0%]
  Simple mixer control 'MicL',0
  Capabilities: volume volume-joined
  Playback channels: Mono
  Capture channels: Mono
  Limits: 0 - 3
  Mono: 1 [33%]
  Simple mixer control 'MicR',0
  Capabilities: volume volume-joined
  Playback channels: Mono
  Capture channels: Mono
  Limits: 0 - 3
  Mono: 3 [100%]
  Simple mixer control 'Playback',0
  Capabilities: pswitch pswitch-joined
  Playback channels: Mono
  Mono: Playback [on]
  Simple mixer control 'Capture',0
  Capabilities: cvolume cvolume-joined
  Capture channels: Mono
  Limits: Capture 0 - 7
  Mono: Capture 7 [100%]
  Simple mixer control 'ADC Input Mux',0
  Capabilities: volume volume-joined
  Playback channels: Mono
  Capture channels: Mono
  Limits: 0 - 7
  Mono: 7 [100%]
  Simple mixer control 'Fm output',0
  Capabilities: volume volume-joined
  Playback channels: Mono
  Capture channels: Mono
  Limits: 0 - 7
  Mono: 7 [100%]
  Simple mixer control 'FmL',0
  Capabilities: pswitch pswitch-joined
  Playback channels: Mono
  Mono: Playback [on]
  Simple mixer control 'FmR',0
  Capabilities: pswitch pswitch-joined
  Playback channels: Mono
  Mono: Playback [on]
  Simple mixer control 'LINEIN APM',0
  Capabilities: volume volume-joined
  Playback channels: Mono
  Capture channels: Mono
  Limits: 0 - 7
  Mono: 7 [100%]
  Simple mixer control 'Ldac Left Mixer',0
  Capabilities: pswitch pswitch-joined
  Playback channels: Mono
  Mono: Playback [on]
  Simple mixer control 'Ldac Right Mixer',0
  Capabilities: pswitch pswitch-joined
  Playback channels: Mono
  Mono: Playback [on]
  Simple mixer control 'Left Capture Mute',0
  Capabilities: pswitch pswitch-joined
  Playback channels: Mono
  Mono: Playback [on]
  Simple mixer control 'MIC output volume',0
  Capabilities: volume volume-joined
  Playback channels: Mono
  Capture channels: Mono
  Limits: 0 - 7
  Mono: 7 [100%]
  Simple mixer control 'Rdac Right Mixer',0
  Capabilities: pswitch pswitch-joined
  Playback channels: Mono
  Mono: Playback [on]
  Simple mixer control 'Right Capture Mute',0
  Capabilities: pswitch pswitch-joined
  Playback channels: Mono
  Mono: Playback [on]
  ```

  This step is critical. One false muted channel and you are out of the game.
* Test recording.

  `# arecord -L`

  Should show a device.

  `# arecord -f S16_LE -r 44100 -t wav test.wav`

  Should make kind of Sound. Stop it with ctr-c and check the file.

**B. SoX Recording / Streaming**

* Install SoX

  `# apt-get install sox libsox-fmt-all` Sox gives you tools like: sox, rec and play. I would suggest you install it on the client pc too.
* Record striaght to mp3

  ```
  # rec -c 2 -C 64 test.mp3
  ```

  -c 2 two channels

  -C 64 tell the encoder to produce a 64kbit/s mp3

  Stop it with ctr-c, check the output.
* Listen to it live over LAN

  Banana:

  `# rec -c 2 -C 64 -t mp3 - | netcat -l -p 9000`

  -t mp3 - produce mp3 stream to stdout

  Client:

  `# netcat <banana ip> 9000 | vlc`

  Works? Great! Live listening is important to play around and test the mic.
* Listen and record at same time

  `# rec -c 2 -C 64 -t mp3 - | tee test.mp3 | netcat -l -p 9000`
* Poor man's streaming server

  ```
  # apt-get install openbsd-inetd tcpd
  ```

  Edit config file:

  ```
  # nano /etc/inetd.conf
  ```

  Insert this at the end:

  ```
  9000            stream  tcp     nowait  nobody  /usr/bin/tail       tail -f /test.mp3
  ```

  Restart service

  ```
  # service openbsd-inetd restart
  ```

  Start recording on banana:

  ```
  # rec -c 2 -C 64 /test.mp3
  ```

  Connect via client:

  ```
  # netcat <banana ip> 9000 | vlc -
  ```

**C. Advanced Recording**

* Devide recording and postprocessing in two parts

  ```
  # rec -t raw - remix - | sox -t raw -r 48000 -c 1 -b 16 -e signed-integer - -t mp3 -c 1 -C 32 test.mp3
  ```

**Recording:**

&#x20;**-t raw** - forward raw input to pipe

**remix** - Mix the two channels to one

**Encoding:**

&#x20;**-t raw -r 48000 -c 1 -b 16 -e signed-integer** - tell sox what kind of strean comes from stdin

**-t mp3 -c 1 -C 32 test.mp3** and what goes out

Note that it is switched to mono from here with -c 1 and the mp3 bitrate is cut in half. Live listening to the strean does not work anymore, i asume it is because of the non regulare output from pipeing and processing in a second step.

* Reduce noise

  The mic input is pretty bad, lot of white noise. Sox can deal with it.

  Make a sample "silent" file:

  ```
  # rec -t raw test.raw remix - trim 0 10
  ```

  &#x20;**trim 0 10** record 10 seconds

  Test it at client pc:

  ```
  # play -r 48000 -c 1 -b 16 -e signed-integer test.raw
  ```

  Create profile file:

  ```
  # sox -r 48000 -c 1 -b 16 -e signed-integer test.raw -n trim 0 2 noiseprof raw-profile
  ```

  Record with noise canceld

  ```
  # rec -t raw - remix - noisered raw-profile 0.2 | sox -t raw -r 48000 -c 1 -b 16 -e signed-integer - -t mp3 -c 1 -C 32 test.mp3
  ```

  Play with the factor of 0.2 to regulate the intensity of cancellation.

**D. Record 24/7**

My goal of this....

Create a script:

```
 # nano /root/record.sh
```

Insert the following:

```
 #!/bin/bash

# send sigterm and wait for exit of the process
function stop() { kill $@; while kill -0 $@; do sleep 1; done; }
function log() { echo $(date +%T) $@; }

log "Stopping previus recording"
stop `pidof rec` &> /dev/null

DATE=`date "+%Y.%m.%d"`
log "Recording $DATE.mp3"
rec -q -t raw - remix - noisered /root/raw-profile 0.2 | \
        sox -v 2.0 -t raw -r 48000 -c 1 -b 16 -e signed-integer -   \
                   -t mp3 -r 22050 -c 1 -C -9.2 /root/$DATE.mp3
log "Done $DATE.mp3"
```

**Recording:**

&#x20;**-q** less output

**Encoding:** **-v 2.0** Make the input stream louder

**-r 22050** encode to a lower sample rate

**-C -9.2** Create a low quality VBR mp3

Fileszie \~100mb per day. If you want smaller files, a 8000 sanple rate gives you a gsm grade quality.

A very good article to sample rate: <http://wiki.audacityteam.org/wiki/Sample_Rates>

Make it executable

```
 # chmod +x record.sh
```

Edit crontab:

```
 # env EDITOR=nano crontab -e
```

Insert at the end the following:

```
 PATH=/usr/local/bin:/usr/local/sbin:/sbin:/usr/sbin:/bin:/usr/bin
0 0 * * * /root/record.sh >> /root/re cord-mic.log
```

A new recording should start every day at midnight.

![](https://4049603582-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M2HWtRPFCC-Z_DiL7ST%2F-M2HWw1TL1Tj7ZkprRnb%2F-M2HXPAxj9fuIVgBpO2C%2Fmic.png?generation=1584080215469116\&alt=media)
