Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node example #207

Open
GeoffreyPlitt opened this issue May 30, 2018 · 1 comment
Open

Node example #207

GeoffreyPlitt opened this issue May 30, 2018 · 1 comment

Comments

@GeoffreyPlitt
Copy link

Can I see a full example on NodeJS?

@lezsakdomi
Copy link

This code works for me:

                    (async (data) => {
                        if (Buffer.isBuffer(data)) {
                            //console.log("Received object is binary data.")
                            //console.log("Trying to play as an audio file...")
                            try {
                                const AV = require('av')
                                const createBuffer = require('audio-buffer-from')
                                const play = require('audio-play')
                                require('mp3')
                                
                                const asset = AV.Asset.fromBuffer(data)

                                asset.on('buffer', percent => console.log("Loading audio file... %d%", percent))
                                //asset.on('format', o => console.log("Audio format: %o", o))
                                asset.on('duration', ms => console.log("Audio duration: %fs", ms/1000))
                                //asset.on('metadata', o => console.log("Audio metadata: %o", o))
                                //asset.on('decodeStart', () => console.log("Audio decoding started."))
                                ////asset.on('data', data => console.log("Audio data acquired with length of %d", data.length))
                                //asset.on('data', data => process.stdout.write('.'))
                                //asset.on('error', err => console.log("Audio processing failed: %o", err))
                                
                                const formatPromise = new Promise((resolve, reject) => {
                                    //asset.get('format', resolve)
                                    asset.on('format', resolve)
                                    asset.on('error', reject)
                                })
                                const arrayBufferPromise = new Promise((resolve, reject) => {
                                    asset.decodeToBuffer(resolve)
                                    asset.on('error', reject)
                                })

                                //arrayBufferPromise.then(array => console.log("Raw audio buffer length: %d", array.length))

                                try {
                                    const format = await formatPromise;
                                    const audioBuffer = await createBuffer(await arrayBufferPromise, {
                                        channels: format.channelsPerFrame,
                                        sampleRate: format.sampleRate,
                                        format: {
                                            sampleRate: format.sampleRate,
                                            channels: format.channelsPerFrame,
                                            type: format.floatingPoint ? 'float32' : 'array',
                                        },
                                    })

                                    util.inspect(audioBuffer)

                                    try {
                                        await play(audioBuffer)
                                        //console.log("Playback succeeded")
                                    } catch (e) {
                                        console.error("Failed to play audio: %o", e)
                                    }
                                } catch (e) {
                                    //console.error("Failed to decode audio: %o", e)
                                    console.log("Failed to play as audio")
                                }
                            } catch (e) {
                                console.error("Failed to prepare audio playback: %o", e)
                            }
                        }
                    })(data)

While this uses a separate library for audio playback, the decoding is still done using AV.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants