sndfile

libsndfile audio file I/O library bindings for Kit

Files

FileDescription
.editorconfigEditor formatting configuration
.gitignoreGit ignore rules for build artifacts and dependencies
.tool-versionsasdf tool versions (Zig, Kit)
LICENSEMIT license file
README.mdThis file
c/kit_sndfile.cC FFI wrapper
c/kit_sndfile.hC header for FFI wrapper
examples/basic.kitBasic usage example
kit.tomlPackage manifest with metadata, native library requirements, and tasks
src/sndfile.kitKit SndFile module
tests/sndfile.test.kitTests for sndfile

Dependencies

No Kit package dependencies.

This package links against the native libsndfile library through the C FFI wrapper in c/.

Installation

kit add gitlab.com/kit-lang/packages/kit-sndfile.git

System Requirements

Install libsndfile before building or running code that imports Kit.SndFile:

PlatformCommand
macOSbrew install libsndfile
Ubuntusudo apt install libsndfile1-dev
Fedorasudo dnf install libsndfile-devel

Usage

import Kit.SndFile as Sndfile

main = fn =>
  println ("libsndfile version: " ++ Sndfile.version)

  # Read audio file metadata
  match Sndfile.open-read "audio.wav"
    | Err e -> println ("Open error: " ++ e)
    | Ok handle ->
      println ("Format: " ++ (Sndfile.format-name handle))
      println ("Sample rate: " ++ (show (Sndfile.get-samplerate handle)) ++ " Hz")
      println ("Channels: " ++ (show (Sndfile.get-channels handle)))
      println ("Frames: " ++ (show (Sndfile.get-frames handle)))
      println ("Duration: " ++ (show (Sndfile.get-duration handle)) ++ " seconds")

      match Sndfile.close handle
        | Ok _ -> println "File closed"
        | Err e -> println ("Close error: " ++ e)

  # Create a WAV file handle
  wav-format = Sndfile.format-wav + Sndfile.format-pcm16
  match Sndfile.open-write "output.wav" wav-format 1 44100
    | Err e -> println ("Create error: " ++ e)
    | Ok handle ->
      println "Created WAV (1 channel, 44100 Hz, PCM 16-bit)"

      match Sndfile.close handle
        | Ok _ -> println "File closed"
        | Err e -> println ("Close error: " ++ e)

main

When writing files, combine one container format with one encoding format. The public constants currently cover format-wav, format-flac, format-ogg, format-aiff, format-pcm16, format-pcm24, format-float, and format-vorbis.

Development

Native Wrapper

The Kit module in src/sndfile.kit exposes result-returning helpers over the C wrapper declared in c/kit_sndfile.h and implemented in c/kit_sndfile.c. Native build settings live in the [native] section of kit.toml.

Running Examples

Run examples with the interpreter:

kit run examples/basic.kit

Compile examples to a native binary:

kit build examples/basic.kit && ./basic

The basic example prints format constants, tries to read /tmp/kit-sndfile-test.wav, writes /tmp/kit-sndfile-output.wav, and verifies the created file.

Running Tests

Run the test suite:

kit test

Run the test suite with coverage:

kit test --coverage

The tests link against libsndfile and currently validate version access, format constants, and exported function availability.

Running kit dev

Run the standard development workflow (format, check, test):

kit dev

This will:

  1. Format and check source files in src/
  2. Run tests in tests/ with coverage

Generating Documentation

Generate API documentation from doc comments:

kit doc

Note: Kit sources with doc comments (##) will generate HTML documents in docs/*.html

Cleaning Build Artifacts

Remove generated files, caches, and build artifacts:

kit task clean

Note: Defined in kit.toml.

Local Installation

To install this package locally for development:

kit install

This installs the package to ~/.kit/packages/@kit/sndfile/, making it available for import as Kit.SndFile in other projects.

License

This package is released under the MIT License - see LICENSE for details.

libsndfile is released under the LGPL 2.1 / LGPL 3.0 License.

Exported Functions & Types

format-wav

WAV file format constant.

Int

format-flac

FLAC file format constant.

Int

format-ogg

OGG container format constant.

Int

format-aiff

AIFF file format constant.

Int

format-pcm16

16-bit PCM encoding constant.

Int

format-pcm24

24-bit PCM encoding constant.

Int

format-float

32-bit float encoding constant.

Int

format-vorbis

Vorbis encoding constant (for use with OGG container).

Int

open-read

Open an audio file for reading.

Parameters:

Returns:

NonEmptyString -> Result Ptr String

open-write

Open an audio file for writing.

Parameters:

Returns:

NonEmptyString -> Int -> PositiveInt -> PositiveInt -> Result Ptr String

close

Close an audio file and free its resources.

Parameters:

Returns:

Ptr -> Result Unit String

get-frames

Get the total number of audio frames in the file.

Parameters:

Returns:

Ptr -> Int

get-samplerate

Get the sample rate of the audio file in Hz.

Parameters:

Returns:

Ptr -> Int

get-channels

Get the number of audio channels.

Parameters:

Returns:

Ptr -> Int

get-format

Get the raw format code of the audio file.

Parameters:

Returns:

Ptr -> Int

get-duration

Get the duration of the audio file in seconds.

Parameters:

Returns:

Ptr -> Float

format-name

Get the human-readable format name (e.g., "WAV", "FLAC").

Parameters:

Returns:

Ptr -> String

version

Get the libsndfile version string.

Returns:

String