shithub: duke3d

Download patch

ref: 0517615df8ab283df2f09ab29f15827f26914b58
parent: 88f635f90b28ccfbcab0eaef1b2d29eb32b5baa8
author: fabien sanglard <fabiensanglard@fabiens-Mac-Pro-3.local>
date: Sat Dec 15 14:14:04 EST 2012

Added SDL Midi playback....thinking of dropping the Windows specific code completely

--- a/Engine/src/cache1d.h
+++ b/Engine/src/cache1d.h
@@ -46,6 +46,7 @@
 extern char  game_dir[512];
 extern int32_t TCkopen4load(const char  *filename, int readfromGRP);
 
+
 #endif  /* !defined _INCLUDE_CACHE1D_H_ */
 
 /* end of cache1d.h ... */
--- a/Game/src/audiolib/music.h
+++ b/Game/src/audiolib/music.h
@@ -31,6 +31,8 @@
 #ifndef __MUSIC_H
 #define __MUSIC_H
 
+#include "duke3d.h"
+
 #include "sndcards.h"
 
 #ifndef PLAT_DOS
@@ -79,7 +81,7 @@
 void  MUSIC_Continue( void );
 void  MUSIC_Pause( void );
 int   MUSIC_StopSong( void );
-int   MUSIC_PlaySong( unsigned char *song, int loopflag );
+int   MUSIC_PlaySong( char *song, int loopflag );
 void  MUSIC_SetContext( int context );
 int   MUSIC_GetContext( void );
 void  MUSIC_SetSongTick( uint32_t PositionInTicks );
--- a/Game/src/duke3d.h
+++ b/Game/src/duke3d.h
@@ -37,6 +37,11 @@
 #include <malloc.h>
 #endif
 
+#ifdef _WIN32
+#include "../../Engine/src/windows/inttypes.h"
+#else
+#include <inttypes.h>
+#endif
 
 #include <fcntl.h>
 #include <time.h>
--- a/Game/src/dummy_audiolib.c
+++ b/Game/src/dummy_audiolib.c
@@ -10,6 +10,9 @@
 #include <inttypes.h>
 #include <stdio.h>
 #include "engine_protos.h"
+#include "audiolib/music.h"
+
+//Dummy sound system for when a system has no sound system yet.
 /*
 uint8_t  *FX_ErrorString( int ErrorNumber ){
    static uint8_t  nope = '\0';
@@ -73,9 +76,9 @@
  */
 
 
-//Dummy music
-#include "audiolib/music.h"
+//Dummy music for when a system has no music system yet. 
 
+/*
 char  *MUSIC_ErrorString(int ErrorNumber)
 {
 	return "";
@@ -208,5 +211,5 @@
     
     kclose(fd);
 }
-
+*/
 
--- /dev/null
+++ b/Game/src/midi/sdl_midi.c
@@ -1,0 +1,188 @@
+//
+//  sdl_midi.c
+//  Duke3D
+//
+//  Created by fabien sanglard on 12-12-15.
+//  Copyright (c) 2012 fabien sanglard. All rights reserved.
+//
+
+#include <stdio.h>
+#include "../audiolib/music.h"
+#include "SDL.h"
+#include "SDL_Mixer.h"
+#include "cache1d.h"
+
+/*
+ Because the music is stored in a GRP file that is never fully loaded in RAM
+ (the full version of Duke Nukem 3D is a 43MB GRP) we need to extract the music
+ from it and store it in RAM.
+*/
+#define KILOBYTE (1024*1024)
+uint8_t musicDataBuffer[100 * KILOBYTE];
+
+char  *MUSIC_ErrorString(int ErrorNumber)
+{
+	return "";
+}
+
+int MUSIC_Init(int SoundCard, int Address)
+{
+	if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024)==-1) {
+	    printf("Mix_OpenAudio: %s\n", Mix_GetError());
+    }
+    
+    return MUSIC_Ok;
+}
+
+int MUSIC_Shutdown(void)
+{
+	MUSIC_StopSong();
+    return(MUSIC_Ok);
+}
+
+void MUSIC_SetMaxFMMidiChannel(int channel)
+{
+}
+
+void MUSIC_SetVolume(int volume)
+{
+    Mix_VolumeMusic((int)(volume / 2));
+}
+
+void MUSIC_SetMidiChannelVolume(int channel, int volume)
+{
+}
+
+void MUSIC_ResetMidiChannelVolumes(void)
+{
+}
+
+int MUSIC_GetVolume(void)
+{
+	return 0;
+}
+
+void MUSIC_SetLoopFlag(int loopflag)
+{
+}
+
+int MUSIC_SongPlaying(void)
+{
+	return Mix_PlayingMusic();
+}
+
+void MUSIC_Continue(void)
+{
+    if (Mix_PausedMusic())
+        Mix_ResumeMusic();
+}
+
+void MUSIC_Pause(void)
+{
+    Mix_PauseMusic();
+}
+
+int MUSIC_StopSong(void)
+{
+	if ( (Mix_PlayingMusic()) || (Mix_PausedMusic()) )
+        Mix_HaltMusic();
+    
+    return(MUSIC_Ok);
+}
+
+
+
+int MUSIC_PlaySong(char  *songFilename, int loopflag)
+{
+    int32_t fd =  0;
+    int fileSize;
+    SDL_RWops *rw;
+    Mix_Music* sdlMusic;
+    
+    fd = kopen4load(songFilename,0);
+    
+	if(fd == -1){ 
+        printf("The music '%s' cannot be found in the GRP or the filesystem.\n",songFilename);
+        return 0;
+    }
+    
+    
+    
+    fileSize = kfilelength( fd );
+    if(fileSize >= sizeof(musicDataBuffer))
+    {
+        printf("The music '%s' was found but is too big (%dKB)to fit in the buffer (%luKB).\n",songFilename,fileSize/1024,sizeof(musicDataBuffer)/1024);
+        kclose(fd);
+        return 0;
+    }
+    
+    kread( fd, musicDataBuffer, fileSize);
+    kclose( fd );
+    
+    //Ok, the file is in memory
+    rw = SDL_RWFromMem((void *) musicDataBuffer, fileSize); 
+    
+    sdlMusic = Mix_LoadMUS_RW(rw);
+    
+    Mix_PlayMusic(sdlMusic, (loopflag == MUSIC_PlayOnce) ? 0 : -1);
+    
+    return 1;
+}
+
+
+void MUSIC_SetContext(int context)
+{
+}
+
+int MUSIC_GetContext(void)
+{
+	return 0;
+}
+
+void MUSIC_SetSongTick(uint32_t PositionInTicks)
+{
+}
+
+void MUSIC_SetSongTime(uint32_t milliseconds)
+{
+}
+
+void MUSIC_SetSongPosition(int measure, int beat, int tick)
+{
+}
+
+void MUSIC_GetSongPosition(songposition *pos)
+{
+}
+
+void MUSIC_GetSongLength(songposition *pos)
+{
+}
+
+int MUSIC_FadeVolume(int tovolume, int milliseconds)
+{
+	return(MUSIC_Ok);
+}
+
+int MUSIC_FadeActive(void)
+{
+	return 0;
+}
+
+void MUSIC_StopFade(void)
+
+{
+}
+
+void MUSIC_RerouteMidiChannel(int channel, int cdecl function( int event, int c1, int c2 ))
+{
+}
+
+void MUSIC_RegisterTimbreBank(uint8_t  *timbres)
+{
+}
+
+// This is the method called from the Game Module.
+void PlayMusic(char  *fileName){
+    MUSIC_PlaySong(fileName,1);
+}
\ No newline at end of file
--- a/Game/src/midi/win_midiout.cpp
+++ b/Game/src/midi/win_midiout.cpp
@@ -1052,10 +1052,11 @@
     return(MUSIC_Ok);
 } // MUSIC_StopSong
 
-
-int MUSIC_PlaySong(uint8_t  *song, int loopflag)
+       
+int MUSIC_PlaySong(char  *songFilename, int loopflag)
 {
-    //SDL_RWops *rw;
+    int32_t GRPFileHandle;
+    SDL_RWops *rw;
 
     MUSIC_StopSong();
 
@@ -1069,12 +1070,7 @@
     // !!! FIXME:  offset from the end of the block), and since we don't have
     // !!! FIXME:  this information, we have to give it SOMETHING.
 
-    /* !!! ARGH! There's no LoadMUS_RW  ?!
-    rw = SDL_RWFromMem((void *) song, (10 * 1024) * 1024);  // yikes.
-    music_musicchunk = Mix_LoadMUS_RW(rw);
-    Mix_PlayMusic(music_musicchunk, (loopflag == MUSIC_PlayOnce) ? 0 : -1);
-    */
-
+   
 	if(midi_device)
 	midi_device->stop_stream(0);
 
@@ -1086,7 +1082,7 @@
 		if(midi_device)
 		midi_device->start_track(eventlist, loopflag?true:false);
 	}
-
+    
 //STUBBED("Need to use PlaySongROTT.  :(");
 
     return(MUSIC_Ok);
@@ -1118,11 +1114,10 @@
 			nMusicState = MUSIC_STATUS_PLAYING;
 		}
 	}
-return(MUSIC_Ok);
+    
+    return(MUSIC_Ok);
 
-//STUBBED("Need to use PlaySongROTT.  :(");
-
-} // MUSIC_PlaySong
+} 
 
 
 extern uint8_t  ApogeePath[256];
--- a/xcode/Duke3D.xcodeproj/project.pbxproj
+++ b/xcode/Duke3D.xcodeproj/project.pbxproj
@@ -7,6 +7,7 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
+		2D4FB6FF167D430F00915887 /* sdl_midi.c in Sources */ = {isa = PBXBuildFile; fileRef = 2D4FB6FE167D430F00915887 /* sdl_midi.c */; };
 		2D7B621D1678885A00E35E54 /* a.c in Sources */ = {isa = PBXBuildFile; fileRef = 2D7B61F11678885A00E35E54 /* a.c */; };
 		2D7B621E1678885A00E35E54 /* cache1d.c in Sources */ = {isa = PBXBuildFile; fileRef = 2D7B61F41678885A00E35E54 /* cache1d.c */; };
 		2D7B621F1678885A00E35E54 /* host.c in Sources */ = {isa = PBXBuildFile; fileRef = 2D7B61FA1678885A00E35E54 /* host.c */; };
@@ -36,7 +37,6 @@
 		2D7B627816788F9B00E35E54 /* keyboard.c in Sources */ = {isa = PBXBuildFile; fileRef = 2D7B625616788F9B00E35E54 /* keyboard.c */; };
 		2D7B627916788F9B00E35E54 /* menues.c in Sources */ = {isa = PBXBuildFile; fileRef = 2D7B625816788F9B00E35E54 /* menues.c */; };
 		2D7B627A16788F9B00E35E54 /* win_midiout.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2D7B625B16788F9B00E35E54 /* win_midiout.cpp */; };
-		2D7B627B16788F9B00E35E54 /* xmidi.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2D7B625D16788F9B00E35E54 /* xmidi.cpp */; };
 		2D7B627C16788F9B00E35E54 /* player.c in Sources */ = {isa = PBXBuildFile; fileRef = 2D7B626116788F9B00E35E54 /* player.c */; };
 		2D7B627D16788F9B00E35E54 /* premap.c in Sources */ = {isa = PBXBuildFile; fileRef = 2D7B626216788F9B00E35E54 /* premap.c */; };
 		2D7B627E16788F9B00E35E54 /* rts.c in Sources */ = {isa = PBXBuildFile; fileRef = 2D7B626316788F9B00E35E54 /* rts.c */; };
@@ -71,6 +71,7 @@
 /* End PBXCopyFilesBuildPhase section */
 
 /* Begin PBXFileReference section */
+		2D4FB6FE167D430F00915887 /* sdl_midi.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = sdl_midi.c; sourceTree = "<group>"; };
 		2D7B61DE167886FB00E35E54 /* Duke3D */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Duke3D; sourceTree = BUILT_PRODUCTS_DIR; };
 		2D7B61F11678885A00E35E54 /* a.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = a.c; path = ../../Engine/src/a.c; sourceTree = "<group>"; };
 		2D7B61F21678885A00E35E54 /* a.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = a.h; path = ../../Engine/src/a.h; sourceTree = "<group>"; };
@@ -143,11 +144,7 @@
 		2D7B625616788F9B00E35E54 /* keyboard.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = keyboard.c; path = ../../Game/src/keyboard.c; sourceTree = "<group>"; };
 		2D7B625716788F9B00E35E54 /* keyboard.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = keyboard.h; path = ../../Game/src/keyboard.h; sourceTree = "<group>"; };
 		2D7B625816788F9B00E35E54 /* menues.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = menues.c; path = ../../Game/src/menues.c; sourceTree = "<group>"; };
-		2D7B625A16788F9B00E35E54 /* databuf.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = databuf.h; sourceTree = "<group>"; };
 		2D7B625B16788F9B00E35E54 /* win_midiout.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = win_midiout.cpp; sourceTree = "<group>"; };
-		2D7B625C16788F9B00E35E54 /* win_midiout.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = win_midiout.h; sourceTree = "<group>"; };
-		2D7B625D16788F9B00E35E54 /* xmidi.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = xmidi.cpp; sourceTree = "<group>"; };
-		2D7B625E16788F9B00E35E54 /* xmidi.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = xmidi.h; sourceTree = "<group>"; };
 		2D7B625F16788F9B00E35E54 /* mouse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = mouse.h; path = ../../Game/src/mouse.h; sourceTree = "<group>"; };
 		2D7B626016788F9B00E35E54 /* names.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = names.h; path = ../../Game/src/names.h; sourceTree = "<group>"; };
 		2D7B626116788F9B00E35E54 /* player.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = player.c; path = ../../Game/src/player.c; sourceTree = "<group>"; };
@@ -350,11 +347,8 @@
 		2D7B625916788F9B00E35E54 /* midi */ = {
 			isa = PBXGroup;
 			children = (
-				2D7B625A16788F9B00E35E54 /* databuf.h */,
 				2D7B625B16788F9B00E35E54 /* win_midiout.cpp */,
-				2D7B625C16788F9B00E35E54 /* win_midiout.h */,
-				2D7B625D16788F9B00E35E54 /* xmidi.cpp */,
-				2D7B625E16788F9B00E35E54 /* xmidi.h */,
+				2D4FB6FE167D430F00915887 /* sdl_midi.c */,
 			);
 			name = midi;
 			path = ../../Game/src/midi;
@@ -455,7 +449,6 @@
 				2D7B627816788F9B00E35E54 /* keyboard.c in Sources */,
 				2D7B627916788F9B00E35E54 /* menues.c in Sources */,
 				2D7B627A16788F9B00E35E54 /* win_midiout.cpp in Sources */,
-				2D7B627B16788F9B00E35E54 /* xmidi.cpp in Sources */,
 				2D7B627C16788F9B00E35E54 /* player.c in Sources */,
 				2D7B627D16788F9B00E35E54 /* premap.c in Sources */,
 				2D7B627E16788F9B00E35E54 /* rts.c in Sources */,
@@ -474,6 +467,7 @@
 				2D7C17DD167AD6A500E1BBA1 /* nodpmi.c in Sources */,
 				2D7C17DE167AD6A500E1BBA1 /* user.c in Sources */,
 				2D7C17E0167ADE2000E1BBA1 /* pitch.c in Sources */,
+				2D4FB6FF167D430F00915887 /* sdl_midi.c in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};