Skip to content

Commit

Permalink
Mac OS X: Use the Cocoa QTKit API
Browse files Browse the repository at this point in the history
The old Carbon QuickTime API is becoming obsolete.
  • Loading branch information
skyjake committed Jul 29, 2011
1 parent d15668b commit a8d2204
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 123 deletions.
3 changes: 3 additions & 0 deletions doomsday/CMakeLists.txt
Expand Up @@ -156,6 +156,9 @@ IF (APPLE)
SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mmacosx-version-min=10.6" )
ENDIF()

# Link against QTKit.
SET (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework QTKit")

SET (BUILDSYSTEM MACOSX)

## Add needed Compiler and Linker flags for OSX
Expand Down
1 change: 1 addition & 0 deletions doomsday/engine/api/sys_audiod_mus.h
Expand Up @@ -39,6 +39,7 @@ enum {
// Generic driver interface. All other interfaces are based on this.
typedef struct audiointerface_music_generic_s {
int (*Init) (void);
void (*Shutdown) (void);
void (*Update) (void);
void (*Set) (int prop, float value);
int (*Get) (int prop, void *value);
Expand Down
42 changes: 42 additions & 0 deletions doomsday/engine/mac/include/MusicPlayer.h
@@ -0,0 +1,42 @@
/**\file
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2011 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/

#import <Cocoa/Cocoa.h>
#import <QTKit/QTKit.h>

@interface MusicPlayer : NSObject
{
QTMovie* currentSong;
float songVolume;
}

- (id)init;
- (void)shutdown;
- (int)playFile:(const char*)filename looping:(int)loop;
- (void)setVolume:(float)volume;
- (void)play;
- (void)stop;
- (void)rewind;

@end

216 changes: 216 additions & 0 deletions doomsday/engine/mac/src/MusicPlayer.m
@@ -0,0 +1,216 @@
/**\file
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2005-2011 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/

#import "MusicPlayer.h"

/**
* QuickTime music player for Mac OS X 10.4 or newer. Uses the Cocoa QTKit.
*/

static MusicPlayer* musicPlayer;

@implementation MusicPlayer

- (id)init
{
currentSong = 0;
songVolume = 1.f;
return self;
}

- (void)shutdown
{
if(currentSong)
{
[currentSong stop];
[currentSong release];
currentSong = 0;
}
}

- (int)playFile:(const char*)filename looping:(int)loop
{
// Get rid of the old song?
if(currentSong)
{
[currentSong stop];
[currentSong release];
currentSong = 0;
}

NSString* path = [[[NSString alloc] initWithCString:filename encoding:[NSString defaultCStringEncoding]] autorelease];
currentSong = [[QTMovie alloc] initWithFile:path error:0];
if(!currentSong) return false;

[currentSong setVolume:songVolume];

// Set the looping attribute.
[currentSong setAttribute:[NSNumber numberWithBool:(loop? YES : NO)] forKey:QTMovieLoopsAttribute];

// Start playing.
[currentSong play];
}

- (void)setVolume:(float)volume
{
songVolume = volume;
if(currentSong)
{
[currentSong setVolume:songVolume];
}
}

- (void)play
{
if(currentSong)
{
[currentSong play];
}
}

- (void)stop
{
if(currentSong)
{
[currentSong stop];
}
}

- (void)rewind
{
if(currentSong)
{
[currentSong gotoBeginning];
}
}

@end

// --------------------------------------------------------------------------

#include "sys_audiod.h"
#include "sys_audiod_mus.h"

static int DM_Ext_Init(void)
{
// Already initialized?
if(musicPlayer) return true;

// Create a new QTKit player.
musicPlayer = [[MusicPlayer alloc] init];

return true;
}

static void DM_Ext_Shutdown(void)
{
if(!musicPlayer) return;

// Release the player.
[musicPlayer shutdown];
[musicPlayer release];
musicPlayer = 0;
}

static void DM_Ext_Update(void)
{
if(!musicPlayer) return;

// Gets called on every frame.
}

static void DM_Ext_Set(int property, float value)
{
if(!musicPlayer)
return;

switch (property)
{
case MUSIP_VOLUME:
if(value > 1) value = 1;
if(value < 0) value = 0;
[musicPlayer setVolume:value];
break;
}
}

static int DM_Ext_Get(int property, void *value)
{
if(!musicPlayer)
return false;

switch (property)
{
case MUSIP_ID:
strcpy(value, "QuickTime(Cocoa)/Ext");
break;

default:
return false;
}
return true;
}

static void DM_Ext_Pause(int pause)
{
if(!musicPlayer) return;

if(pause)
{
[musicPlayer stop];
}
else
{
[musicPlayer play];
}
}

static void DM_Ext_Stop(void)
{
if(!musicPlayer) return;

[musicPlayer stop];
[musicPlayer rewind];
}

static int DM_Ext_PlayFile(const char *filename, int looped)
{
if(!musicPlayer) return 0;

return [musicPlayer playFile:filename looping:looped];
}

// The audio driver struct.
audiointerface_music_t audiodQuickTimeMusic = { {
DM_Ext_Init,
DM_Ext_Shutdown,
DM_Ext_Update,
DM_Ext_Set,
DM_Ext_Get,
DM_Ext_Pause,
DM_Ext_Stop },
NULL,
NULL,
DM_Ext_PlayFile,
};

0 comments on commit a8d2204

Please sign in to comment.