Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
hamitzor committed Jan 1, 2024
0 parents commit 3491545
Show file tree
Hide file tree
Showing 16 changed files with 4,293 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true

[*.{js,json,yml}]
charset = utf-8
indent_style = space
indent_size = 2
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.yarn/** linguist-vendored
/.yarn/releases/* binary
/.yarn/plugins/**/* binary
/.pnp.* binary linguist-generated
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions

node_modules
prebuilds
.vscode
build
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "vendor/rtaudio"]
path = vendor/rtaudio
url = https://github.com/thestk/rtaudio
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodeLinker: node-modules
70 changes: 70 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
cmake_minimum_required(VERSION 3.1)
if(UNIX AND NOT APPLE)
set(LINUX TRUE)
endif()

set(CMAKE_CXX_STANDARD 20)
project(rtaudio-js)
set(CMAKE_DEBUG_POSTFIX "")

file(GLOB_RECURSE SOURCES "src/*.cpp" "src/*.hpp")

add_library(${PROJECT_NAME} SHARED ${SOURCES} ${CMAKE_JS_SRC})

set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "")

if(WIN32)
set(RTAUDIO_API_DS ON)
set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX "-win32.node")
endif()

if(LINUX)
if(DEFINED ENV{RTAUDIO_JS_LINUX_ALSA_ONLY})
option(RTAUDIO_API_PULSE "" OFF)
option(RTAUDIO_API_JACK "" OFF)
set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX "-linux-alsa.node")
elseif(DEFINED ENV{RTAUDIO_JS_LINUX_PULSE_ALSA})
option(RTAUDIO_API_PULSE "" ON)
option(RTAUDIO_API_JACK "" OFF)
set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX "-linux-pulse-alsa.node")
else()
set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX "-linux-custom.node")
endif()
endif()

# Use relative RPATH=$ORIGIN for portability.
# This will allow the addon to use the librtaudio.so located in the same directory on unix-like systems.
# On Windows, rtaudio.dll in the same directory is already visible to the addon.
if(UNIX)
set_target_properties(${PROJECT_NAME} PROPERTIES BUILD_RPATH_USE_ORIGIN TRUE)
endif()

add_subdirectory(vendor/rtaudio rtaudio)
include_directories(vendor/rtaudio)

# Disable symlinks for portability.
if(UNIX)
set_property(TARGET rtaudio PROPERTY SOVERSION)
set_property(TARGET rtaudio PROPERTY VERSION)
endif()

target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_JS_INC})
add_compile_definitions(NAPI_ENABLE_CPP_EXCEPTIONS)
add_compile_definitions(NODE_ADDON_API_DISABLE_DEPRECATED)
add_compile_definitions(NAPI_VERSION=4)
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)

execute_process(COMMAND node -p "require('node-addon-api').include"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE NODE_ADDON_API_DIR
)

string(REGEX REPLACE "[\r\n\"]" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})

target_include_directories(${PROJECT_NAME} PRIVATE ${NODE_ADDON_API_DIR})

target_link_libraries(${PROJECT_NAME} ${OPENSSL_LIBS} ${CMAKE_JS_LIB} rtaudio)

if(MSVC AND CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET)
execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS})
endif()
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2023 Hamit Can Zor

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

<h1>
RtAudio.js
<br>
</h1>

<h4>RtAudio binding for Node</h4>

<p>
<a href="https://www.npmjs.com/package/@hamitzor/rtaudio.js">
<img src="https://badge.fury.io/js/@hamitzor%2Frtaudio.js.svg"
alt="NPM">
</a>
</p>

Node binding of the <a href="https://github.com/thestk/rtaudio">RtAudio</a> C++ audio library. To see a detailed description of RtAudio visit it's <a href="https://github.com/thestk/rtaudio">GitHub page</a>.

An overview of this binding:

* Access well-known audio I/O APIs
- Windows: WASAPI and DirectSound
- Linux: ALSA and PulseAudio
* No additional library/software needed
- Direct access to the aforementioned APIs through C++
* Prebuilds are available, so don't worry about building it yourself
- A single `npm install` should get you ready to go
* Supports Node.js versions >= 18.19.0
* Probe available audio devices
* Stream audio to output devices
* Stream audio from input devices
* Fully configurable audio streaming, allows configuring
- sample rate
- bit depth
- frame size
- number of channels

## Installation

Install it using `npm` or `yarn`

```
npm install @hamitzor/rtaudio.js
```
or
```
yarn add @hamitzor/rtaudio.js
```

As simple as that, no additional library/software required for installation. If you run into trouble during installation, don't hesitate to create an issue at <a href="https://github.com/hamitzor/node-audio/issues">Github</a>.

> **Note**
> Only Windows and Linux are supported at the moment.
## Credits

This packages uses the C++ library named rtaudio under the hood. To check it out, visit https://github.com/thestk/rtaudio.

## License

<a href="https://raw.githubusercontent.com/hamitzor/node-audio/master/LICENSE">MIT</a>
203 changes: 203 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
/** The native API to use */
export declare enum LowLevelAudioApi {
/** Search for a working compiled API. */
UNSPECIFIED = 0,

/** Macintosh OS-X Core Audio API. */
MACOSX_CORE = 1,

/** The Advanced Linux Sound Architecture API. */
LINUX_ALSA = 2,

/** The Linux PulseAudio API. */
LINUX_PULSE = 4,

/** The Microsoft WASAPI API. */
WINDOWS_WASAPI = 7,

/** The Microsoft DirectSound API. */
WINDOWS_DS = 8,
}

/** The format of the PCM data. */
export declare enum PCMFormat {
/** 8-bit signed integer. */
RTAUDIO_SINT8 = 0x1,

/** 16-bit signed integer. */
RTAUDIO_SINT16 = 0x2,

/** 32-bit signed integer. */
RTAUDIO_SINT32 = 0x8,

/** Normalized between plus/minus 1.0. */
RTAUDIO_FLOAT32 = 0x10,

/** Normalized between plus/minus 1.0. */
RTAUDIO_FLOAT64 = 0x20,
}

/** Flags that change the default stream behavior */
export declare enum StreamFlag {
/** Use non-interleaved buffers (default = interleaved). */
RTAUDIO_NONINTERLEAVED = 0x1,

/** Attempt to set stream parameters for lowest possible latency. */
RTAUDIO_MINIMIZE_LATENCY = 0x2,

/** Attempt grab device and prevent use by others. */
RTAUDIO_HOG_DEVICE = 0x4,

/** Try to select realtime scheduling for callback thread. */
RTAUDIO_SCHEDULE_REALTIME = 0x8,

/** Use the "default" PCM device (ALSA only). */
RTAUDIO_ALSA_USE_DEFAULT = 0x10,

/** Do not automatically connect ports (JACK only). */
RTAUDIO_JACK_DONT_CONNECT = 0x20,
}

/** RtAudio error types */
export declare enum ErrorType {
/** A non-critical error. */
WARNING,

/** A non-critical error which might be useful for debugging. */
DEBUG_WARNING,

/** The default, unspecified error type. */
UNSPECIFIED,

/** No devices found on system. */
NO_DEVICES_FOUND,

/** An invalid device ID was specified. */
INVALID_DEVICE,

/** An error occurred during memory allocation. */
MEMORY_ERROR,

/** An invalid parameter was specified to a function. */
INVALID_PARAMETER,

/** The function was called incorrectly. */
INVALID_USE,

/** A system driver error occurred. */
DRIVER_ERROR,

/** A system error occurred. */
SYSTEM_ERROR,

/** A thread error occurred. */
THREAD_ERROR,
}

/** Stream status */
export declare enum StreamStatus {
/** Input overflow */
RTAUDIO_INPUT_OVERFLOW = 1,
/** Output underflow */
RTAUDIO_OUTPUT_UNDERFLOW = 2,
}

/** Stream options */
export declare type StreamOptions = {
/** A bit-mask of stream flags (RTAUDIO_MINIMIZE_LATENCY, RTAUDIO_HOG_DEVICE, RTAUDIO_ALSA_USE_DEFAULT). */
flags?: number

/** Number of stream buffers. */
numberOfBuffers?: number

/** A stream name (currently used only in Jack). */
streamName?: string

/** Scheduling priority of callback thread (only used with flag RTAUDIO_SCHEDULE_REALTIME). */
priority?: number
}

/** The public device information structure for returning queried values. */
export declare interface DeviceInfo {
/** Unique numeric device identifier. */
id: number;

/** Character string device identifier. */
name: string;

/** Maximum output channels supported by device. */
outputChannels: number;

/** Maximum input channels supported by device. */
inputChannels: number;

/** Maximum simultaneous input/output channels supported by device. */
duplexChannels: number;

/** Is the device the default output device */
isDefaultOutput: number;

/** Is the device the default input device */
isDefaultInput: number;

/** Supported sample rates (queried from list of standard rates). */
sampleRates: Array<number>;

/** Preferred sample rate, e.g. for WASAPI the system sample rate. */
preferredSampleRate: number;

/** Bit mask of supported data formats. */
nativeFormats: number;
}

/** The structure for specifying stream parameters. */
export declare interface StreamParameters {
/**
* Device id. Can be obtained using `defaultInputDevice`/`defaultOutputDevice` or using `devices`.
*/
deviceId: number;

/** Number of channels. */
nChannels: number;

/** First channel index on device (default = 0). */
firstChannel?: number;
}

export declare class NodeRtAudio {
constructor()
constructor(api: LowLevelAudioApi)

get devices(): DeviceInfo[]
get defaultInputDevice(): number
get defaultOutputDevice(): number
get isDestroyed(): boolean
get isRunning(): boolean
get isOpen(): boolean
get currentApi(): number
get latency(): number
get sampleRate(): number
get time(): number

setErrorCallback: (errorCallback: (errType: ErrorType, message: string) => void) => void
enableWarnings: () => void
disableWarnings: () => void
open: (
outputParameters: StreamParameters | null,
inputParameters: StreamParameters | null,
format: PCMFormat,
sampleRate: number,
bufferFrames: number,
options: StreamOptions | null,
callback: (output: Uint8Array, input: Uint8Array, nFrames: number, streamTime: number, status: StreamStatus) => void,
) => void
close: () => void
start: () => void
abort: () => void

static rtAudioVersion: string
static availableApiList: LowLevelAudioApi[]

static getApiDisplayName(api: LowLevelAudioApi): string
static getApiName(api: LowLevelAudioApi): string
}
Loading

0 comments on commit 3491545

Please sign in to comment.