2

I'm JavaScript beginner. I am not English native and I'm sorry for my poor English.

Now I want to do sound visualization with Node.js + Typescript, but I don't know how to load sound in setup(). Please teach me how to load sound in Node.js + Typescript.

I tried importing 'p5/lib/addons/p5.sound'. But I don't know how to use this module in sketch of below code.

main.ts

import * as p5 from 'p5';

const sketch = (p: p5) => {
  p.setup = () => {
    p.resizeCanvas(100, 100);
    // want to load sound here
  };
  p.draw = () => {
    p.background(100);
  }
};

const sketchP = new p5(sketch);

global.d.ts

import module = require('p5');

export = module;
export as namespace p5;
declare global {
    interface Window {
        p5: typeof module
    }
}
2
  • To load a sound with p5.js you use loadSound. Take a look at p5js.org/examples/sound-load-and-play-sound.html Commented May 18, 2019 at 22:40
  • Thank you for your comment! I understand that I load sound using loadSound(). However, method loadSound() is the module 'p5/lib/addons/p5.sound' but p in the above code is the module 'p5'. So I could not load sounds only to write loadSound() in setup simply. Commented May 19, 2019 at 6:54

1 Answer 1

5

Now I resolve the problem. Thank you.

main.ts

require('p5/lib/addons/p5.sound')

const sketch = function(p: p5) {
  let sound: p5.SoundFile;

  p.preload = () => {
    const loadSound = (path: string) =>
            ((p as any) as p5.SoundFile).loadSound(path);
    sound = loadSound("../audio/demo.mp3")
  };

  p.setup = () => {
    p.resizeCanvas(100, 100);
    sound.play()
  };
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.