Skip to content

Commit

Permalink
Merge pull request #2 from razvangeangu/master
Browse files Browse the repository at this point in the history
feat(#1): expose presetIndex
  • Loading branch information
Mike Perri authored Nov 20, 2020
2 parents 1bc0f5c + 93fb2f9 commit 414abef
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 19 deletions.
10 changes: 7 additions & 3 deletions android/src/main/cpp/Engine/SoundFontInstrument.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@

class SoundFontInstrument : public IInstrument {
public:
SoundFontInstrument(int32_t sampleRate, bool isStereo, const char* path, bool isAsset) {
int presetIndex;

SoundFontInstrument(int32_t sampleRate, bool isStereo, const char* path, bool isAsset, int32_t presetIndex) {
presetIndex = presetIndex;

if (isAsset) {
auto asset = openAssetBuffer(path);
auto assetBuffer = AAsset_getBuffer(asset);
Expand All @@ -36,10 +40,10 @@ class SoundFontInstrument : public IInstrument {
void handleMidiEvent(uint8_t status, uint8_t data1, uint8_t data2) override {
if (status == 0x90) {
// Note On
tsf_note_on(mTsf, 0, data1, data2 / 255.0);
tsf_note_on(mTsf, presetIndex, data1, data2 / 255.0);
} else if (status == 0x80) {
// Note Off
tsf_note_off(mTsf, 0, data1);
tsf_note_off(mTsf, presetIndex, data1);
}
}

Expand Down
4 changes: 2 additions & 2 deletions android/src/main/cpp/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ extern "C" {
}

__attribute__((visibility("default"))) __attribute__((used))
void add_track_sf2(const char* filename, bool isAsset, Dart_Port callbackPort) {
void add_track_sf2(const char* filename, bool isAsset, int32_t presetIndex, Dart_Port callbackPort) {
check_engine();

std::thread([=]() {
auto sampleRate = engine->getSampleRate();
auto channelCount = engine->getChannelCount();
auto isStereo = channelCount > 1;

auto sf2Instrument = new SoundFontInstrument(sampleRate, isStereo, filename, isAsset);
auto sf2Instrument = new SoundFontInstrument(sampleRate, isStereo, filename, isAsset, presetIndex);
auto trackIndex = engine->mSchedulerMixer.addTrack(sf2Instrument);

callbackToDartInt32(callbackPort, trackIndex);
Expand Down
7 changes: 5 additions & 2 deletions ios/Classes/AppleSamplerUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ func isAppleSampler(component: AVAudioUnitComponent) -> Bool {
return isApple && isMIDISynth
}

func loadSoundFont(avAudioUnit: AVAudioUnit, soundFontURL: URL) {
func loadSoundFont(avAudioUnit: AVAudioUnit, soundFontURL: URL, presetIndex: Int32) {
let audioUnit = avAudioUnit.audioUnit
var mutableSoundFontURL = soundFontURL

Expand All @@ -34,7 +34,7 @@ func loadSoundFont(avAudioUnit: AVAudioUnit, soundFontURL: URL) {
// Send program change command for patch 0 to preload
let channel = UInt32(0)
let pcCommand = UInt32(0xC0 | channel)
let patch1 = UInt32(0)
let patch1 = UInt32(presetIndex)
result = MusicDeviceMIDIEvent(audioUnit, pcCommand, patch1, 0, 0)
assert(result == noErr, "Patch could not be preloaded")

Expand All @@ -48,4 +48,7 @@ func loadSoundFont(avAudioUnit: AVAudioUnit, soundFontURL: URL) {
UInt32(MemoryLayout.size(ofValue: enabled)))

assert(result == noErr, "Preload could not be disabled")

result = MusicDeviceMIDIEvent(audioUnit, pcCommand, patch1, 0, 0)
assert(result == noErr, "Patch could not be changed")
}
4 changes: 2 additions & 2 deletions ios/Classes/CocoaEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public class CocoaEngine {
}
}

func addTrackSf2(sf2Path: String, isAsset: Bool, completion: @escaping (track_index_t) -> Void) {
func addTrackSf2(sf2Path: String, isAsset: Bool, presetIndex: Int32, completion: @escaping (track_index_t) -> Void) {
let trackIndex = SchedulerAddTrack(self.scheduler)

AudioUnitUtils.loadAudioUnits { avAudioUnitComponents in
Expand All @@ -119,7 +119,7 @@ public class CocoaEngine {

if let url = self.getUrlForPath(sf2Path, isAsset: isAsset) {

loadSoundFont(avAudioUnit: avAudioUnit, soundFontURL: url)
loadSoundFont(avAudioUnit: avAudioUnit, soundFontURL: url, presetIndex: presetIndex)

self.setTrackAudioUnit(trackIndex: trackIndex, avAudioUnit: avAudioUnit)

Expand Down
4 changes: 2 additions & 2 deletions ios/Classes/SwiftFlutterSequencerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ func buildKeyMap(trackIndex: track_index_t, resultCallbackPort: Dart_Port) {
}

@_cdecl("add_track_sf2")
func addTrackSf2(path: UnsafePointer<CChar>, isAsset: Bool, callbackPort: Dart_Port) {
plugin.engine!.addTrackSf2(sf2Path: String(cString: path), isAsset: isAsset) { trackIndex in
func addTrackSf2(path: UnsafePointer<CChar>, isAsset: Bool, presetIndex: Int32, callbackPort: Dart_Port) {
plugin.engine!.addTrackSf2(sf2Path: String(cString: path), isAsset: isAsset, presetIndex: presetIndex) { trackIndex in
callbackToDartInt32(callbackPort, trackIndex)
}
}
Expand Down
3 changes: 3 additions & 0 deletions lib/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ const TOP_OFF_PERIOD_MS = 1000;
/// "Lead frames" account for the fact that it may take some time to build the
/// events and sync them with the native sequencer engine.
const LEAD_FRAMES = 1024;

/// The patch number to select from a sf2 file.
const DEFAULT_PATCH_NUMBER = 0;
8 changes: 5 additions & 3 deletions lib/instrument.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import 'constants.dart';
import 'models/sample_descriptor.dart';

/// The base class for Instruments.
abstract class Instrument {
final String idOrPath;
final bool isAsset;
final int presetIndex;

Instrument(this.idOrPath, this.isAsset);
Instrument(this.idOrPath, this.isAsset, { this.presetIndex = DEFAULT_PATCH_NUMBER });

String get displayName {
return idOrPath.split(RegExp('[\\\\/]')).last;
Expand All @@ -30,8 +32,8 @@ class SfzInstrument extends Instrument {
/// Describes an instrument in SF2 format. Will be played by the SoundFont
/// player for the current platform.
class Sf2Instrument extends Instrument {
Sf2Instrument({ String path, bool isAsset })
: super(path, isAsset);
Sf2Instrument({ String path, bool isAsset, int presetIndex = DEFAULT_PATCH_NUMBER })
: super(path, isAsset, presetIndex: presetIndex);
}

/// Describes an AudioUnit instrument (Apple platforms only.)
Expand Down
8 changes: 4 additions & 4 deletions lib/native_bridge.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ final nBuildKeyMap = nativeLib.lookupFunction<
void Function(int, int)>('build_key_map');

final nAddTrackSf2 = nativeLib.lookupFunction<
Void Function(Pointer<Utf8>, Int8, Int64),
void Function(Pointer<Utf8>, int, int)>('add_track_sf2');
Void Function(Pointer<Utf8>, Int8, Int32, Int64),
void Function(Pointer<Utf8>, int, int, int)>('add_track_sf2');

final nRemoveTrack = nativeLib.lookupFunction<
Void Function(Int32),
Expand Down Expand Up @@ -156,9 +156,9 @@ class NativeBridge {
nBuildKeyMap(trackIndex, port.nativePort));
}

static Future<int> addTrackSf2(String filename, bool isAsset) {
static Future<int> addTrackSf2(String filename, bool isAsset, int patchNumber) {
final filenameUtf8Ptr = Utf8.toUtf8(filename);
return singleResponseFuture<int>((port) => nAddTrackSf2(filenameUtf8Ptr, isAsset ? 1 : 0, port.nativePort));
return singleResponseFuture<int>((port) => nAddTrackSf2(filenameUtf8Ptr, isAsset ? 1 : 0, patchNumber, port.nativePort));
}

static Future<int> addTrackAudioUnit(String id) async {
Expand Down
2 changes: 1 addition & 1 deletion lib/sequence.dart
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ class Sequence {
int id;

if (instrument is Sf2Instrument) {
id = await NativeBridge.addTrackSf2(instrument.idOrPath, instrument.isAsset);
id = await NativeBridge.addTrackSf2(instrument.idOrPath, instrument.isAsset, instrument.presetIndex);
} else if (instrument is SfzInstrument) {
final parseResult = await parseSfz(instrument.idOrPath, instrument.isAsset);
final samplerInstrument =
Expand Down

0 comments on commit 414abef

Please sign in to comment.