node.js 로 raspberry sound system 만들기

|

1. 싸운드 환경 설치


다음과 같은 순서로 유틸리티들을 설치합니다.

  • The ALSA utilities:
    sudo apt-get install alsa-utils
  • MP3 tools:
    sudo apt-get install mpg321
  • WAV to MP3 conversion tool:
    sudo apt-get install lame


사운드 드라이버를 로드해줍니다.


  • sudo modprobe snd-bcm2835

제대로 설치 됐는지 확인하려면


  • sudo lsmod | grep 2835

출력디바이스를 지정합니다.

Select the output device for sound (0=auto, 1=analog, 2=HDMI):

  • sudo amixer cset numid=3 1

1번은 일반핀입니다. 2번은 hdhi단자로 출력하고싶을때 사용합니다.


혹시 소리가 나지 않앗다면 다음과 같이해서 볼륨을 조절합니다.


sudo amixer cset numid=1 n



n 이 볼륨값입니다. 퍼센테이지로 0~100%  사의 값을 넣어 주거나 0~400사이 값을 넣어줄수있습니다.


테스트는 다음과 같이 합니다. 커멘드 창에서 아래 명령어들을 입력 해봅니다.


  • aplay /usr/share/sounds/alsa/Front_Center.wav
  • speaker-test -t sine -f 440 -c 2 -s 1
  • mpg321 “Mannish Boy.mp3″

aplay 로는 wav 만 연주 가능합니다. mp3 는 mpg321 로 하셔야합니다.


기타 사항입니다.


It you get the following error message:

  • ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front

Edit the file /usr/share/alsa/alsa.conf:

  • sudo nano /usr/share/alsa/alsa.conf
  • change the line “pcm.front cards.pcm.front” to “pcm.front cards.pcm.default”

If you are using HDMI and cannot hear any audio at all change the following PIconfiguration setting:

  • edit the RasPI configuration file:
    sudo nano /boot/config.txt
  • uncomment the line:
    hdmi_drive=2
  • save the file and reboot the PI


출처 : 

http://cagewebdev.com/index.php/raspberry-pi-getting-audio-working/

http://www.raspberrypi-spy.co.uk/2013/06/raspberry-pi-command-line-audio/




2. 노드 플러그인 설치


alsa 개발 sdk 를 먼저 설치합니다.

sudo apt-get install libasound2-dev

npm install -g lame
npm install -g wav
npm install -g speaker


출처 :

http://nmecdesign.com/blog/audio-on-the-raspberry-pi-with-node-js/


3. 예제


var lame = require(theApp.module_path +'lame');

var wav = require(theApp.module_path +'wav');

var Speaker = require(theApp.module_path + 'speaker');


var wav_obj = {

                    file : fs.createReadStream('./effectsound/hit1.wav' ),

                    reader : new wav.Reader()

                };


                wav_obj.reader.on('format',function(format) {


                    //console.log(format);

                    wav_obj.speaker = new Speaker(format);


                    wav_obj.reader.pipe(wav_obj.speaker);




                });


                wav_obj.file.pipe(wav_obj.reader);


mp 의 경우는 reader stream을 lame 으로 하면된다.



And