Git Product home page Git Product logo

luajit-rtaudio's Introduction

LuaJIT-rtaudio

ffi binding to the C interface of RtAudio

The C interface function are accesed removing the rtaudio prefix.

To use rtaudio C interface:

Define your callback

--callback
local function AudioInit(udatacode)
    local ffi = require"ffi"
    local sin = math.sin
    ffi.cdef(udatacode)
    return function(out, inp, nFrames,stream_time,status,userdata)
        print(nFrames,out,stream_time,status)

        local buf = ffi.cast("float*",out)
        local udc = ffi.cast("MyUdata*",userdata)
        local lenf = nFrames*2
    
        for i=0,lenf-2,2 do
            local sample = sin(udc.Phase)--*32767--*0.01
            udc.Phase = udc.Phase + udc.dPhase
            buf[i] = sample
            buf[i+1] = sample
        end

        --if stream_time > 3 then return 1 end
        return 0
    end
end
-- userdata
local udatacode = [[typedef struct {double Phase;double dPhase;} MyUdata]]
ffi.cdef(udatacode)
local ud = ffi.new"MyUdata"

And use it from RtAudio C interface as:

local rt = require"rtaudio_ffi"
local dac = rt.create(0)
--options
local options = ffi.new("rtaudio_stream_options_t[1]")
options[0].flags = bit.bor(options[0].flags,rt.FLAGS_HOG_DEVICE,rt.FLAGS_SCHEDULE_REALTIME)
--output parameters
local outpar = ffi.new"rtaudio_stream_parameters_t[1]"
outpar[0].device_id = rt.get_default_output_device(dac)
outpar[0].num_channels = 2

local bufferFrames = ffi.new("unsigned int[1]",2048)
local callback = rt.MakeAudioCallback(AudioInit,udatacode) --defined elsewhere
--open it
local ret = rt.open_stream(dac,outpar,nil,rt.FORMAT_FLOAT32,44100,bufferFrames, callback,ud,options,errcb)
if(ret<0) then print(ret,"open_stream",ffi.string(rt.error(dac))) end
--start it
rtaudio.start_stream(dac)
...
rt.stop_stream(dac)
rt.close_stream(dac)
rt.destroy(dac)

rtAudioPlayer

Gives a simplified interface for playing libsndfile files.

local sndf = require"sndfile_ffi"
local rt = require"rtaudio_ffi"
local AudioPlayer = require"rtAudioPlayer"
local dac = rt.rtaudio(0)

--copy specs from file
local info = sndf.get_info(filename)
local audioplayer,err = AudioPlayer({
    dac = dac,
    device = dac:get_default_output_device(),
    freq = info.samplerate, 
    format = rt.FORMAT_SINT32,
    channels = info.channels, 
    samples = 1024})

assert(audioplayer)
--insert several files
for i=1,10 do
    --filename, level, timeoffset
    audioplayer:insert(filename,(11-i)*0.1,i*0.6)
end
--show them
for node in audioplayer:nodes() do
    print("node",node.sf)
end
print"return to start"
io.read"*l"
--play them 7 secs
audioplayer:start()
print("start error",ffi.string(rt.error(dac)))
sleep(7); -- this can be found in http://luajit.org/ext_ffi_tutorial.html
--close
audioplayer:close()

luajit-rtaudio's People

Contributors

sonoro1234 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.