Browse Source

set MIDI volume based on global music volume; set Native MIDI volume range as 0-127

M-HT 6 years ago
parent
commit
a7a3891e34
5 changed files with 42 additions and 2 deletions
  1. 3 0
      audio.c
  2. 2 2
      macos/native_midi_macosx.c
  3. 16 0
      midi.c
  4. 20 0
      midi.h
  5. 1 0
      native_midi/native_midi.h

+ 3 - 0
audio.c

@@ -212,6 +212,7 @@ AUDIO_OpenDevice(
    gAudioDevice.fSoundEnabled = TRUE;
    gAudioDevice.iMusicVolume = gConfig.iMusicVolume * SDL_MIX_MAXVOLUME / PAL_MAX_VOLUME;
    gAudioDevice.iSoundVolume = gConfig.iSoundVolume * SDL_MIX_MAXVOLUME / PAL_MAX_VOLUME;
+   MIDI_SetVolume(gConfig.iMusicVolume);
 
    //
    // Initialize the resampler module
@@ -425,6 +426,7 @@ AUDIO_IncreaseVolume(
    AUDIO_ChangeVolumeByValue(&gConfig.iSoundVolume, 3);
    gAudioDevice.iMusicVolume = gConfig.iMusicVolume * SDL_MIX_MAXVOLUME / PAL_MAX_VOLUME;
    gAudioDevice.iSoundVolume = gConfig.iSoundVolume * SDL_MIX_MAXVOLUME / PAL_MAX_VOLUME;
+   MIDI_SetVolume(gConfig.iMusicVolume);
 }
 
 VOID
@@ -450,6 +452,7 @@ AUDIO_DecreaseVolume(
    AUDIO_ChangeVolumeByValue(&gConfig.iSoundVolume, -3);
    gAudioDevice.iMusicVolume = gConfig.iMusicVolume * SDL_MIX_MAXVOLUME / PAL_MAX_VOLUME;
    gAudioDevice.iSoundVolume = gConfig.iSoundVolume * SDL_MIX_MAXVOLUME / PAL_MAX_VOLUME;
+   MIDI_SetVolume(gConfig.iMusicVolume);
 }
 
 VOID

+ 2 - 2
macos/native_midi_macosx.c

@@ -45,7 +45,7 @@ struct _NativeMidiSong
 };
 
 static NativeMidiSong *currentsong = NULL;
-static int latched_volume = 128;
+static int latched_volume = 127;
 
 static OSStatus
 GetSequenceLength(MusicSequence sequence, MusicTimeStamp *_sequenceLength)
@@ -302,7 +302,7 @@ void native_midi_setvolume(NativeMidiSong *song, int volume)
 
     latched_volume = volume;
     if ((song) && (song->audiounit)) {
-        const float floatvol = ((float) volume) / ((float) 128);
+        const float floatvol = ((float) volume) / ((float) 127);
         AudioUnitSetParameter(song->audiounit, kHALOutputParam_Volume,
                               kAudioUnitScope_Global, 0, floatvol, 0);
     }

+ 16 - 0
midi.c

@@ -24,6 +24,21 @@
 
 static int  g_iMidiCurrent = -1;
 static NativeMidiSong *g_pMidi = NULL;
+static int  g_iMidiVolume = PAL_MAX_VOLUME;
+
+void
+MIDI_SetVolume(
+	int       iVolume
+)
+{
+#if PAL_HAS_NATIVEMIDI
+	g_iMidiVolume = iVolume;
+	if (g_pMidi)
+	{
+		native_midi_setvolume(g_pMidi, iVolume * 127 / PAL_MAX_VOLUME);
+	}
+#endif
+}
 
 void
 MIDI_Play(
@@ -82,6 +97,7 @@ MIDI_Play(
 
 	if (g_pMidi)
 	{
+		MIDI_SetVolume(g_iMidiVolume);
 		native_midi_start(g_pMidi, fLoop);
 		g_iMidiCurrent = iNumRIX;
 	}

+ 20 - 0
midi.h

@@ -26,6 +26,26 @@
 #include "common.h"
 #include "native_midi/native_midi.h"
 
+/*++
+  Purpose:
+
+    Set volume for MIDI music.
+
+  Parameters:
+
+    [IN]  iVolume - volume in range 0-PAL_MAX_VOLUME.
+
+  Return value:
+
+    None.
+
+--*/
+PAL_C_LINKAGE
+void
+MIDI_SetVolume(
+	int       iVolume
+);
+
 /*++
   Purpose:
 

+ 1 - 0
native_midi/native_midi.h

@@ -156,6 +156,7 @@ int native_midi_active(NativeMidiSong *song);
   Parameters:
 
     [IN]  song - the NativeMidiSong object.
+    [IN]  volume - midi volume in range 0-127.
 
   Return value: