Forum rules - please read before posting.

Integration with Midi Tool Kit/external music playing scripts

edited September 2018 in Technical Q&A
I'm reposting this as the old topic was lost. Some music playing functions were added to the latest AC to allow tighter integration with external music playing assets, in this case it's Midi Tool Kit I'm trying to integrate: https://assetstore.unity.com/packages/tools/audio/midi-tool-kit-pro-115331 and the free version: https://assetstore.unity.com/packages/tools/audio/midi-tool-kit-free-107994

There is a list of API functions for this particular tool here: http://paxstellar.fr/2018/06/05/use-midi-player-tool-kit-in-your-script/

I'm not 100% certain how to send messages from an AC actionlist to this to change songs, play songs on scene load (in the scene load actionscript), etc.

I understand this isn't totally an AC issue, but this particular tool could be invaluable to people looking to use tiny midis for mobile adventure games to keep the file size down, as well as people looking to make retro adventure games with classic music. If it's possible to Send messages to MTK from AC, it would be useful for theme changes, and with midi it would be less noticeable that an audio music file being interrupted midway.

Again, sorry this is slightly specific to this tool (hopefully it'll be the same as sending messages to any external music player script), but I'm not sure how to send messages to other assets with AC using their API. If I can get this working, I'll be able to add a wiki tutorial for midi music in AC games.

Comments

  • Indeed - this should require use of the OnPlayMusic / OnStopMusic custom events.

    I'll take a look if I have time, but the upcoming AC release has to take priority.
  • You kindly posted a script back in August which hooked into music events: http://pasteall.org/1079279/csharp

    Were any new features added to AC in the last major update that would allow closer integration with external music systems such as MidiToolKit?
  • The events should allow for it, that was the point.  You'll need to write a custom script to make use of them, though.
  • That's fine, I was just wondering if there were any new features you'd added to the version since that would further support this or if anything necessary was already in place. So, would it be better to just write a script for the midi asset and call a send a custom message from the Scene Start actionlist rather than hooking in to the music event? 
  • edited November 2018
    It's all about preference, but hooking into the music events allow you to make use of AC's existing music Actions and saving/loading system.
  • Just to clarify, this would require the use of a dummy mp3 with the relevant name and length to play music via AC, and then hook into the MTK script to play a midi instead?
  • Yes - AC will use that to keep track of the state of the track when saving and loading.
  • edited December 2018

    I'm working on script and it's going well! I just need to clean it up and I'll be ready to add to the wiki for seamless MTK integration. Is there a way of passing the track name instead of index through via the hook? At the moment, I'm using midiFilePlayer.MPTK_MidiIndex = trackID; which is great if both Midi TookKit and AC's list of music is in the exact same order, but if not, it would require manual reassembly. If i could pass the track name from AC, I could use the same clip name and midi file, and just use midiFilePlayer.MPTK_MidiName = trackName; . The other option would be allowing the reordering of music in AC, but that's necessarily ideal.

  • You can extract the track name with a given track ID with this function:

    private string GetTrackName (int trackID)
    {
        foreach (MusicStorage musicStorage in KickStarter.settingsManager.musicStorages)
        {
            if (musicStorage.ID == trackID)
            {
                return musicStorage.audioClip.name;
            }
        }
    }
    
  • edited December 2018

    Thanks, that's great! I used this as I had an error with GetTrackName otherwise, maybe it could be done better than I have though:
    private string GetTrackName(int trackID) { string returnTrack = ""; foreach (MusicStorage musicStorage in KickStarter.settingsManager.musicStorages) { if (musicStorage.ID == trackID) { returnTrack = musicStorage.audioClip.name; } } return returnTrack; }

    Question about fadeTime, does this just pass the length of the fade in/fade out time for the track when the hook is accessed? In which case, I'm trying to think of how this could be mirrored in MidiToolKit, as I would imagine midiToolKit (or any other music tool accessing AC's music variables) would also need the volume information from the game, in which case that would need to be updated all the time. If it's passing a linear volume change length with fadeOut, it would need to also pass fadeType to any external music script. Just want to confirm this is how all these are used!

  • The fadeTime is the duration that the fade would occur normally, yes. How it's used is down to your script.

  • I think the best way for this would be to pass a relative volume change during the fade. AC isn't capable of crossfading music tracks is it, unless I'm mistake? If it is, it could be trickier to match playing two files simutaneous during a fade, but if it's serial (fade out one song, fade in second) either a volume change could be passed via script all the time, or simply during fade to save on performance. Does AC call anything to determine if it's fading that could be passed on?

  • AC isn't capable of crossfading music tracks is it, unless I'm mistake?

    It is, but it's your choice.

    Crossfading is handled via an interim AudioSource component that's a child of the MusicEngine prefab. This is controlled by the attached MusicCrossfade component.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.