-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
19ee0f4
commit 5439bc0
Showing
7 changed files
with
112 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
FinModelUtility/Fin/Fin/src/audio/io/importers/midi/MidiAudioFileBundle.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using fin.io; | ||
|
||
namespace fin.audio.io.importers.midi; | ||
|
||
public class MidiAudioFileBundle( | ||
IReadOnlyTreeFile midiFile, | ||
IReadOnlyTreeFile soundFontFile) : IAudioFileBundle { | ||
public string? GameName { get; init; } | ||
public IReadOnlyTreeFile MainFile => this.MidiFile; | ||
|
||
public IReadOnlyTreeFile MidiFile { get; } = midiFile; | ||
public IReadOnlyTreeFile SoundFontFile { get; } = soundFontFile; | ||
} |
70 changes: 70 additions & 0 deletions
70
FinModelUtility/Fin/Fin/src/audio/io/importers/midi/MidiAudioImporter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
|
||
using fin.util.sets; | ||
|
||
using SimpleSynth.Parameters; | ||
using SimpleSynth.Parsing; | ||
using SimpleSynth.Providers; | ||
using SimpleSynth.Synths; | ||
|
||
namespace fin.audio.io.importers.midi; | ||
|
||
public class MidiAudioImporter : IAudioImporter<MidiAudioFileBundle> { | ||
public ILoadedAudioBuffer<short>[] ImportAudio( | ||
IAudioManager<short> audioManager, | ||
MidiAudioFileBundle audioFileBundle) { | ||
var midiFile = audioFileBundle.MidiFile; | ||
using var ms = midiFile.OpenRead(); | ||
var midi = new MidiInterpretation(ms, new DefaultNoteSegmentProvider()); | ||
|
||
// Create a new synthesizer with default providers. | ||
var synth = new BasicSynth(midi, new DefaultAdsrEnvelopeProvider(AdsrParameters.Short), new DefaultBalanceProvider()); | ||
var signal = synth.GetSignal(); | ||
|
||
var mutableBuffer = audioManager.CreateLoadedAudioBuffer( | ||
audioFileBundle, | ||
midiFile.AsFileSet()); | ||
|
||
mutableBuffer.Frequency = signal.SamplingRate; | ||
|
||
{ | ||
var samples = signal.Samples; | ||
var sampleCount = samples.Length; | ||
|
||
var channelCount = 1; | ||
var floatCount = channelCount * sampleCount; | ||
var floatPcm = new float[floatCount]; | ||
|
||
var channels = new short[channelCount][]; | ||
for (var c = 0; c < channelCount; ++c) { | ||
channels[c] = new short[sampleCount]; | ||
} | ||
|
||
for (var i = 0; i < sampleCount; ++i) { | ||
for (var c = 0; c < channelCount; ++c) { | ||
var floatSample = floatPcm[channelCount * i + c]; | ||
|
||
var floatMin = -1f; | ||
var floatMax = 1f; | ||
|
||
var normalizedFloatSample = | ||
(MathF.Max(floatMin, Math.Min(floatSample, floatMax)) - | ||
floatMin) / (floatMax - floatMin); | ||
|
||
float shortMin = short.MinValue; | ||
float shortMax = short.MaxValue; | ||
|
||
var shortSample = (short) (shortMin + | ||
normalizedFloatSample * | ||
(shortMax - shortMin)); | ||
|
||
channels[c][i] = shortSample; | ||
} | ||
} | ||
|
||
mutableBuffer.SetPcm(channels); | ||
} | ||
|
||
return [mutableBuffer]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.