atsushieno / managed-midi

[Past project] Cross-platform MIDI processing library for mono and .NET (ALSA, CoreMIDI, Android, WinMM and UWP).
MIT License
195 stars 26 forks source link

Unable to load shared library 'asound' or one of its dependencies #35

Closed Connor14 closed 5 years ago

Connor14 commented 5 years ago

I am working on an app written in .NET Core using this library and it works great on Windows. I tried it on my Ubuntu 18.04 installation and I got this exception:

Unhandled Exception: System.TypeInitializationException: The type initializer for 'Commons.Music.Midi.MidiAccessManager' threw an exception. ---> System.DllNotFoundException: Unable to load shared library 'asound' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libasound: cannot open shared object file: No such file or directory
   at AlsaSharp.Natives.snd_seq_open(IntPtr handle, IntPtr name, Int32 streams, Int32 mode)
   at AlsaSharp.AlsaSequencer..ctor(AlsaIOType ioType, AlsaIOMode ioMode, String driverName) in D:\a\1\s\external\alsa-sharp\alsa-sharp\AlsaSharp\AlsaSequencer.cs:line 19
   at Commons.Music.Midi.Alsa.AlsaMidiAccess..ctor() in D:\a\1\s\Commons.Music.Midi.DesktopShared\alsaseq\AlsaMidiAccess.cs:line 16
   at Commons.Music.Midi.MidiAccessManager.InitializeDefault() in D:\a\1\s\Commons.Music.Midi.DesktopShared\MidiAccessManager.cs:line 10
   at Commons.Music.Midi.MidiAccessManager..cctor() in D:\a\1\s\Commons.Music.Midi.Shared\MidiAccessManager.cs:line 15
   --- End of inner exception stack trace ---
   at Commons.Music.Midi.MidiAccessManager.get_Default() in D:\a\1\s\Commons.Music.Midi.Shared\MidiAccessManager.cs:line 23
   at DemoPlayer.Program.Main(String[] args) in /home/connor/Documents/AbundantMusic.NET/DemoPlayer/Program.cs:line 25
Aborted (core dumped)

I tried installing libasound2-dev and that resolved the exception, but no sound was played. I am running the my compiled program using dotnet DemoPlayer.dll from my terminal.

Here is the code used to play the midi:

// Play the MIDI using managed-midi
// https://github.com/atsushieno/managed-midi
var access = MidiAccessManager.Default;
var music = MidiMusic.Read(new MemoryStream(midiFile));
using (var player = new MidiPlayer(music, access))
{
    long totalPlayTime = player.GetTotalPlayTimeMilliseconds();
    Console.WriteLine("Play time: " + TimeSpan.FromMilliseconds(totalPlayTime).ToString("g"));

    player.Play();
    while (player.State == PlayerState.Playing)
    {

    }
}

The Console.WriteLine was shown and my computer was definitely doing something because htop showed 2 threads running at 100% here and there. After the midi finished playing, the program ended.

I just don't hear any sound. It's probably a missing package on my end but I'm not sure which one.

atsushieno commented 5 years ago

Do you open any actual MIDI output device/software? ALSA default MIDI port is "MIDI through" which has no sound output.

Connor14 commented 5 years ago

I don't have any MIDI specific output devices or software connected, which is definitely the problem.

I thought the player would interpret the MIDI and play it to speakers (like any other audio file) or a MIDI output device depending on the configuration. It played directly to speakers in Windows so I assumed it would work the same way in Linux. But that's probably because Windows has some additional functionality built in. I guess I just misunderstood what the MidiPlayer was doing.

Ultimately what I was looking for is a way to take in a MIDI file and spit out audio. The best way to do that might be by "rendering" it to a WAV or MP3 and then playing that back separately.

atsushieno commented 5 years ago

Exactly. Windows has built-in "Microsoft GS Wavetable Synth". On Linux there is timidity or fluidsynth (which also needs some attention on how to use it; it needs soundfont file as the command line argument) that work like MS GS Synth. On Mac I have no idea. Fluidsynth should build on Mac too, but I don't see any package.

For audio rendering fluidsynth is an easy option on Linux. It has file rendering "audio driver". (disclaimer: I am a fluidsynth contributor so it's from very personal point of view.)

Connor14 commented 5 years ago

I think I'll use FluidSynth. It works great so far!