Un4Seen Bass Audio Library Wrapper
Bass Audio library is the best audio library to play, edit, convert, stream etc. this wrapper wraps most of the "audio playback" features using ffi , ref , ref-struct .
ffi enables to call c library methods, properties , callbacks etc.
Compatible with?
tested on MacosX El capitan, windows 32 and 64 bits, ubuntu 14+, raspberrypi 2-3 (armv7), not tested on armv6 but its easy to test, just install and see.. related platform binaries can be downloaded from bass web page..
Warning
Please put the required bass audio library files to root
folder (dll
files for windows, so
files for *nix, dylib
files for macos)
Download the required dll files from http://www.un4seen.com. i did not include the required files in this module..
Read full documentation of bass from its original help. i Will only write same simple examples.
Features:
-
BASS_Init
-
BASS_GetVersion
-
BASS_StreamCreateFile
-
BASS_StreamCreateURL
-
BASS_StreamFree
-
BASS_ChannelPlay
-
BASS_ChannelStop
-
BASS_ChannelPause
-
BASS_ChannelGetPosition
-
BASS_ChannelSetPosition
-
BASS_ChannelGetLength
-
BASS_ChannelBytes2Seconds
-
BASS_ChannelSeconds2Bytes
-
BASS_ChannelGetLevel
-
BASS_ChannelRemoveSync
-
BASS_ChannelIsActive
-
BASS_ChannelSetAttribute
-
BASS_ChannelGetAttribute
-
BASS_ChannelSetSync
-
BASS_ChannelSlideAttribute
-
BASS_ChannelIsSliding
-
BASS_ChannelGetDevice
-
BASS_ChannelSetDevice
-
BASS_StreamFree
-
BASS_SetDevice
-
BASS_SetVolume
-
BASS_Start
-
BASS_Stop
-
BASS_Pause
-
BASS_GetInfo
-
BASS_ErrorGetCode
-
BASS_Free
-
BASS_GetCPU
-
BASS_GetDevice
-
BASS_GetDeviceInfo
-
BASS_ChannelGetTags
-
BASS_Mixer_StreamCreate
-
BASS_Mixer_StreamAddChannel
-
BASS_Mixer_ChannelGetLevel
-
BASS_Mixer_ChannelGetMixer
-
BASS_Mixer_ChannelGetPosition
-
BASS_Mixer_ChannelRemove
-
BASS_Mixer_ChannelRemoveSync
-
BASS_Mixer_ChannelSetPosition
-
BASS_Mixer_ChannelSetSync
-
BASS_Encode_Start
-
BASS_Encode_IsActive
-
BASS_Encode_SetNotify
-
BASS_Encode_SetPaused
-
BASS_Encode_Stop
-
BASS_Encode_CastInit
-
BASS_Encode_CastGetStats
-
BASS_Encode_CastSetTitle
-
BASS_Split_StreamCreate
-
BASS_Split_StreamGetAvailable
-
BASS_Split_StreamGetSource
-
BASS_Split_StreamGetSplits
-
BASS_Split_StreamReset
-
BASS_Split_StreamResetEx
Extra:
SYNCPROC also implemented
Installation
Install with npm :
npm install bassaudio
Examples
basic load and play file
var bass=;var basslib=; //get all sound cards var cards=basslib; console //lets print first sound card's info, find out more inside source code.. //first item in array '[0]' is "no sound" , then use the item [1] //you will see that card isInitialized will be false, because we did not init it yet.. var card=cards1; console // [device],[freq],[flags] , -1 is default sound cardvar result=basslibif!result console console // isMemoryFile,filename,offset,length,flags, returns pointer of filevar chan=basslibifbasslib!=basslibBASS_ErrorCodeBASS_OK console //lets play//channel,restart , returns (also there are stop , pause commands)var success=basslibif!success console
Get Duration
//get the duration, bass returns the total bytes of the channel pointer, then we must convert it to seconds :)var durationInBytes= basslibvar durationInSeconds=basslib
Get Duration Example2
//if stream is active (playing), then get position and duration..
Get Volume of channel
var ref=//set a float pointervar volume=ref;//get the volumevar result=basslib;//now deref volume to get the valueconsole
Set Volume
//lets set to 0.3basslib
Get current Position of playback
var positionInBytes= basslib;//now convert it to secondsvar position=basslib;
Set Position
//first get the byte position of desired seconds (ex:to last 10 secondsvar Last10SecondsBytePos=basslib;basslib;
Is channel is playing?
var result=basslibifresult==basslibBASS_ChannelIsActiveAttribsBASS_ACTIVE_PLAYING console
sliding
//Lets slide volume to 0 in 3 seconds (3000 milliseconds)basslib
callback
//lets make a callback when position reaches to 20. seconds.var Pos20SecondsBytePos=basslib;var proc20SecondsID=basslib //lets get the event when the position reaches to endvar procTOENDID=basslib
vumeter
//lets get vumeter :) var levels=basslib; //its a 64 bit value, lets get lo and hiwords var rightlevel=basslib0var leftlevel=basslib1
close the file
ifbasslib==basslibBASS_ChannelIsActiveAttribsBASS_ACTIVE_PLAYING //stop the channel basslib; var result=basslib if!result console
change sound card
//first check if this sound card is initialized var soundCardIndex=1; var info=basslib; if!infoenabled console if!infoIsInitialized var result= basslib; ifresult!=basslibBASS_ErrorCodeBASS_OK console var success=basslib if!success console
Info of a channel
var info= basslib;console console//other infos are: freq,chans,flags,ctype,origres,plugin,sample,filename
Info of a device
var info= basslib;consoleconsoleconsole
Free the memory from bass
basslib;
MIXER FEATURES
Enable Mixer
//before using mixer, first enable it. and put the required component to root folder.basslib;
Mixer is Enabled?
//before using mixer, first enable it. and put the required component to root folder.console;
Create mixer stream
basslib;var mixer=basslib;var chan1=basslibvar chan2=basslibvar ok1 = basslib;var ok2 = basslib;basslib
Get current Position of mixer playback
var positionInBytes= basslib;//now convert it to secondsvar position=basslib;
ENCODER FEATURES
you can directly encode and send output to shotcast and icecast servers
use mixer as a trick, because if the channel freed or added new channel, the encoder stops itself.
add channels to mixer every time, and add mixer channel to encoder. so the encoder never stops..
Init encoder
basslib;var mixer=basslib;var chan=basslibvar ok = basslib; basslib; //lets try icecastvar enc_chan=basslib;var result=basslib; basslib;
get notification from encoder server
var result=basslib
mono speaker output
//lets say if you have 5.1 speaker and want to use each output stereo or mono//basically with 5.1 output you can use 6 different output channels.//this example shows how to do it var bass= var basslib=; //set init to speakers var result=basslib if!result console //to use mixer feature, you have to enable it basslib; var cards=basslib; var speakerCount=basslibspeakers; console var path= var f1=path //create a mixer and tell it to output front right var mixer=basslib; console //set channel to decode var chan1=basslib //if your file is stereo , you have to downmix to mono, else you cannot get it mono output to only 1 speaker. var ok1 = basslib; var ok2=basslib console console
splitting channels
//if you want to add a mixer to another mixer, it is not possible/supported//but there is another way to do it.//you can split any channel or mixer into two, then you can use the new splitted channel on any other mixer.//A "splitter" basically does the opposite of a mixer: it splits a single source into multiple streams rather then mixing multiple sources into a single stream. Like mixer sources, splitter sources must be decoding channels. var splitChan=basslibvar splits=basslibvar splitsource=basslibvar avail1=basslibvar avail2=basslib
INFO i only added methods, properties what i needed.. add yours to the code or send me mail..
IMPORTANT
Below modifications are now handled internally. You shouldn't need to do these manually. However, if you do, you know where to find them.
2017-02-10
please modify node_modules/ffi/lib/callback.js if you are using callbacks. ffi garbage collector removes callbacks after 10 seconds. with this modification, callbacks stays on memory
at line 81 , before return, add;
Object;
--------------------------------
2017-01-31
please modify node_modules/ffi/lib/library.js if you are using linux os and if you are using addons. ffi loads bass into instance, not as global instance.. so the addons could not find main bass on memory. this fixes that issue.. (on windows and macos, it works without this modification)
find
var dl = libfile || null RTLD_NOW
change it to
var dl = libfile || null RTLD_NOW | DynamicLibraryFLAGSRTLD_GLOBAL
UPDATE LOG
--------------------------------
-
1.1.1
bugfix..
-
1.1.0
updates:
its been too much time passed since i created this library. i was using it when node was 8.x.x , so some libraries are outdated. updated to the latest ffi and ref libraries with newer napi implementation. as you know, ffi and ref are the gateway between c, c++ libraries and nodejs.. so what changed? just 3 libs and 3 lines.. yep.. now it supports newer nodejs versions..
var Struct = ;var ref = ;var ffi = ;thanks to RiccardoBiemmi the creator of bassaudiolibrary while i was not here, he updated the code.. but he just added his needs.. anyway, use his, or my work.. its up to you.. and add new features..
-
1.0.8
new features added:
-
BASS_Split_StreamCreate
-
BASS_Split_StreamGetAvailable
-
BASS_Split_StreamGetSource
-
BASS_Split_StreamGetSplits
-
BASS_Split_StreamReset
-
BASS_Split_StreamResetEx
-
-
1.0.7
- BASS_SetConfigflags, BASS_SetConfig, BASS_GetConfig, BASS_Update, BASS_ChannelUpdate features added
-
1.0.6
- correct an arg type in BASS_Encode_CastInit handler
-
1.0.5
- lock installed "ffi" version
- add shim to automate the changes to the "ffi" source code
-
1.0.4
- BASS_Encode_CastSetTitle has a minor bug. fixed.
-
1.0.3
- constructor now accepts and options object
- examples folder comes with link files
-
1.0.2
- getVolume() gives ref error when channel is 0. fixed.
-
1.0.1
- some types fixed for raspberrypi compability.
- examples folder added
- mono speaker out example added
-
1.0.0 Release
- i hope i fixed everything :) lets cross the fingers..
-
1.0.0-rc.22
- some types in BASS_StreamCreateFile are changed to ref.types.int64
-
1.0.0-rc.21
- these fixes are for linux environment
- fixed BASS_ChannelBytes2Seconds
- fixed BASS_ChannelSeconds2Bytes
- fixed BASS_ChannelSetPosition
-
1.0.0-rc.20
- added BASS_MIXERsourceflags
- found a bug on ffi source code, see above change for linux os..
-
1.0.0-rc.18
- BASS_ChannelGetAttribute returns float pointer. fixed.see getvolume example for usage.
- BASS_GETInfo now works.
-
1.0.0-rc.17
- BASS_Encode_IsActive fixed
-
1.0.0-rc.16
- getDevice(-1) must find the default sound card and return it.
- BASS_GetDevice takes 1 argument, but missing in code. fixed.
-
1.0.0-rc.15
- documentation fix
-
1.0.0-rc.14
- documentation fix
-
1.0.0-rc.13
-
new mixer features:
- BASS_Mixer_StreamCreate
- BASS_Mixer_StreamAddChannel
- BASS_Mixer_ChannelGetLevel
- BASS_Mixer_ChannelGetMixer
- BASS_Mixer_ChannelGetPosition
- BASS_Mixer_ChannelRemove
- BASS_Mixer_ChannelRemoveSync
- BASS_Mixer_ChannelSetPosition
- BASS_Mixer_ChannelSetSync
-
new encoder features:
- BASS_Encode_Start
- BASS_Encode_IsActive
- BASS_Encode_SetNotify
- BASS_Encode_SetPaused
- BASS_Encode_Stop
- BASS_Encode_CastInit
- BASS_Encode_CastGetStats
- BASS_Encode_CastSetTitle
-
-
1.0.0-rc.12
- BASS_ChannelBytes2Seconds and BASS_ChannelSeconds2Bytes returns wrong on arm cpu. pos value type changed to int64. now works correctly