Coder Social home page Coder Social logo

ziggysynth's Introduction

ZiggySynth

ZiggySynth is a SoundFont MIDI synthesizer written in pure Zig, ported from MeltySynth for C#.

Features

  • Suitable for both real-time and offline synthesis.
  • Support for standard MIDI files.
  • No dependencies other than the standard library.
  • All the functionality is in a single file.

Installation

Demo

https://www.youtube.com/watch?v=GQj3tF8OdPw

Youtube video

Examples

Some useful aliases:

const ziggysynth = @import("ziggysynth.zig");
const SoundFont = ziggysynth.SoundFont;
const Synthesizer = ziggysynth.Synthesizer;
const SynthesizerSettings = ziggysynth.SynthesizerSettings;
const MidiFile = ziggysynth.MidiFile;
const MidiFileSequencer = ziggysynth.MidiFileSequencer;

An example code to synthesize a simple chord:

// Load the SoundFont.
var sf2 = try fs.cwd().openFile("TimGM6mb.sf2", .{});
defer sf2.close();
var sound_font = try SoundFont.init(allocator, sf2.reader());
defer sound_font.deinit();

// Create the synthesizer.
var settings = SynthesizerSettings.init(44100);
var synthesizer = try Synthesizer.init(allocator, &sound_font, &settings);
defer synthesizer.deinit();

// Play some notes (middle C, E, G).
synthesizer.noteOn(0, 60, 100);
synthesizer.noteOn(0, 64, 100);
synthesizer.noteOn(0, 67, 100);

// The output buffer (3 seconds).
const sample_count: usize = @intCast(3 * settings.sample_rate);
var left: []f32 = try allocator.alloc(f32, sample_count);
defer allocator.free(left);
var right: []f32 = try allocator.alloc(f32, sample_count);
defer allocator.free(right);

// Render the waveform.
synthesizer.render(left, right);

Another example code to synthesize a MIDI file:

// Load the SoundFont.
var sf2 = try fs.cwd().openFile("TimGM6mb.sf2", .{});
defer sf2.close();
var sound_font = try SoundFont.init(allocator, sf2.reader());
defer sound_font.deinit();

// Create the synthesizer.
var settings = SynthesizerSettings.init(44100);
var synthesizer = try Synthesizer.init(allocator, &sound_font, &settings);
defer synthesizer.deinit();

// Load the MIDI file.
var mid = try fs.cwd().openFile("flourish.mid", .{});
defer mid.close();
var midi_file = try MidiFile.init(allocator, mid.reader());
defer midi_file.deinit();

// Create the sequencer.
var sequencer = MidiFileSequencer.init(&synthesizer);

// Play the MIDI file.
sequencer.play(&midi_file, false);

// The output buffer.
const sample_count = @as(f64, @floatFromInt(settings.sample_rate)) * midi_file.getLength();
var left: []f32 = try allocator.alloc(f32, @intFromFloat(sample_count));
defer allocator.free(left);
var right: []f32 = try allocator.alloc(f32, @intFromFloat(sample_count));
defer allocator.free(right);

// Render the waveform.
sequencer.render(left, right);

Todo

  • Wave synthesis
    • SoundFont reader
    • Waveform generator
    • Envelope generator
    • Low-pass filter
    • Vibrato LFO
    • Modulation LFO
  • MIDI message processing
    • Note on/off
    • Bank selection
    • Modulation
    • Volume control
    • Pan
    • Expression
    • Hold pedal
    • Program change
    • Pitch bend
    • Tuning
  • Effects
    • Reverb
    • Chorus
  • Other things
    • Standard MIDI file support
    • Performace optimization

License

ZiggySynth is available under the MIT license.

ziggysynth's People

Contributors

sinshu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

jack-ji

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.