0
2

More than 1 year has passed since last update.

WSLのUbuntuでPC内の音楽データをGoogleHomeで操作する

Last updated at Posted at 2018-12-27

はじめに

●PC内の音楽データを声で操作し、PCスピーカで再生できるようにする。
【操作例】GoogleHomeに「音楽 ユーミン」と話す

環境

●Windows10 HOMEのPCにWSLのubuntuをインストールする。
●ubuntuでnpm,node.js,firebase,mplayerをインストールする
●iftttでGoogleアシスタント(this)でfirebaseにテキストをPUTする(that)ように設定する
●firebaseのDatabaseはgooglehome5-wordにしています
●WSLのubuntuでpulseaudioでPCのスピーカを使うようにする
●Windows側でpulseaudioサーバのインストールが必要です
 https://www.cendio.com/thinlinc/download の 「Client Bundle」のリンクからダウンロードできます

プログラム(node.js)

●firebaseのKeyは自分のKeyに置き換えてください
●Googlehomeからのテキストが「停止」、「止めて」以外の場合、音楽ファイルリストを検索して再生します。複数の曲がヒットした場合は、続けて再生します。
●音楽データのリストを予めフリーソフトで作成してください。(list.txt)
"/mnt/j/music/松任谷由実/日本の恋と、ユーミン/01 やさしさに包まれたな.mp3"
"/mnt/j/music/松任谷由実/日本の恋と、ユーミン/01 リフレインが叫んでる.mp3"
"/mnt/j/music/松任谷由実/日本の恋と、ユーミン/01 真珠のピアス.mp3"

var firebase = require("/home/ユーザ名/node_modules/firebase");
require("/home/ユーザ名/node_modules/firebase");
var iconv = require('/home/ユーザ名/node_modules/iconv-lite');
var value1 ;
var str1 = '" | xargs -n1 mplayer'

//firebase config
var config = {
    apiKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    authDomain: "xxxxxxxxxx.firebaseapp.com",
    databaseURL: "https://xxxxxxxxxx.firebaseio.com",
    projectId: "xxxxxxxxxx",
    storageBucket: "xxxxxxxxxx.appspot.com",
    messagingSenderId: "xxxxxxxxxxxxxx"
};

function sleep(time) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve();
    }, time);
  });
}

firebase.initializeApp(config);

//jsonからvalueに一致する値取得
const getJsonData = (value, json) => {
  for (var word in json)  if (value == word) return json[word]
  return json["default"]
}

//database更新時
const path = "/googlehome5"
const key = "word"
const db = firebase.database()
db.ref(path).on("value", function(changedSnapshot) {
//値取得
let value = changedSnapshot.child(key).val()
if (!value) return
console.log(value)
var value1 = value.replace(/ /g,"");

//コマンド
const STOP = "pkill mplayer";

if(value === "停止"){
  command = STOP
} else if(value === "止め て"){
  command = STOP
} else {
  command = "未定義" 
}
console.log(command)

//コマンド実行
var exec = require('child_process').exec;
exec(STOP, {maxBuffer: 1000*1024}, function(error, stdout, stderr) {
  if (error !== null) {
    console.log('Exec error: ' + error);
  }
});
sleep(2000);
if(command === "未定義") {
  command = 'cat /mnt/j/music/list.txt | grep "'  + value1 + '"';
  var exec = require('child_process').exec;
  exec(command, {maxBuffer: 40000*1024}, function(error, stdout, stderr) {
    if (error !== null) {
      console.log('指定された曲はありません');
    } else {
      console.log(stdout);
    }
  });

  command = 'cat /mnt/j/music/list.txt | grep "'  + value1 + str1;
  console.log(command)
  var exec = require('child_process').exec;
  exec(command, {maxBuffer: 40000*1024}, function(error, stdout, stderr) {
    if (error !== null) {
      console.log('Exec error: ' + error);
    }
  });
}
//firebase clear
db.ref(path).set({[key]: ""});
})

まとめ

●PC内の音楽データ(mp3)を声で操作でき便利です
●プログラムはラズパイでも同様に動作しますが、mplayerだと音飛びするのでmpg321に変えています。また、PCの音楽データのある場所をmountする必要があります。
●Googleアシスタントは英語で話してもカタカナになる場合が多いので、ファイル名が英語だと検索できないことが多いです。
(back numberはバックナンバになる)
フォルダまたはファイル名を検索しやすいようにするのがおすすめです。
 ジャズやクラシックなどのジャンルを入れる。歌手名は検索しやすいようにするなど。
●複数の曲がヒットした場合、pkill -KILL -f mplayerだと曲が停止します。
●複数の曲がヒットした場合、pkill mplayerだと再生中の曲が停止し、次の曲が再生します。

追記

shuffle再生版の記事を投稿しました。
WSLのUbuntuでPC内の音楽データをGoogleHomeで操作する(shuffle再生)

参考資料

GoogleHomeとIFTTTとFirebaseとラズパイとnode.jsでyoutubeの音を流す
WSLのUbuntuでyoutube音楽をGoogleHomeで操作する

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2