【问题标题】:Integrating p5.sound.js in Meteor: p5.AudioIn() is not a constructor在 Meteor 中集成 p5.sound.js:p5.AudioIn() 不是构造函数
【发布时间】:2016-05-08 17:48:20
【问题描述】:

我尝试在 Meteor 1.2.1 中集成 p5.sound.js

我想使用 p5js 功能在我的模板中录制音频,但我总是收到一条错误消息。让我逐步向您展示我的工作:

  1. 集成 p5 库

我已将p5.min.jsp5.sound.js 放入Meteor 文件夹/client/compatibility/

  1. 录音草图

我想首先使用 p5 标准 Audio Recoding example 并通过定义一个全局变量 var sketch1; 来重新设计代码,该变量保存在 Meteor lib 文件夹中名为 global.js 的文件中,而整个草图sketch1.js 只是保存在 client 文件夹中。这是我的草图的样子:

/////////////////
///p5js tryaudio recording
/////////////////
sketch1 = function(s){
  var mic, recorder, soundFile;

  var state = 0; // mousePress will increment from Record, to Stop, to Play

  s.setup = function() {
    s.createCanvas(400,400);
    s.background(200);
    s.fill(0);
    s.text('Enable mic and click the mouse to begin recording', 20, 20);

    // create an audio in
    mic = new p5.AudioIn();

    // users must manually enable their browser microphone for recording to work properly!
    mic.start();

    // create a sound recorder
    recorder = new p5.SoundRecorder();

    // connect the mic to the recorder
    recorder.setInput(mic);

    // create an empty sound file that we will use to playback the recording
    soundFile = new p5.SoundFile();
  }

  s.mousePressed = function() {
    // use the '.enabled' boolean to make sure user enabled the mic (otherwise we'd record silence)
    if (state === 0 && mic.enabled) {

      // Tell recorder to record to a p5.SoundFile which we will use for playback
      recorder.record(soundFile);

      s.background(255,0,0);
      s.text('Recording now! Click to stop.', 20, 20);
      state++;
    }

    else if (state === 1) {
      recorder.stop(); // stop recorder, and send the result to soundFile

      s.background(0,255,0);
      s.text('Recording stopped. Click to play & save', 20, 20);
      state++;
    }

    else if (state === 2) {
      soundFile.play(); // play the result!
      saveSound(soundFile, 'mySound.wav'); // save file
      state++;
    }
  }
}
  1. 将草图集成到模板中

我的模板名为tryaudiolist.html,它只是将草图集成到带有id="s" 的<div> 标记中,如下所示:

<template name="tryaudiolist">
			<div class="container">
				<div class="row">
					<div class="col-md-12">
						<p>the sketch works here: </p>
					</div>
					<div id="s"></div>
				</div>

			</div>
</template>
  1. client.js 文件

client.js 文件中,我使用 Meteor onRendered() 函数将草图渲染到模板中。这是代码sn-p:

Template.tryaudiolist.onRendered(function() {
    	console.log("entering onCreated");
    	var myp5 = new p5(sketch1, "s");
    
})
  1. 控制台错误

当我尝试运行应用程序时,代码被渲染并且画布按预期由 p5js 构建:

<div id="s">
  <canvas id="defaultCanvas0" data-hidden="true" width="400" height="400"     style="visibility: hidden; width: 400px; height: 400px;"></canvas>
</div>

但是,当输入 onRendered() 函数时(请参阅我的 console.log 语句 client.js:37 entering onCreated),它会抱怨 p5.AudioIn() 方法:

sketch1.js:1 Uncaught SyntaxError: Unexpected token <
client.js:37 entering onCreated
debug.js:41 Exception from Tracker afterFlush function:
debug.js:41 Error: error connecting to node: [object GainNode]
    at ScriptProcessorNode.AudioNode.connect (p5.sound.js:2976)
    at new p5.Amplitude (p5.sound.js:2229)
    at new p5.AudioIn (p5.sound.js:6327)
    at s.setup (sketch1.js:17)
    at .<anonymous> (p5.min.js:5)
    at .<anonymous> (p5.min.js:5)
    at new o (p5.min.js:5)
    at .<anonymous> (client.js:38)
    at template.js:116
    at Function.Template._withTemplateInstanceFunc (template.js:457)
sketch1.js:34 Uncaught TypeError: Cannot read property 'enabled' of undefined

这里,s.setup (sketch1.js:17) 指的是我的对象定义mic = new p5.AudioIn();,不例外。在sketch1.js:34 Uncaught TypeError: Cannot read property 'enabled' of undefined 中相同,它无法识别mic 被实例化为p5.AudioIn() 对象。最后在.&lt;anonymous&gt; (client.js:38) 中,控制台抱怨的是 p5 对象。

  1. 总结

如您所见,我尝试在 Meteor 中实现标准 p5 Record Save Audio example。但还没有成功。

我不知道我该如何解决这个问题?是引用错误吗?我是否可能使用错误的语法来访问对象?

【问题讨论】:

  • 您的sketch1.js 文件中是否还有其他未在此处显示的内容?使用此代码,我没有在控制台中收到该错误消息。
  • 不,其实这里的sketch1.js是完整的。
  • 但是我没有向你展示所有控制台的错误信息。就像有一个“p5.js 的 exp() 函数错误”(p5 想让我在 setup() 中初始化所有 var --> 这就是我所做的)和一个 SyntaxError: Unexpected token
  • @MarkLeiber:事实上,马克,你已经暴露了我的错误。谢谢!我的客户端文件夹中仍然有一个 head.html 文件,其中包含以下引用

标签: javascript audio meteor p5.js


【解决方案1】:

我终于发现了自己的错误。

  1. 我仍然在 head.html 文件夹中的 head.html 文件中引用了 p5.sound.min.js 的 CDN 在线存储库,这是我在切换到实现 Meteor 之前第一次尝试集成 p5js 时使用的onRendered() 函数。通过删除这个引用,sketch1.js 最终得到了正确渲染。

  2. saveSound(soundFile, 'mySound.wav') 方法未正确引用。我需要通过 p5 对象使用s.saveSound(soundFile, 'mySound.wav') 调用它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 2014-08-04
    • 2011-12-07
    • 1970-01-01
    相关资源
    最近更新 更多