分析来自麦克风输入节点js

时间:2018-12-16 19:51:28

标签: node.js audio-processing

我有代码可以实时分析流文件中的频率数据。我试图弄清楚如何进行转换,因此可以从Mac上的行输入中分析数据,而不是从文件中流式传输。

我拥有的分析代码是:

const fs = require('fs');
const fsa = require('fs-extra');
const Lame     = require('lame');
const Speaker  = require('speaker');
const Volume   = require('pcm-volume');
const Analyser = require('audio-analyser')
const request  = require('request')
const Chunker  = require('stream-chunker');
const { YIN } = require('node-pitchfinder')
const detectPitch = YIN({ sampleRate: 44100})
//const BUFSIZE  = 64;
const BUFSIZE  = 10000;


var decoder   = new Lame.Decoder();	
decoder.on('format', function(format){onFormat(format)});

var chunker  = Chunker(BUFSIZE);
chunker.pipe(decoder);	





var audio_stream = fs.createReadStream('./2000.mp3');

audio_stream.pipe(chunker);

function onFormat(format)
{
	//if (volume == "undefined")
	volume = 1.0;
	
	vol      = new Volume(volume);
	speaker  = new Speaker(format);
	analyser = createAnalyser(format);
	analyser.on('data', sample);
	
	console.log(format);
	vol.pipe(speaker);	
	vol.pipe(analyser);	
	decoder.pipe(vol);
	vol.setVolume(volume);
}


function createAnalyser(format)
{
	return new Analyser({
		fftSize: 8,
	        bufferSize: BUFSIZE,
	        'pcm-stream': {
			channels: format.channels,
			sampleRate: format.sampleRate,
			bitDepth: format.bitDepth
		}
	});
}


var logFile = 'log.txt';
var logOptions = {flag: 'a'};

function sample()
{

	if (analyser) {

		const frequency = detectPitch(analyser._data)
  		console.log(frequency)
	}
}

这很好用,但是我不确定如何将其转换为监听麦克风输入。我尝试使用npm'mic'模块,但未成功。

谢谢

0 个答案:

没有答案