| 1 |
/** |
| 2 |
* \file snd-cocoa.m |
| 3 |
* \brief Sound handling for the Cocoa frontend on macOS |
| 4 |
* |
| 5 |
* Copyright (c) 2022 Eric Branlund |
| 6 |
* |
| 7 |
* This work is free software; you can redistribute it and/or modify it |
| 8 |
* under the terms of either: |
| 9 |
* |
| 10 |
* a) the GNU General Public License as published by the Free Software |
| 11 |
* Foundation, version 2, or |
| 12 |
* |
| 13 |
* b) the "Angband licence": |
| 14 |
* This software may be copied and distributed for educational, research, |
| 15 |
* and not for profit purposes provided that this copyright and statement |
| 16 |
* are included in all such copies. Other copyrights may also apply. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "angband.h" |
| 20 |
#if defined(MACH_O_CARBON) && defined(SOUND) && !defined(SOUND_SDL) && !defined(SOUND_SDL2) |
| 21 |
#include "sound.h" |
| 22 |
#include "snd-cocoa.h" |
| 23 |
#import <Cocoa/Cocoa.h> |
| 24 |
|
| 25 |
|
| 26 |
/* |
| 27 |
* Values for type field in sound_file_type and sample_type field in |
| 28 |
* cocoa_sample |
| 29 |
*/ |
| 30 |
enum { |
| 31 |
COCOA_SOUND_NULL = 0, /* placeholder expected by core sound */ |
| 32 |
COCOA_SOUND_BRIDGED_NSSOUND /* use NSSound to handle load and play */ |
| 33 |
}; |
| 34 |
|
| 35 |
/* Holds the platform-specific data for a sound */ |
| 36 |
struct cocoa_sample { |
| 37 |
union { |
| 38 |
void *bridged_nssound; /* for COCOA_SOUND_BRIDGED_NSSOUND */ |
| 39 |
} sample_data; |
| 40 |
int sample_type; |
| 41 |
}; |
| 42 |
|
| 43 |
/* |
| 44 |
* Supported file types (according to |
| 45 |
* https://wiki.lazarus.freepascal.org/macOS_NSSound there are more but mp3 |
| 46 |
* and wav are likely the most useful for Angband) |
| 47 |
*/ |
| 48 |
static const struct sound_file_type supported_sound_formats[] = { |
| 49 |
{ ".mp3", COCOA_SOUND_BRIDGED_NSSOUND }, |
| 50 |
{ ".wav", COCOA_SOUND_BRIDGED_NSSOUND }, |
| 51 |
{ "", COCOA_SOUND_NULL } |
| 52 |
}; |
| 53 |
|
| 54 |
/** |
| 55 |
* Load a sound. |
| 56 |
*/ |
| 57 |
static bool load_sound_cocoa(const char *filename, int file_type, struct sound_data *data) |
| 58 |
{ |
| 59 |
struct cocoa_sample *sample = (struct cocoa_sample*) (data->plat_data); |
| 60 |
|
| 61 |
if (!sample) { |
| 62 |
sample = mem_zalloc(sizeof(*sample)); |
| 63 |
} |
| 64 |
|
| 65 |
switch (file_type) { |
| 66 |
case COCOA_SOUND_BRIDGED_NSSOUND: |
| 67 |
{ |
| 68 |
NSData *fileData = [NSData dataWithContentsOfFile:[NSString stringWithUTF8String:filename]]; |
| 69 |
NSSound *sound = |
| 70 |
[[NSSound alloc] initWithData:fileData]; |
| 71 |
|
| 72 |
if (sound) { |
| 73 |
sample->sample_data.bridged_nssound = |
| 74 |
(void*) CFBridgingRetain(sound); |
| 75 |
sample->sample_type = |
| 76 |
COCOA_SOUND_BRIDGED_NSSOUND; |
| 77 |
} else { |
| 78 |
/* |
| 79 |
* Failed to initialize. Mark it as unplayable. |
| 80 |
*/ |
| 81 |
sample->sample_type = COCOA_SOUND_NULL; |
| 82 |
} |
| 83 |
} |
| 84 |
break; |
| 85 |
|
| 86 |
default: |
| 87 |
/* |
| 88 |
* Either COCOA_SOUND_NULL or something unrecognized. In |
| 89 |
* either case, flag this as unplayable. |
| 90 |
*/ |
| 91 |
sample->sample_type = COCOA_SOUND_NULL; |
| 92 |
break; |
| 93 |
} |
| 94 |
|
| 95 |
data->plat_data = (void*)sample; |
| 96 |
if (sample->sample_type != COCOA_SOUND_NULL) { |
| 97 |
data->status = SOUND_ST_LOADED; |
| 98 |
return true; |
| 99 |
} |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Play a sound. |
| 105 |
*/ |
| 106 |
static bool play_sound_cocoa(struct sound_data *data) |
| 107 |
{ |
| 108 |
struct cocoa_sample *sample = (struct cocoa_sample*) (data->plat_data); |
| 109 |
|
| 110 |
if (sample) { |
| 111 |
switch (sample->sample_type) { |
| 112 |
case COCOA_SOUND_BRIDGED_NSSOUND: |
| 113 |
{ |
| 114 |
NSSound *sound = (__bridge NSSound*) (sample->sample_data.bridged_nssound); |
| 115 |
|
| 116 |
if (sound) { |
| 117 |
if ([sound isPlaying]) { |
| 118 |
[sound stop]; |
| 119 |
} |
| 120 |
[sound play]; |
| 121 |
} |
| 122 |
} |
| 123 |
break; |
| 124 |
|
| 125 |
default: |
| 126 |
/* |
| 127 |
* Either COCOA_SOUND_NULL or something unrecognized. |
| 128 |
* In either case, do nothing. |
| 129 |
*/ |
| 130 |
break; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Unload a sound. |
| 139 |
*/ |
| 140 |
static bool unload_sound_cocoa(struct sound_data *data) |
| 141 |
{ |
| 142 |
struct cocoa_sample *sample = (struct cocoa_sample*) (data->plat_data); |
| 143 |
|
| 144 |
if (sample) { |
| 145 |
switch (sample->sample_type) { |
| 146 |
case COCOA_SOUND_BRIDGED_NSSOUND: |
| 147 |
{ |
| 148 |
NSSound *sound = (__bridge NSSound*) (sample->sample_data.bridged_nssound); |
| 149 |
if (sound) { |
| 150 |
/* |
| 151 |
* Balance the CFBridgingRetain() from |
| 152 |
* when the sound was loaded. |
| 153 |
*/ |
| 154 |
CFRelease(sample->sample_data.bridged_nssound); |
| 155 |
} |
| 156 |
} |
| 157 |
break; |
| 158 |
|
| 159 |
default: |
| 160 |
/* |
| 161 |
* Either COCOA_SOUND_NULL or something unrecognized. |
| 162 |
* Do nothing. |
| 163 |
*/ |
| 164 |
break; |
| 165 |
} |
| 166 |
mem_free(sample); |
| 167 |
data->plat_data = NULL; |
| 168 |
} |
| 169 |
data->status = SOUND_ST_UNKNOWN; |
| 170 |
return true; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Initialize sound support (dummy; nothing needs to be done). |
| 175 |
*/ |
| 176 |
static bool open_audio_cocoa(void) |
| 177 |
{ |
| 178 |
return true; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Clean up sound support (dummy; nothing needs to be done). |
| 183 |
*/ |
| 184 |
static bool close_audio_cocoa(void) |
| 185 |
{ |
| 186 |
return true; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Return the supported sound formats. |
| 191 |
*/ |
| 192 |
static const struct sound_file_type *supported_files_cocoa(void) |
| 193 |
{ |
| 194 |
return supported_sound_formats; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Hook Cocoa-specific sound support into the core sound handling. |
| 199 |
*/ |
| 200 |
errr init_sound_cocoa(struct sound_hooks *hooks, int argc, char **argv) |
| 201 |
{ |
| 202 |
/* |
| 203 |
* Load all the sounds when sound.prf is parsed to mimic the past |
| 204 |
* behavior of the Mac client and avoid lag when a sound is first |
| 205 |
* played. |
| 206 |
*/ |
| 207 |
(void) set_preloaded_sounds(true); |
| 208 |
hooks->open_audio_hook = open_audio_cocoa; |
| 209 |
hooks->close_audio_hook = close_audio_cocoa; |
| 210 |
hooks->load_sound_hook = load_sound_cocoa; |
| 211 |
hooks->unload_sound_hook = unload_sound_cocoa; |
| 212 |
hooks->play_sound_hook = play_sound_cocoa; |
| 213 |
hooks->supported_files_hook = supported_files_cocoa; |
| 214 |
return 0; |
| 215 |
} |
| 216 |
|
| 217 |
#endif |