SF2cute  0.1
Modern C++ Library for SoundFont 2
SF2cute: C++ Library for SoundFont 2

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.

1 cmake .

Then compile the library using the generated project file.

1 make

SoundFont file writing example

Here is an example of SoundFont file writing.

1 #include <algorithm>
2 #include <memory>
3 #include <vector>
4 #include <iostream>
5 #include <fstream>
6 #include <stdexcept>
7 
8 #include <sf2cute.hpp>
9 
10 using namespace sf2cute;
11 
12 /// Makes a pulse wave of n/8 duty cycle.
13 /// @param n the duty cycle n/8.
14 /// @return the pulse wave sample datapoints.
15 std::vector<int16_t> MakePulseVector(int n) {
16  std::vector<int16_t> data(200);
17  size_t width = data.size() * n / 8;
18  std::fill(data.begin(), std::next(data.begin(), width), 0x4000);
19  std::fill(std::next(data.begin(), width), data.end(), 0);
20  return data;
21 }
22 
23 /// Writes SoundFont 2 file using SF2cute.
24 /// @param argc Number of arguments. Not used.
25 /// @param argv Argument vector. Not used.
26 /// @return 0 if the SoundFont file is successfully written.
27 int main(int argc, char * argv[]) {
28  SoundFont sf2;
29 
30  // Set metadata.
31  sf2.set_sound_engine("EMU8000");
32  sf2.set_bank_name("Chipsound");
33  sf2.set_rom_name("ROM");
34 
35  // Construct sample datapoints.
36  std::vector<int16_t> data_50 = MakePulseVector(4);
37 
38  // Add a sample.
39  std::shared_ptr<SFSample> sample_50 = sf2.NewSample(
40  "Square", // name
41  data_50, // sample data
42  0, // start loop
43  uint32_t(data_50.size()), // end loop
44  44100, // sample rate
45  57, // root key
46  0); // microtuning
47 
48  // Make an instrument zone.
49  SFInstrumentZone instrument_zone(sample_50,
50  std::vector<SFGeneratorItem>{
51  //SFGeneratorItem(SFGenerator::kKeyRange, RangesType(0, 127)),
52  //SFGeneratorItem(SFGenerator::kVelRange, RangesType(0, 127)),
53  SFGeneratorItem(SFGenerator::kSampleModes, uint16_t(SampleMode::kLoopContinuously)),
54  },
55  std::vector<SFModulatorItem>{});
56  // Add more generators (or modulators) if necessary.
57  instrument_zone.SetGenerator(SFGeneratorItem(SFGenerator::kReverbEffectsSend, 618));
58 
59  // Add an instrument.
60  std::shared_ptr<SFInstrument> instrument_50 = sf2.NewInstrument(
61  sample_50->name(),
62  std::vector<SFInstrumentZone>{
63  std::move(instrument_zone)
64  });
65 
66  // Add a preset.
67  std::shared_ptr<SFPreset> preset_50 = sf2.NewPreset(
68  instrument_50->name(), 0, 0,
69  std::vector<SFPresetZone>{
70  SFPresetZone(instrument_50)
71  });
72 
73  // Write SoundFont file.
74  try {
75  std::ofstream ofs("output.sf2", std::ios::binary);
76  sf2.Write(ofs);
77  return 0;
78  }
79  catch (const std::fstream::failure & e) {
80  // File output error.
81  std::cerr << e.what() << std::endl;
82  return 1;
83  }
84  catch (const std::exception & e) {
85  // Other errors.
86  // For example: Too many samples.
87  std::cerr << e.what() << std::endl;
88  return 1;
89  }
90 }