Skip to content

Commit 63cacef

Browse files
committed
[[ Cocoa ]] Implemented basic MCPlatformSound API - used by audioclip.
1 parent f816c97 commit 63cacef

3 files changed

Lines changed: 96 additions & 1 deletion

File tree

engine/src/aclip.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,8 +615,46 @@ Boolean MCAudioClip::import(const char *fname, IO_handle stream)
615615

616616
#if defined FEATURE_PLATFORM_AUDIO
617617

618+
struct au_file_header_t
619+
{
620+
uint32_t magic;
621+
uint32_t offset;
622+
uint32_t size;
623+
uint32_t encoding;
624+
uint32_t sample_rate;
625+
uint32_t channels;
626+
};
627+
618628
void MCAudioClip::convert_tocontainer(void*& r_data, size_t& r_data_size)
619629
{
630+
au_file_header_t t_header;
631+
t_header . magic = 0x2e736e64;
632+
t_header . offset = sizeof(au_file_header_t);
633+
t_header . size = size;
634+
if (format == AF_MULAW)
635+
t_header . encoding = 1;
636+
else if (format == AF_SLINEAR)
637+
t_header . encoding = (swidth == 1 ? 2 : (swidth == 2 ? 3 : 4));
638+
else if (format == AF_ULINEAR)
639+
{
640+
convert_ulintoslin();
641+
t_header . encoding = (swidth == 1 ? 2 : (swidth == 2 ? 3 : 4));
642+
}
643+
t_header . sample_rate = rate;
644+
t_header . channels = nchannels;
645+
646+
swap_uint4(&t_header . magic);
647+
swap_uint4(&t_header . offset);
648+
swap_uint4(&t_header . size);
649+
swap_uint4(&t_header . encoding);
650+
swap_uint4(&t_header . sample_rate);
651+
swap_uint4(&t_header . channels);
652+
653+
r_data = malloc(sizeof(au_file_header_t) + size);
654+
r_data_size = sizeof(au_file_header_t) + size;
655+
656+
memcpy(r_data, &t_header, sizeof(au_file_header_t));
657+
memcpy(((au_file_header_t *)r_data) + 1, samples, size);
620658
}
621659

622660
Boolean MCAudioClip::open_audio(void)
@@ -630,6 +668,11 @@ Boolean MCAudioClip::open_audio(void)
630668

631669
MCPlatformSoundCreateWithData(t_data, t_data_size, s_current_sound);
632670

671+
free(t_data);
672+
673+
if (s_current_sound == nil)
674+
return False;
675+
633676
double t_volume;
634677
t_volume = loudness / 100.0;
635678
MCPlatformSoundSetProperty(s_current_sound, kMCPlatformSoundPropertyVolume, kMCPlatformPropertyTypeDouble, &t_volume);

engine/src/cmdse.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1738,7 +1738,6 @@ Exec_stat MCMM::exec(MCExecPoint &ep)
17381738
delete fname;
17391739
}
17401740
MCacptr->setlooping(looping);
1741-
MCacptr -> play();
17421741
MCU_play();
17431742
#ifndef FEATURE_PLATFORM_AUDIO
17441743
if (MCacptr != NULL)

engine/src/mac-sound.mm

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,59 +19,112 @@
1919
#include <AudioToolbox/AudioToolbox.h>
2020

2121
#include "platform.h"
22+
#include "platform-internal.h"
23+
24+
////////////////////////////////////////////////////////////////////////////////
25+
26+
@interface com_runrev_livecode_MCSoundDelegate: NSObject<NSSoundDelegate>
27+
28+
- (void)sound: (NSSound *)sound didFinishPlaying:(BOOL)finishedPlaying;
29+
30+
@end
31+
32+
@implementation com_runrev_livecode_MCSoundDelegate
33+
34+
- (void)sound:(NSSound *)sound didFinishPlaying:(BOOL)finishedPlaying
35+
{
36+
MCPlatformCallbackSendSoundFinished((MCPlatformSoundRef)sound);
37+
}
38+
39+
@end
40+
41+
static com_runrev_livecode_MCSoundDelegate *s_delegate = nil;
2242

2343
////////////////////////////////////////////////////////////////////////////////
2444

2545
void MCPlatformSoundCreateWithData(const void *p_data, size_t p_data_size, MCPlatformSoundRef& r_sound)
2646
{
47+
NSData *t_data;
48+
t_data = [NSData dataWithBytes: p_data length: p_data_size];
49+
50+
NSSound *t_sound;
51+
t_sound = [[NSSound alloc] initWithData: t_data];
52+
53+
if (s_delegate == nil)
54+
s_delegate = [[com_runrev_livecode_MCSoundDelegate alloc] init];
55+
56+
[t_sound setDelegate: s_delegate];
57+
58+
r_sound = (MCPlatformSoundRef)t_sound;
2759
}
2860

2961
void MCPlatformSoundRetain(MCPlatformSoundRef self)
3062
{
3163
NSSound *t_sound;
3264
t_sound = (NSSound *)self;
65+
[t_sound retain];
3366
}
3467

3568
void MCPlatformSoundRelease(MCPlatformSoundRef self)
3669
{
3770
NSSound *t_sound;
3871
t_sound = (NSSound *)self;
72+
[t_sound release];
3973
}
4074

4175
bool MCPlatformSoundIsPlaying(MCPlatformSoundRef self)
4276
{
4377
NSSound *t_sound;
4478
t_sound = (NSSound *)self;
79+
return [t_sound isPlaying];
4580
}
4681

4782
void MCPlatformSoundPlay(MCPlatformSoundRef self)
4883
{
4984
NSSound *t_sound;
5085
t_sound = (NSSound *)self;
86+
[t_sound play];
5187
}
5288

5389
void MCPlatformSoundPause(MCPlatformSoundRef self)
5490
{
5591
NSSound *t_sound;
5692
t_sound = (NSSound *)self;
93+
[t_sound pause];
5794
}
5895

5996
void MCPlatformSoundResume(MCPlatformSoundRef self)
6097
{
6198
NSSound *t_sound;
6299
t_sound = (NSSound *)self;
100+
[t_sound resume];
63101
}
64102

65103
void MCPlatformSoundStop(MCPlatformSoundRef self)
66104
{
67105
NSSound *t_sound;
68106
t_sound = (NSSound *)self;
107+
[t_sound stop];
69108
}
70109

71110
void MCPlatformSoundSetProperty(MCPlatformSoundRef self, MCPlatformSoundProperty property, MCPlatformPropertyType type, void *value)
72111
{
73112
NSSound *t_sound;
74113
t_sound = (NSSound *)self;
114+
115+
switch(property)
116+
{
117+
case kMCPlatformSoundPropertyVolume:
118+
[t_sound setVolume: *(double *)value];
119+
break;
120+
case kMCPlatformSoundPropertyLooping:
121+
[t_sound setLoops: *(bool *)value];
122+
break;
123+
124+
default:
125+
assert(false);
126+
break;
127+
}
75128
}
76129

77130
void MCPlatformSoundGetProperty(MCPlatformSoundRef self, MCPlatformSoundProperty property, MCPlatformPropertyType type, void *value)

0 commit comments

Comments
 (0)