### Configure Build Environment Source: https://github.com/streampunk/naudiodon/blob/master/portaudio/mac-build.txt Sets the macOS deployment target for the build process. This ensures compatibility with older macOS versions. ```bash ./configure MACOSX_DEPLOYMENT_TARGET=10.6 ``` -------------------------------- ### Copy PortAudio Library Source: https://github.com/streampunk/naudiodon/blob/master/portaudio/mac-build.txt Copies the compiled PortAudio dynamic library from its build location to the specified directory within the Naudiodon project structure. ```bash cp libs/.libs/libportaudio.dylib ../naudiodon/portaudio/bin/ ``` -------------------------------- ### Update Library Installation Name Source: https://github.com/streampunk/naudiodon/blob/master/portaudio/mac-build.txt Modifies the installation name of the PortAudio dynamic library to use a relative path. This is crucial for ensuring the library can be found at runtime. ```bash install_name_tool -id @rpath/libportaudio.dylib portaudio/bin/libportaudio.dylib ``` -------------------------------- ### Control Audio Stream Playback and Recording Source: https://context7.com/streampunk/naudiodon/llms.txt Demonstrates stream control methods: start(), quit(), and abort() for managing audio playback. It pipes audio from a file to the output stream and handles stream events. Dependencies: 'naudiodon', 'fs'. ```javascript const portAudio = require('naudiodon'); const fs = require('fs'); const ao = new portAudio.AudioIO({ outOptions: { channelCount: 2, sampleFormat: portAudio.SampleFormat16Bit, sampleRate: 48000, deviceId: -1 } }); const audioFile = fs.createReadStream('audio.wav'); audioFile.pipe(ao); // start() - Begin streaming audio ao.start(); console.log('Playback started'); // Handle events ao.on('error', err => console.error('Error:', err)); ao.on('closed', () => console.log('Stream closed')); ao.on('finished', () => console.log('Stream finished')); // quit() - Graceful stop (processes remaining data) // Optional callback executes when quit completes setTimeout(() => { ao.quit(() => { console.log('Gracefully stopped after processing buffer'); }); }, 5000); // abort() - Immediate stop (discards pending data) // Use for immediate stop without processing remaining audio process.on('SIGINT', () => { ao.abort(() => { console.log('Immediately aborted'); process.exit(0); }); }); ``` -------------------------------- ### Stop Naudiodon Recording Source: https://github.com/streampunk/naudiodon/blob/master/README.md Provides an example of how to gracefully stop an ongoing audio recording using Naudiodon by calling the `quit()` method, typically triggered by a process interrupt signal. ```javascript process.on('SIGINT', () => { console.log('Received SIGINT. Stopping recording.'); ai.quit(); }); ``` -------------------------------- ### List Host Audio APIs in Node.js using Naudiodon Source: https://github.com/streampunk/naudiodon/blob/master/README.md Fetches a list of available host audio APIs on the system, such as MME or Core Audio. This function returns an object containing the default host API and an array of host API details, including their IDs, names, device counts, and default input/output devices. The default input/output device IDs can be used to specify devices for audio operations. ```javascript var portAudio = require('naudiodon'); console.log(portAudio.getHostAPIs()); ``` -------------------------------- ### Naudiodon Sample Format Constants and Usage Source: https://context7.com/streampunk/naudiodon/llms.txt Demonstrates the use of Naudiodon's sample format constants (e.g., `SampleFormat16Bit`) for configuring audio output. These constants define the bit depth and data type of audio samples, allowing for precise control over audio quality. ```javascript const portAudio = require('naudiodon'); // Available sample formats console.log(portAudio.SampleFormatFloat32); // 1 - 32-bit floating point (-1.0 to +1.0) console.log(portAudio.SampleFormat8Bit); // 8 - 8-bit signed integer console.log(portAudio.SampleFormat16Bit); // 16 - 16-bit signed integer (CD quality) console.log(portAudio.SampleFormat24Bit); // 24 - 24-bit signed integer (studio quality) console.log(portAudio.SampleFormat32Bit); // 32 - 32-bit signed integer // Usage in AudioIO configuration const ao = new portAudio.AudioIO({ outOptions: { channelCount: 2, sampleFormat: portAudio.SampleFormat16Bit, // CD quality 16-bit audio sampleRate: 44100 } }); ``` -------------------------------- ### Play Audio with Naudiodon Source: https://github.com/streampunk/naudiodon/blob/master/README.md Plays audio data by creating an AudioIO instance with output options and piping a readable stream into it. Requires the 'fs' and 'naudiodon' modules. Outputs audio to the default audio device. ```javascript const fs = require('fs'); const portAudio = require('naudiodon'); // Create an instance of AudioIO with outOptions (defaults are as below), which will return a WritableStream var ao = new portAudio.AudioIO({ outOptions: { channelCount: 2, sampleFormat: portAudio.SampleFormat16Bit, sampleRate: 48000, deviceId: -1, // Use -1 or omit the deviceId to select the default device closeOnError: true // Close the stream if an audio error is detected, if set false then just log the error } }); // Create a stream to pipe into the AudioOutput // Note that this does not strip the WAV header so a click will be heard at the beginning var rs = fs.createReadStream('steam_48000.wav'); // Start piping data and start streaming rs.pipe(ao); ao.start(); ``` -------------------------------- ### Record Audio with Naudiodon Source: https://github.com/streampunk/naudiodon/blob/master/README.md Records audio data by creating an AudioIO instance with input options and piping the resulting readable stream to a write stream. Requires 'fs' and 'naudiodon' modules. Outputs raw audio data to a specified file. ```javascript var fs = require('fs'); var portAudio = require('../index.js'); // Create an instance of AudioIO with inOptions (defaults are as below), which will return a ReadableStream var ai = new portAudio.AudioIO({ inOptions: { channelCount: 2, sampleFormat: portAudio.SampleFormat16Bit, sampleRate: 44100, deviceId: -1, // Use -1 or omit the deviceId to select the default device closeOnError: true // Close the stream if an audio error is detected, if set false then just log the error } }); // Create a write stream to write out to a raw audio file var ws = fs.createWriteStream('rawAudio.raw'); //Start streaming ai.pipe(ws); ai.start(); ``` -------------------------------- ### Generate Sine Wave Audio Buffer and Play Source: https://context7.com/streampunk/naudiodon/llms.txt Generates an 8-bit signed sine wave buffer programmatically and plays it to the audio output stream. It handles back-pressure by resuming writes when the buffer drains. Dependencies: 'naudiodon'. ```javascript const portAudio = require('naudiodon'); // Create output stream for generated audio const sampleRate = 44100; const ao = new portAudio.AudioIO({ outOptions: { channelCount: 1, sampleFormat: portAudio.SampleFormat8Bit, sampleRate: sampleRate, deviceId: -1 } }); // Generate a sine wave buffer const frequency = 440; // A4 note const duration = 1; // 1 second const tableSize = Math.floor(sampleRate / frequency); const buffer = Buffer.allocUnsafe(tableSize); for (let i = 0; i < tableSize; i++) { // Generate sine wave samples (8-bit signed: -128 to 127) buffer[i] = Math.sin((i / tableSize) * Math.PI * 2.0) * 127; } // Write buffer repeatedly with back-pressure handling function playTone(writer, data, repetitions) { let count = repetitions; const write = () => { let ok = true; while (count > 0 && ok) { count--; if (count === 0) { writer.end(data, () => console.log('Tone finished')); } else { ok = writer.write(data); } } if (count > 0) { writer.once('drain', write); // Resume when buffer drains } }; write(); } // Play 440Hz tone for ~5 seconds ao.start(); playTone(ao, buffer, Math.floor(sampleRate / tableSize * 5)); process.on('SIGINT', () => ao.quit()); ``` -------------------------------- ### List Audio Devices in Node.js using Naudiodon Source: https://github.com/streampunk/naudiodon/blob/master/README.md Retrieves a list of all supported audio devices on the system. The output includes device IDs, names, channel counts, default sample rates, and latency information. The device ID can be used to specify a particular device for audio operations. ```javascript var portAudio = require('naudiodon'); console.log(portAudio.getDevices()); ``` -------------------------------- ### Audio Input Stream for Recording with Naudiodon Source: https://context7.com/streampunk/naudiodon/llms.txt Creates a readable stream for audio recording using Naudiodon. Audio data can be piped to files or processed in real-time. Each buffer includes a timestamp property for synchronization. ```javascript const fs = require('fs'); const portAudio = require('naudiodon'); // Create audio input stream for recording const ai = new portAudio.AudioIO({ inOptions: { channelCount: 2, // Stereo recording sampleFormat: portAudio.SampleFormat16Bit, // 16-bit audio sampleRate: 44100, // 44.1kHz (CD quality) deviceId: -1, // -1 = default input device closeOnError: true, // Close on audio errors highwaterMark: 16384 // Buffer size } }); // Record to a raw audio file const outputFile = fs.createWriteStream('recording.raw'); ai.pipe(outputFile); // Access timestamp on each buffer ai.on('data', buf => { console.log('Buffer timestamp:', buf.timestamp); console.log('Buffer size:', buf.length, 'bytes'); }); // Start recording ai.start(); console.log('Recording... Press Ctrl+C to stop'); // Stop recording on Ctrl+C process.on('SIGINT', () => { console.log('Stopping recording...'); ai.quit(() => { console.log('Recording saved to recording.raw'); process.exit(0); }); }); ``` -------------------------------- ### List Audio Host APIs with Naudiodon Source: https://context7.com/streampunk/naudiodon/llms.txt Retrieves information about the audio host APIs available on the system, such as CoreAudio, WASAPI, ALSA, and MME, using `getHostAPIs()`. This function helps in selecting specific audio backends and identifying default devices for each API. ```javascript const portAudio = require('naudiodon'); // Get host API information const hostAPIs = portAudio.getHostAPIs(); console.log(hostAPIs); // Example output: // { // defaultHostAPI: 0, // HostAPIs: [ // { // id: 0, // name: 'MME', // type: 'MME', // deviceCount: 8, // defaultInput: 1, // defaultOutput: 5 // }, // { // id: 1, // name: 'Windows WASAPI', // type: 'WASAPI', // deviceCount: 6, // defaultInput: 0, // defaultOutput: 3 // } // ] // } // Use default devices from a specific host API const wasapiAPI = hostAPIs.HostAPIs.find(api => api.type === 'WASAPI'); if (wasapiAPI) { console.log('WASAPI default input device:', wasapiAPI.defaultInput); console.log('WASAPI default output device:', wasapiAPI.defaultOutput); } ``` -------------------------------- ### List Available Audio Devices with Naudiodon Source: https://context7.com/streampunk/naudiodon/llms.txt Retrieves a list of all audio devices available on the system using Naudiodon's `getDevices()` function. The output includes device capabilities like channel counts, sample rates, and latency. This information is crucial for selecting specific input or output devices for audio operations. ```javascript const portAudio = require('naudiodon'); // Get all available audio devices const devices = portAudio.getDevices(); console.log(devices); // Example output: // [ // { // id: 0, // name: 'Built-in Microphone', // maxInputChannels: 2, // maxOutputChannels: 0, // defaultSampleRate: 44100, // defaultLowInputLatency: 0.00199546485260771, // defaultLowOutputLatency: 0.01, // defaultHighInputLatency: 0.012154195011337868, // defaultHighOutputLatency: 0.1, // hostAPIName: 'Core Audio' // }, // { // id: 1, // name: 'Built-in Output', // maxInputChannels: 0, // maxOutputChannels: 2, // defaultSampleRate: 44100, // defaultLowInputLatency: 0.01, // defaultLowOutputLatency: 0.002108843537414966, // defaultHighInputLatency: 0.1, // defaultHighOutputLatency: 0.012267573696145125, // hostAPIName: 'Core Audio' // } // ] // Find devices by capability const inputDevices = devices.filter(d => d.maxInputChannels > 0); const outputDevices = devices.filter(d => d.maxOutputChannels > 0); console.log('Input devices:', inputDevices.map(d => d.name)); console.log('Output devices:', outputDevices.map(d => d.name)); ``` -------------------------------- ### Duplex Stream for Simultaneous Audio I/O with Naudiodon Source: https://context7.com/streampunk/naudiodon/llms.txt Creates a duplex stream for simultaneous audio input and output using Naudiodon. This is useful for real-time processing or monitoring. Both input and output streams must share the same sample rate. ```javascript const portAudio = require('naudiodon'); // Create duplex stream for audio loopback (monitoring) const aio = new portAudio.AudioIO({ inOptions: { channelCount: 2, sampleFormat: portAudio.SampleFormat16Bit, sampleRate: 44100, deviceId: -1, // Default input closeOnError: false // Continue on minor errors }, outOptions: { channelCount: 2, sampleFormat: portAudio.SampleFormat16Bit, sampleRate: 44100, // Must match input sample rate deviceId: -1, // Default output closeOnError: false } }); // Simple audio passthrough (loopback) aio.pipe(aio); // Or with separate input/output streams for processing const ai = new portAudio.AudioIO({ inOptions: { channelCount: 2, sampleFormat: portAudio.SampleFormat16Bit, sampleRate: 44100, deviceId: -1 } }); const ao = new portAudio.AudioIO({ outOptions: { channelCount: 2, sampleFormat: portAudio.SampleFormat16Bit, sampleRate: 44100, deviceId: -1 } }); // Pipe input to output with delayed start ai.pipe(ao); ai.once('data', () => ao.start()); // Start output when input data arrives ai.start(); console.log('Audio loopback active. Press Ctrl+C to stop'); process.on('SIGINT', () => ai.quit()); ``` -------------------------------- ### Bi-directional Audio with Naudiodon Source: https://github.com/streampunk/naudiodon/blob/master/README.md Establishes a bi-directional audio stream by configuring an AudioIO instance with both input and output options. This creates a Node.js Duplex stream for simultaneous audio input and output. ```javascript var portAudio = require('../index.js'); // Create an instance of AudioIO with inOptions and outOptions, which will return a DuplexStream var aio = new portAudio.AudioIO({ inOptions: { channelCount: 2, sampleFormat: portAudio.SampleFormat16Bit, sampleRate: 44100, deviceId: -1 // Use -1 or omit the deviceId to select the default device }, outOptions: { channelCount: 2, sampleFormat: portAudio.SampleFormat16Bit, sampleRate: 44100, deviceId: -1 // Use -1 or omit the deviceId to select the default device } }); aio.start(); ``` -------------------------------- ### Audio Output Stream with Naudiodon Source: https://context7.com/streampunk/naudiodon/llms.txt Creates a writable stream for audio playback using Naudiodon. Audio data can be piped from various sources like files or network streams. It supports back-pressure and includes error handling. ```javascript const fs = require('fs'); const portAudio = require('naudiodon'); // Create audio output stream with configuration const ao = new portAudio.AudioIO({ outOptions: { channelCount: 2, // Stereo sampleFormat: portAudio.SampleFormat16Bit, // 16-bit audio sampleRate: 48000, // 48kHz sample rate deviceId: -1, // -1 = default device closeOnError: true, // Close stream on audio errors highwaterMark: 16384 // Buffer size in bytes } }); // Handle stream events ao.on('error', err => console.error('Audio error:', err)); ao.once('finish', () => console.log('Playback finished')); // Play audio from a WAV file const audioFile = fs.createReadStream('audio.wav'); audioFile.pipe(ao); ao.start(); // Graceful shutdown on Ctrl+C process.on('SIGINT', () => { console.log('Stopping playback...'); ao.quit(() => { console.log('Playback stopped'); process.exit(0); }); }); ``` -------------------------------- ### Access Timestamp in Naudiodon Recording Source: https://github.com/streampunk/naudiodon/blob/master/README.md Demonstrates how to access the 'timestamp' property from data buffers during audio recording with Naudiodon. This property indicates the time value for the first sample in the buffer. ```javascript ai.on('data', buf => console.log(buf.timestamp)); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.