GithubHelp home page GithubHelp logo

moonsndfile's Introduction

MoonSndFile: Lua bindings for libsndfile

MoonSndFile is a Lua binding library for Erik de Castro Lopo's libsndfile.

It runs on GNU/Linux and on Windows (MSYS2/MinGW) and requires Lua (>=5.3) and libsndfile (>= 1.0.25).

Author: Stefano Trettel

Lua logo

License

MIT/X11 license (same as Lua). See LICENSE.

Documentation

See the Reference Manual.

Getting and installing

Setup the build environment as described here, then:

$ git clone https://github.com/stetre/moonsndfile
$ cd moonsndfile
moonsndfile$ make
moonsndfile$ sudo make install

Note: that MoonSndFile does not link directly to the libsndfile library (libsndfile.so on Linux), but it builds dynamically its own internal dispatch tables instead. As a consequence, libsndfile.so is not needed at compile time but it is required at runtime, so you have to make sure that it is reachable by the linker. You can do this by installing it in the standard search directories (e.g. /usr/lib/), or by properly setting the LD_LIBRARY_PATH environment variable in the shell where you execute the Lua scripts.

Example

The example below is the Lua version of the sfprocess.c example that comes with libsndfile-1.0.28.

Other examples can be found in the examples/ directory contained in the release package.

-- MoonSndFile example: hello.lua
sf = require('moonsndfile')

NFRAMES = 64 -- no. of frames requested at each read
CHANNEL_GAIN = { 0.5, 0.8, 0.1, 0.4, 0.4, 0.9 }
MAX_CHANNELS = #CHANNEL_GAIN

function process_data(data, channels)
   -- Unpack the data binary string to a table of doubles:
   local data = sf.unpack('double', data)

   -- Process the data (apply a channel dependent gain):
   for chan = 1, channels do
      for k = chan, #data, channels do
         data[k] = data[k] * CHANNEL_GAIN[chan]
      end
   end
   
   -- Pack the table of doubles to a binary string, and return it:
   return sf.pack('double', data)
end

-- Open the input file:
infile, fileinfo = sf.open("input.wav", 'r')

if fileinfo.channels > MAX_CHANNELS then
   error("Not able to process more than "..MAX_CHANNELS.. "channels")
end

-- Open the output file, with the same properties as the input file:
outfile, fileinfo = sf.open("output.wav", 'w', fileinfo)

-- Read input frames / process them / write output frames:
while true do 
   local data, nframes = sf.read(infile, 'double', NFRAMES)
   if not data then break end
   data = process_data(data, fileinfo.channels)
   sf.write(outfile, 'double', data)
end

-- Close files:
sf.close(infile)
sf.close(outfile)

The script can be executed at the shell prompt with the standard Lua interpreter:

$ lua hello.lua

See also

moonsndfile's People

Contributors

stetre avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

moonsndfile's Issues

Error when running examples/list_formats.lua

┌[ ~/github.com/stetre/moonsndfile ]─[ ⑂master* ]
└─> lua5.3 examples/list_formats.lua
lua5.3: examples/list_formats.lua:14: unexpected error (enums.c, 201)
stack traceback:
    [C]: in function 'moonsndfile.get_format_list'                                                   
        examples/list_formats.lua:14: in main chunk         
   [C]: in ?```

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.