AudioNet / node-core-audio

Bindings for PortAudio giving JavaScript access to sound card samples (mostly unmaintained)
MIT License
330 stars 68 forks source link

Performance/Parallel Processing improvements #31

Closed anprogrammer closed 9 years ago

anprogrammer commented 10 years ago

When testing this library, I noticed that it was blocking Javascript from running for large portions of time. I discovered that the background thread doesn't actually do anything but lock up immediately after starting, meanwhile Pa_WriteStream (which blocks until the audio passed to it finishes playing) is being called from the main Javascript thread.

I've refactored the architecture, so that the "write" function simply stores the buffer, while the background thread sends said buffer to Port Audio. This way, JavaScript can run while the audio is played.

With these changes this library works rock-solid for the game I'm developing, and has good performance.

I've also added an option to disable microphone input, and a couple of code clean-up tweaks.

Let me know what you think.

ZECTBynmo commented 10 years ago

Thank you for looking at and working on the project! I really appreciate the help.

I'm trying to run some of my old scripts to test the changes out, and I'm having some trouble getting things running. Do you have a test script that runs that I could check out? I'd love to strip some of the old garbage out of the repository and have a nice example script.

anprogrammer commented 10 years ago

Thank you for this library, I would have had to spend a lot of time writing an audio solution if this didn't exist!

I'll work on putting together a new example script over the next few days so that you can see this in action and verify the functionality.

ZECTBynmo commented 10 years ago

I'm really looking forward to the example script @anprogrammer :smile:

anprogrammer commented 10 years ago

I just added an example script @ZECTBynmo After you "npm install node-core-audio" and "npm install node-qt" you can "node input_output_graphical_test.js"

It outputs sine waves which pan between left and right (cool effect with head-phones) to demonstrate audio output functionality. At the same time it displays the microphone input oscilloscope-style in real-time, which demonstrates that the audio is not significantly blocking the graphics loop.

Let me know if it works for you!

ZECTBynmo commented 9 years ago

Awesome! Thanks @anprogrammer. I'll take a look at this as soon as I get a chance.

anprogrammer commented 9 years ago

@ZECTBynmo Have you had a chance to test this?

I'm working on a game I'm planning to release early next week, so I may create a "node-core-audio-parallel" package in NPM so that people can easily install for my game. I'll of course get rid of that and reference the proper node-core-audio package once the pull request is in :)

ZECTBynmo commented 9 years ago

No need for that, I'll test it right now. Thanks for your patience. @anprogrammer

ZECTBynmo commented 9 years ago

@anprogrammer Can you add me as a contributor on your fork please? I'd like to make a couple commits before merging

ZECTBynmo commented 9 years ago

Actually, it's such a small change I can just make it after the merge. This looks great. Thank you so much for contributing! @anprogrammer

ZECTBynmo commented 9 years ago

@anprogrammer Version 0.4.0 is now live on npm :+1: Thanks again!

ZECTBynmo commented 9 years ago

@anprogrammer On another note: You might want to do some testing on mac, particularly a macbook pro with a retina display. For some reason, when I open the Qt window on my retina display, it starts to lag badly, to the point where the audio thread is totally lagged out and only emitting pulses of the sine wave. However, when I drag the window to my other (normal) monitor, everything goes back to normal. This might indicate there's a similar issue with your game.

anprogrammer commented 9 years ago

@ZECTBynmo Thanks for merging that (and cleaning up my lack of Linux/OSX support with sleep)! I don't have access to an OSX machine w/ Retina display, although I'm assuming it's some sort of inefficiency with QT. My game is actually pure OpenGL so I doubt it'll be affected. Once I have my game released, I can break part of it off to use as an improved test-script.

ZECTBynmo commented 9 years ago

No, thank you @anprogrammer! By the way your example is now here

If you need me to run something or give it a quick test during development, I'd be happy to help.