Giter VIP home page Giter VIP logo

sf2cute's Introduction

SF2cute: C++ Library for SoundFont 2

Travis Build Status AppVeyor Build Status

SF2cute is a C++ library for writing SoundFont 2.

Downloading

You can download a source code package of the latest release from Releases page.

Visual Studio users can install the precompiled binaries from the SF2cute NuGet Gallery.

SF2cute Documentation is available on the GitHub Pages. You can also build the documentation by yourself using Doxygen.

Compiling

First of all, install the following prerequisite softwares.

The following command will generate a Makefile for your system. You can use cmake-gui if you want to use a GUI and configure the options.

cmake .

Then compile the library using the generated project file.

make

SoundFont file writing example

Here is an example of SoundFont file writing.

#include <algorithm>
#include <memory>
#include <vector>
#include <iostream>
#include <fstream>
#include <stdexcept>

#include <sf2cute.hpp>

using namespace sf2cute;

/// Makes a pulse wave of n/8 duty cycle.
/// @param n the duty cycle n/8.
/// @return the pulse wave sample datapoints.
std::vector<int16_t> MakePulseVector(int n) {
  std::vector<int16_t> data(200);
  size_t width = data.size() * n / 8;
  std::fill(data.begin(), std::next(data.begin(), width), 0x4000);
  std::fill(std::next(data.begin(), width), data.end(), 0);
  return data;
}

/// Writes SoundFont 2 file using SF2cute.
/// @param argc Number of arguments. Not used.
/// @param argv Argument vector. Not used.
/// @return 0 if the SoundFont file is successfully written.
int main(int argc, char * argv[]) {
  SoundFont sf2;

  // Set metadata.
  sf2.set_sound_engine("EMU8000");
  sf2.set_bank_name("Chipsound");
  sf2.set_rom_name("ROM");

  // Construct sample datapoints.
  std::vector<int16_t> data_50 = MakePulseVector(4);

  // Add a sample.
  std::shared_ptr<SFSample> sample_50 = sf2.NewSample(
    "Square",                 // name
    data_50,                  // sample data
    0,                        // start loop
    uint32_t(data_50.size()), // end loop
    44100,                    // sample rate
    57,                       // root key
    0);                       // microtuning

  // Make an instrument zone.
  SFInstrumentZone instrument_zone(sample_50,
    std::vector<SFGeneratorItem>{
      //SFGeneratorItem(SFGenerator::kKeyRange, RangesType(0, 127)),
      //SFGeneratorItem(SFGenerator::kVelRange, RangesType(0, 127)),
      SFGeneratorItem(SFGenerator::kSampleModes, uint16_t(SampleMode::kLoopContinuously)),
    },
    std::vector<SFModulatorItem>{});
  // Add more generators or modulators if necessary.
  instrument_zone.SetGenerator(SFGeneratorItem(SFGenerator::kReverbEffectsSend, 618));
  instrument_zone.SetModulator(SFModulatorItem(
      SFModulator(SFGeneralController::kNoteOnVelocity,
      SFControllerDirection::kDecrease, SFControllerPolarity::kUnipolar,
      SFControllerType::kConcave),
    SFGenerator::kInitialAttenuation,
    960,
    SFModulator(0),
    SFTransform::kLinear));

  // Add an instrument.
  std::shared_ptr<SFInstrument> instrument_50 = sf2.NewInstrument(
    sample_50->name(),
    std::vector<SFInstrumentZone>{
      std::move(instrument_zone)
    });

  // Add a preset.
  std::shared_ptr<SFPreset> preset_50 = sf2.NewPreset(
    instrument_50->name(), 0, 0,
    std::vector<SFPresetZone>{
      SFPresetZone(instrument_50)
    });

  // Write SoundFont file.
  try {
    std::ofstream ofs("output.sf2", std::ios::binary);
    sf2.Write(ofs);
    return 0;
  }
  catch (const std::fstream::failure & e) {
    // File output error.
    std::cerr << e.what() << std::endl;
    return 1;
  }
  catch (const std::exception & e) {
    // Other errors.
    // For example: Too many samples.
    std::cerr << e.what() << std::endl;
    return 1;
  }
}

sf2cute's People

Contributors

frabert avatar gocha avatar wcgbg avatar

Stargazers

 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.