Last active
November 20, 2022 16:23
-
-
Save nishioka/b732bd7cd4080ccb2610 to your computer and use it in GitHub Desktop.
node.jsで棒読みちゃんにしゃべらせる
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var net = require('net'); | |
var client = new net.Socket(); | |
client.connect('50001', 'localhost', function(){ | |
console.log('client-> connected to server'); | |
var iCommand = new Buffer(2); | |
iCommand.writeInt16LE(1, 0); //コマンド( 0:メッセージ読み上げ) | |
console.log('iCommand', iCommand); | |
client.write(iCommand); | |
var iSpeed = new Buffer(2); | |
iSpeed.writeInt16LE(-1, 0); //速度 (-1:棒読みちゃん画面上の設定) | |
console.log('iSpeed', iSpeed); | |
client.write(iSpeed); | |
var iTone = new Buffer(2); | |
iTone.writeInt16LE(-1, 0); //音程 (-1:棒読みちゃん画面上の設定) | |
console.log('iTone', iTone); | |
client.write(iTone); | |
var iVolume = new Buffer(2); | |
iVolume.writeInt16LE(-1, 0); //音量 (-1:棒読みちゃん画面上の設定) | |
console.log('iVolume', iVolume); | |
client.write(iVolume); | |
var iVoice = new Buffer(2); | |
iVoice.writeInt16LE(1, 0); //声質 ( 1:女性) | |
console.log('iVoice', iVoice); | |
client.write(iVoice); | |
var bCode = new Buffer(1); | |
bCode.writeInt8(0, 0); //文字列のbyte配列の文字コード(0:UTF-8, 1:Unicode, 2:Shift-JIS) | |
console.log('bCode', bCode); | |
client.write(bCode); | |
var bMessage = new Buffer('のーどJSからこんばんわ', 'utf8'); //文字列のbyte配列 | |
var iLength = new Buffer(4); | |
iLength.writeInt32LE(bMessage.length, 0); //文字列のbyte配列の長さ | |
console.log('iLength', iLength); | |
client.write(iLength); | |
client.write(bMessage); | |
console.log('bMessage', bMessage); | |
client.end(); | |
}); | |
client.on('close', function(){ | |
console.log('client-> connection is closed'); | |
}); |
使い方いずどこですか
僕はこういう関数にして使ってます。
const net = require("net");
const bclient = new net.Socket();
bclient.on("ready", async () => { console.log("接続しました。"); });
bclient.on("error", async e => { if (e) console.log(e); });
bclient.on("end", async () => { console.log("切断済みです。"); });
/**
* 棒読みちゃん送信に使いやすい関数
* @param {String} text 送信するテキストを入力
* @param {Number} speed 速度を入力
* @param {Number} tone 声の高さを入力
* @param {Number} volume 間で音量を入力
* @param {Number} voice 声の種類を入力
*/
const bsend = async (text, speed, tone, volume, voice) => {
if (bclient.connecting) return console.log("まだ送信が終わっていません。");
if (!text) text = "テキストがありません。";
if (!speed) speed = -1;
if (!tone) tone = -1;
if (!volume) volume = -1;
if (!voice) voice = 0;
console.log("次のテキストを送信します。:" + text);
bclient.connect("50001", "localhost");
let Command = new Buffer.allocUnsafe(2);
Command.writeInt16LE(1, 0);
bclient.write(Command);
let Speed = new Buffer.allocUnsafe(2);
Speed.writeInt16LE(speed, 0);
bclient.write(Speed);
let Tone = new Buffer.allocUnsafe(2);
Tone.writeInt16LE(tone, 0);
bclient.write(Tone);
let Volume = new Buffer.allocUnsafe(2);
Volume.writeInt16LE(volume, 0);
bclient.write(Volume);
let Voice = new Buffer.allocUnsafe(2);
Voice.writeInt16LE(voice, 0);
bclient.write(Voice);
let Code = new Buffer.allocUnsafe(1);
Code.writeInt8(0, 0);
bclient.write(Code);
let Message = new Buffer.from(text, "utf8");
let Length = new Buffer.allocUnsafe(4);
Length.writeInt32LE(Message.length, 0);
bclient.write(Length);
bclient.write(Message);
bclient.end();
};
setInterval(async () => {
bsend("のーどJSからこんばんわ", 100, 100, 100, 1); //送信
}, 1500); //これはテスト用に使っただけだからいらない
ありがとうございます!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ねむい