[PYTHON] Music playback server with NanoPi-NEO, MPD and OLED

Overview

Build a music playback server with MPD and display the song name, artist name, album name and jacket image being played on the OLED. Of course, it's OK in Japanese !! And you can also "previous song", "play / pause", and "next song" with three buttons.

Perhaps it will be one of the completed forms of MPD in SingleBoard Computing (laughs). Since it is a great deal, I will send it as a complete version including environment construction.

_72D8924.jpg

The left side is NEO2 Complete Starter Kit. It's out of stock, and it's miraculous when it becomes in stock for a moment on 5/26? A gem that I was able to get. The right side is a combination of NanoPi-NEO + NanoHat PCM5102a + NanoHat OLED.

The code for displaying various information on OLED by Python, which is the main code this time, is registered below. https://github.com/blue777/NanoPi-NEO_OLED_MPD

Related series introduction

-NanoPi-NEO, MPD, LCD, I2S-DAC and TDA7297 music playback server (complete) -MCLK output with I2S of NanoPi-NEO2 & 384kHz, 768kHz, 1536kHz, 8kHz compatible

Things necessary

hardware

-NanoPi NEO ($ 7.99) or [NanoPi NEO2 ($ 14.99)](http://www.friendlyarm.com/ index.php? route = product / product & product_id = 180)

Or

Or

software

NanoPi NEO

Official image

Is assumed to be one of the above.

NanoPi NEO2

Official rom image

Is assumed to be one of the above.

Server side construction procedure

It is assumed that the Boot Image is transferred to MicroSD and booted.

1. Initial setup

timedatectl set-timezone Asia/Tokyo apt-get update apt-get upgrade npi-config

I will prepare around. If the fixed IP is good, it looks like the one below.

nano /etc/network/interfaces

/etc/network/interfaces


allow-hotplug eth0
#        iface eth0 inet dhcp
        iface eth0 inet static
            address 192.168.1.100
            netmask 255.255.255.0
            gateway 192.168.1.1
            dns-nameservers 192.168.1.1

2. Install MPD (Music Player Daemon)

Install with the following command. After installation, change the settings.

apt-get install mpd nano /etc/mpd.conf

Change music directory

Find the line and do the following: (I will explain on the premise of the following)

/etc/mpd.conf


music_directory  "/media/"

Audio output settings

First, cat /proc/asound/cards Outputs a list of sound cards.

It looks like this. For the time being, it's in a connected state (laughs)

NanoPi-NEO


root@NanoPi-NEO:~# cat /proc/asound/cards
 0 [Pcm5102a       ]: Pcm5102a - Pcm5102a
                      Pcm5102a
 1 [sndhdmi        ]: sndhdmi - sndhdmi
                      sndhdmi
 2 [DAC            ]: USB-Audio - USB Audio DAC
                      Burr-Brown from TI USB Audio DAC at usb-sunxi-ehci-1.2, full speed
 3 [J20            ]: USB-Audio - JAVS USB Audio 2.0
                      JAVS JAVS USB Audio 2.0 at usb-sunxi-ehci-1.3, high speed
 4 [N2             ]: USB-Audio - NuForce µDAC 2
                      NuForce, Inc. NuForce µDAC 2 at usb-sunxi-ehci-1.4, full speed
 5 [AI301DA        ]: USB-Audio - TEAC AI-301DA
                      TEAC Corporation TEAC AI-301DA at usb-sunxi-ehci-1.1, high speed

root@NanoPi-NEO:~# uname -a
Linux NanoPi-NEO 3.4.39-h3 #3 SMP PREEMPT Tue May 23 17:37:51 CST 2017 armv7l armv7l armv7l GNU/Linux

NanoPi-NEO2


root@NanoPi-NEO2:~# cat /proc/asound/cards
 0 [Codec          ]: H3_Audio_Codec - H3 Audio Codec
                      H3 Audio Codec
 1 [USBCamera      ]: USB-Audio - USB_Camera
                      KYE Systems Corp. USB_Camera at usb-1c1d000.usb-1, high speed

root@NanoPi-NEO2:~# uname -a
Linux NanoPi-NEO2 4.11.2 #1 SMP Wed Jun 7 03:24:53 PDT 2017 aarch64 aarch64 aarch64 GNU/Linux

If you explain in order from the top,

InstanceNo. DeviceName Commentary
0 Pcm5102a It is NanoHat Pcm5102a. NanoPi-NEO Kernel=3.4.This is the display name for 39. If the Kernel Ver changes, the display name will also change.
1 sndhdmi I think it is an HDMI Audio signal output, but it cannot be used because there is no HDMI connector.
2 DAC It is a USB DAC.
3 J20 It is a USB DDC. JAVS X-It is a USB to SPDIF converter called DDC.
4 N2 It is a USB DAC. NuForce μ DAC.
5 AI301DA It is a USB DAC. Teac AI-It is 301DA.
0 Codec NanoPi-Line Out on the NEO2 board. You should be able to use it by soldering. It seems that the reason why it is H5 but H3 is because the driver is common.
1 USBCamera It is a camera with a microphone input. It cannot be used because it cannot output audio.

This is the basic writing method for each. I will write it like this. It is OK to have multiple audio_output tags. If there are multiple, you can also specify simultaneous output.

For NanoHat PCM5102a


audio_output {
        type            "alsa"
        name            "HanoHat PCM5102a"
        device          "hw:Pcm5102a,0"
        mixer_type      "software"
        format          "*:32:2"
        replay_gain_handler "software"
}

J20(JAVS_X-DDC)in the case of


audio_output {
        type            "alsa"
        name            "JAVS X-DDC"
        device          "hw:J20,0"
        mixer_type      "none"
}

For Codec


audio_output {
        type            "alsa"
        name            "NanoPi-NEO2 LineOut"
        device          "hw:Codec,0"
        mixer_type      "software"
}

The name specified for each will be the display name from the MPD client. Enter InstanceNo. Or DeviceName in the XXXX part of "hw: XXXX, 0" of device. If you want it to be automatically recognized by USB connection, enter the last Instance No. etc. However, it is recommended to specify by DeviceName because it changes depending on the allocation by InstanceNo. And the connection order. (You can change the numbering order in ALSA, but I will omit it.)

3. Preparation of music data

When making microSD into a shared folder

Make the microSD with the OS installed a shared folder Music data will be uploaded to Nanopi NEO from an external PC.

Samba installation and Create a directory in / media / local and set sharing. The sharing settings are full access settings. (Lol

apt-get install samba mkdir /media/local chmod 777 /media/local nano /etc/samba/smb.conf

/etc/samba/smb.conf


・
・
・
[Music]
   comment = share folder
   path = /media/local
   public = yes
   writable = yes
   guest ok = yes
   guest only = yes
   create mode = 0777
   directory mode = 0777

systemctl enable smbd systemctl start smbd

When using an external shared folder such as NAS

When using Windows file sharing or mounting NAS.

apt-get install cifs-utils mkdir /media/nas nano /etc/fstab

・
・
・
//192.168.1.9/Public /media/nas cifs username=guest,password=,uid=root,iocharset=utf8 0 0

After setting

mount -a df

Make sure it mounts properly with.

4. OLED display setup

I wrote the song title display and the acquisition / display of the jacket image in Python. I used Python for the first time this time, and it was completed in about two days. .. .. On the way, I was suffering from a mixture of tabs and spaces, and there were twists and turns, It was better than I expected, so it also inspired me to write this article.

2017/08/31 Addendum: Sequel "NanoPi-NEO, MPD, LCD, I2S-DAC, and TDA7297 music playback server (complete)" In addition to OLED display, LCD display is also supported. Please use your favorite display software.

Main Specifications

--Display of song title. If it is long, it will scroll automatically. --Display of artist name and album name --Display of current playback position (underscore below the title) --Display of Freq, BitDepth, Ch --Display album jacket image (using mutagen) --Physical buttons allow you to operate "previous song", "play / pause", and "next song" --Displays the current date and time during pause --Japanese display OK

Mechanism etc.

--The button press detection uses the NanoPi Official program (C language). It detects that the button is pressed and skips the event to the Python side. ――I made it with reference to NanoPi Official's OLED display program, but since I2C wrote the display data byte by byte kindly and carefully, I rewrote it completely to 32 byte unit transfer. The display speed has increased a few times. --For the jacket image, access the local file by mutagen and fetch the embedded image. If there is no image, front.jpg in the same directory will be used. --Information is acquired from mpd via TCP. Control is also true. --I2C communication is 100k or 400k depending on the OS. This will greatly change the display speed. --If you want to adjust the scroll speed of the title, you can adjust it with scroll_unit = 3 in oled_mpd.py.

Now let's go to the main installation procedure

Insert NanoHat OLED related

Reference: http://wiki.friendlyarm.com/wiki/index.php/NanoHat_OLED

Follow the steps below to install. (Installation destination is / root) NanoHat OLED operates in two configurations, a C program and a Python program, and the OLED display itself is closed in Python, but the key operation is notified to the Python side after being received by the C program side. It was.

And, on the C program side, the Python program is also started, so that the standard product is not started. For the time being, it is commented out in the example below. (Alternative Python code is launched from rc.local)

cd /root git clone https://github.com/friendlyarm/NanoHatOLED.git nano /root/NanoHatOLED/Source/main.c

/root/NanoHatOLED/Source/main.c


・
・
・
int load_python_view() {
/*
    int ret;
    char* cmd = (char*)malloc(255);
    sprintf(cmd, "cd %s/BakeBit/Software/Python && python %s 2>&1 | tee /tmp/nanoled-python.log", workpath, python_file);

    ret = pthread_create(&view_thread_id, NULL, (void*)threadfunc,cmd);
    if(ret) {
        log2file("create pthread error \n");
        return 1;
    }
//*/
    return 0;
}
・
・
・

cd NanoHatOLED ./install.sh

Enter the main subject (OLED display)

Install including externally dependent packages so that it starts automatically in rc.local. Since the automatic start (oled-start) of NanoHat OLED has already been written, I'll add a start command on the next line.

apt-get install fonts-takao-pgothic apt-get install python-mutagen python3-mutagen

cd /root git clone https://github.com/blue777/NanoPi-NEO_OLED_MPD

nano /etc/rc.local

/etc/rc.local


・
・
・
/usr/local/bin/oled-start &
cd /root/NanoPi-NEO_OLED_MPD
python oled_mpd.py &
・
・
・

5. Install ympd (MPD Web GUI)

Try to access http from each client on the network and put ympd so that you can control it.

python


apt install cmake
apt install libmpdclient-dev
apt install libssl-dev

cd /usr/lib
git clone https://github.com/notandy/ympd
cd ympd
mkdir -p build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX_PATH=/usr
make
make install 

It starts with ./ympd, but it is difficult to start each time, so edit rc.local so that it starts automatically.

nano /etc/rc.local

/etc/rc.local


・
・
・
/usr/lib/ympd/build/ympd &

The access port will be 8080, so http://YourMpdServer:8080/ Access in the form of.

If port80 is good, specify as follows.

./ympd -w 80

This is the end of the server side. Please reboot and enjoy.

reboot

Client side preparation

Playback can be controlled by web access, but the dedicated app is still good! In that case, please insert your favorite MPD client.

Various MPD clients such as iOS, Windows and Android are available. If you search with the MPD client, you will find various things, so install the one you like.

We are using gmpc because it is a Windows machine. https://gmpclient.org/

image.png

Items set in audio_output can be selected from the Server menu.

bonus

About NanoHat PCM5102a

In the standard state, it is in the De-Emphasis, Low-Latency state in terms of hardware. In particular, De-Emphasis has a high frequency cut at 44.1KHz, and it sounds a little muffled. It is recommended to solder the De-emphasis and Low-Latency areas.

As for sound quality, it's usually good. A 100,000-class CD player will not be able to compete.

However, if the sounds (such as the peak of full-scale) are intricately intertwined, the resolution reduction becomes noticeable and becomes a dumpling. However, since the power supply is a switching power supply and is shared by NanoPi-NEO, it will surely awaken if you modify another power supply.

NanoPi-NEO, NEO2 I2S signals

Reference http://wiki.friendlyarm.com/wiki/index.php/NanoPi_NEO2

The I2S signal output by NanoPi-NEO is

3 are output, but NanoPi-NEO2 is in addition to the above

Can also be output and will be output to GPIOA6. This is a specification only for H5, and it seems that H3 does not support it. (As far as you can see the data sheet)

And if you look further, it says PWM on GPIO A6, but neither H3 / H5 can be the output destination of PWM. I'm wondering where the typographical error came from (H2 + or something?)

I'm hoping to provide a driver that can secretly output I2S_MCLK.

■ 2018/01/13 added I was finally able to output! MCLK output & 384kHz support with I2S of NanoPi-NEO2

Sound quality of NanoPi NEO system and BeagleBone system

The reason for using MPD in SBC (Single Board Computing) is undoubtedly the pursuit of sound quality. So, the personal subjective evaluation of the sound quality of the NanoPi series and the BeagleBone series is like this.

NanoPi-NEO < NanoPi-NEO2 << BeagleBoneBlack <= BeagleBoneGreen

Both are evaluated by connecting with SBC --- (USB)-> JAVS X-DDC (revised) --- (Coaxial spdif)-> DAC, and SBC and X-DDC are evaluated with Infineon's SiC SBD. A series power supply assembled by bridge rectification (LM350 on the SBC side and Cree's SiC FET discrete on the X-DDC) is used. In this case, the BeagleBone type or one lace curtain will come out clearly. The sound is also fine.

Also, I am very pleased that lightMPD, a distribution dedicated to MPD, has supported NanoPi-NEO and NanoPi-NEO2.

Finally

It is around the display of OLED that I wrote variously, such as that it was better than I expected, but it is not used in my production environment. (Lol

Recommended Posts

Music playback server with NanoPi-NEO, MPD and OLED
Launch a web server with Python and Flask
GRPC starting with Go server and Dart client
Control music playback on a smartphone connected to Raspberry Pi 3 and bluetooth with AVRCP
Create a home music server with Centos8 + Universal Media Server