diff --git a/Makefile b/Makefile index 56f1a360..b79efafe 100644 --- a/Makefile +++ b/Makefile @@ -482,10 +482,17 @@ endif ifeq ($(BACKEND),sdl) SOURCES += simsys_s.cc ifeq ($(OSTYPE),mac) - # Core Audio (Quicktime) base sound system routines - SOURCES += sound/core-audio_sound.mm - SOURCES += music/core-audio_midi.mm - LIBS += -framework Foundation -framework QTKit + ifeq ($(AV_FOUNDATION),1) + # Core Audio (AVFoundation) base sound system routines + SOURCES += sound/AVF_core-audio_sound.mm + SOURCES += music/AVF_core-audio_midi.mm + LIBS += -framework Foundation -framework AVFoundation + else + # Core Audio (Quicktime) base sound system routines + SOURCES += sound/core-audio_sound.mm + SOURCES += music/core-audio_midi.mm + LIBS += -framework Foundation -framework QTKit + endif else SOURCES += sound/sdl_sound.cc ifeq ($(findstring $(OSTYPE), cygwin mingw),) @@ -521,10 +528,17 @@ endif ifeq ($(BACKEND),sdl2) SOURCES += simsys_s2.cc ifeq ($(OSTYPE),mac) - # Core Audio (Quicktime) base sound system routines - SOURCES += sound/core-audio_sound.mm - SOURCES += music/core-audio_midi.mm - LIBS += -framework Foundation -framework QTKit + ifeq ($(AV_FOUNDATION),1) + # Core Audio (AVFoundation) base sound system routines + SOURCES += sound/AVF_core-audio_sound.mm + SOURCES += music/AVF_core-audio_midi.mm + LIBS += -framework Foundation -framework AVFoundation + else + # Core Audio (Quicktime) base sound system routines + SOURCES += sound/core-audio_sound.mm + SOURCES += music/core-audio_midi.mm + LIBS += -framework Foundation -framework QTKit + endif else SOURCES += sound/sdl_sound.cc ifeq ($(findstring $(OSTYPE), cygwin mingw),) @@ -575,10 +589,17 @@ endif ifeq ($(BACKEND),opengl) SOURCES += simsys_opengl.cc ifeq ($(OSTYPE),mac) - # Core Audio (Quicktime) base sound system routines - SOURCES += sound/core-audio_sound.mm - SOURCES += music/core-audio_midi.mm - LIBS += -framework Foundation -framework QTKit + ifeq ($(AV_FOUNDATION),1) + # Core Audio (AVFoundation) base sound system routines + SOURCES += sound/AVF_core-audio_sound.mm + SOURCES += music/AVF_core-audio_midi.mm + LIBS += -framework Foundation -framework AVFoundation + else + # Core Audio (Quicktime) base sound system routines + SOURCES += sound/core-audio_sound.mm + SOURCES += music/core-audio_midi.mm + LIBS += -framework Foundation -framework QTKit + endif else SOURCES += sound/sdl_sound.cc ifeq ($(findstring $(OSTYPE), cygwin mingw),) diff --git a/config.template b/config.template index 7351da74..b49ce660 100644 --- a/config.template +++ b/config.template @@ -30,6 +30,7 @@ #PROFILE = 2 # Enable profiling with optimisation flags, can be used with `OPTIMISE = 1' #STATIC = 1 # Enable static linkage, currently mingw only +#AV_FOUNDATION = 1 # Use AVFoundation instead of QTKit. If you are using macOS 10.12 or later, this must be enabled. #WITH_REVISION = 1 # adds the revision from svn; required for networkgames # if you do not use SVN, add -DREVISION="1234" to the FLAGS below diff --git a/music/AVF_core-audio_midi.mm b/music/AVF_core-audio_midi.mm new file mode 100644 index 00000000..70dad138 --- /dev/null +++ b/music/AVF_core-audio_midi.mm @@ -0,0 +1,77 @@ +/* + * Apple OSX Core Audio MIDI routine added by Leopard + * + * This file is part of the Simutrans project under the artistic licence. + * + */ + +#import "music.h" + +#import +#import +#import + + +static int nowPlaying = -1; // the number of the track currently being played + +static NSMutableArray* players; + + +void dr_set_midi_volume(int const vol) +{ + // Not supportd by AVMIDIPlayer +} + + +int dr_load_midi(char const* const filename) +{ + NSURL* const url = [NSURL fileURLWithPath: [NSString stringWithUTF8String: filename]]; + AVMIDIPlayer* const player = [[AVMIDIPlayer alloc] initWithContentsOfURL:url soundBankURL: nil error: nil]; + if (player) { + [player prepareToPlay]; + [players addObject: player]; + } + return [players count] - 1; +} + + +void dr_play_midi(int const key) +{ + // Play the file referenced by the supplied key. + AVMIDIPlayer* const player = [players objectAtIndex: key]; + [player play: ^{}]; + nowPlaying = key; +} + + +void dr_stop_midi() +{ + // We assume the 'nowPlaying' key holds the most recently started track. + AVMIDIPlayer* const player = [players objectAtIndex: nowPlaying]; + [player stop]; +} + + +sint32 dr_midi_pos() +{ + if (nowPlaying == -1) { + return -1; + } + float const rate = [[players objectAtIndex: nowPlaying] rate]; + return rate > 0 ? 0 : -1; +} + + +void dr_destroy_midi() +{ + if (nowPlaying != -1) { + dr_stop_midi(); + } + } + + +bool dr_init_midi() +{ + players = [NSMutableArray arrayWithCapacity: MAX_MIDI]; + return true; +} diff --git a/sound/AVF_core-audio_sound.mm b/sound/AVF_core-audio_sound.mm new file mode 100644 index 00000000..c494c1e2 --- /dev/null +++ b/sound/AVF_core-audio_sound.mm @@ -0,0 +1,55 @@ +/* + * Apple OSX Core Audio MIDI routine added by Leopard + * + * This file is part of the Simutrans project under the artistic licence. + * + */ + +#include "sound.h" + +#import +#import +#import +#import + + +static NSMutableArray* players_WAV; + + +bool dr_init_sound() +{ + printf("Sound system Initialise\n"); + printf("Wave File database\n"); + players_WAV = [NSMutableArray arrayWithCapacity: 128]; + printf("Sound system Initialisation complete\n"); + return true; +} + + +int dr_load_sample(char const* const filename) +{ + NSURL* const url = [NSURL fileURLWithPath: [NSString stringWithUTF8String: filename]]; + AVAudioPlayer* const player = [[AVAudioPlayer alloc] initWithContentsOfURL: url error: nil]; + if (!player) { + printf("** Warning, unable to open wav file %s\n", filename); + return -1; + } + + // Preload the file into memory. + [player setVolume: 0]; + [player play]; + + [players_WAV addObject: player]; + + int const i = [players_WAV count] - 1; + printf("Load WAV (%d): %s\n", i, filename); + return i; +} + + +void dr_play_sample(int const key, int const volume) +{ + AVAudioPlayer* const m = [players_WAV objectAtIndex: key]; + [m setVolume: volume / 255.f]; + [m play]; +}