瀏覽代碼

Merge remote-tracking branch 'origin/master'

Brucey 5 年之前
父節點
當前提交
71b0d9e044
共有 5 個文件被更改,包括 78 次插入59 次删除
  1. 43 40
      audiosample.mod/audiosample.bmx
  2. 0 12
      blitz.mod/blitz_thread.h
  3. 8 1
      threads.mod/threads.bmx
  4. 2 6
      threads.mod/threads.c
  5. 25 0
      threads.mod/threads_mac.m

+ 43 - 40
audiosample.mod/audiosample.bmx

@@ -1,5 +1,5 @@
 
-Strict
+SuperStrict
 
 Rem
 bbdoc: Audio/Audio samples
@@ -32,22 +32,25 @@ Type TAudioSample
 	Rem
 	bbdoc: Length, in samples, of the sample data
 	end rem
-	Field length
+	Field length:Int
 	
 	Rem
 	bbdoc: Sample rate
 	end rem
-	Field hertz
+	Field hertz:Int
 	
 	Rem
 	bbdoc: Sample format
 	end rem
-	Field format
+	Field format:Int
 	
+	Rem
+	bbdoc: Allocated memory in bytes
+	End Rem
 	Field capacity:Long
 
 	Method Delete()
-		If capacity>=0 MemFree samples
+		If capacity>=0 Then MemFree( samples )
 	End Method
 
 	Rem
@@ -55,8 +58,8 @@ Type TAudioSample
 	returns: A new audio sample object
 	end rem
 	Method Copy:TAudioSample()
-		Local t:TAudioSample=Create( length,hertz,format )
-		CopySamples samples,t.samples,format,length
+		Local t:TAudioSample = Create( length,hertz,format )
+		CopySamples( samples,t.samples,format,length )
 		Return t
 	End Method
 
@@ -64,9 +67,9 @@ Type TAudioSample
 	bbdoc: Convert audio sample
 	returns: A new audio sample object in the specified format
 	end rem
-	Method Convert:TAudioSample( to_format )
-		Local t:TAudioSample=Create( length,hertz,to_format )
-		ConvertSamples samples,format,t.samples,to_format,length
+	Method Convert:TAudioSample( to_format:Int )
+		Local t:TAudioSample = Create( length,hertz,to_format )
+		ConvertSamples( samples,format,t.samples,to_format,length )
 		Return t
 	End Method
 
@@ -74,14 +77,14 @@ Type TAudioSample
 	bbdoc: Create an audio sample
 	returns: A new audio sample object
 	end rem
-	Function Create:TAudioSample( length,hertz,format )
-		Local t:TAudioSample=New TAudioSample
-		Local capacity:Long=length*BytesPerSample[format]
-		t.samples=MemAlloc( Size_T(capacity) )
-		t.length=length
-		t.hertz=hertz
-		t.format=format
-		t.capacity=capacity
+	Function Create:TAudioSample( length:Int,hertz:Int,format:Int )
+		Local t:TAudioSample = New TAudioSample
+		Local capacity:Long = length*BytesPerSample[format]
+		t.samples = MemAlloc( Size_T(capacity) )
+		t.length = length
+		t.hertz = hertz
+		t.format = format
+		t.capacity = capacity
 		Return t
 	End Function
 
@@ -89,13 +92,13 @@ Type TAudioSample
 	bbdoc: Create a static audio sample
 	returns: A new audio sample object that references an existing block of memory
 	end rem
-	Function CreateStatic:TAudioSample( samples:Byte Ptr,length,hertz,format )
-		Local t:TAudioSample=New TAudioSample
-		t.samples=samples
-		t.length=length
-		t.hertz=hertz
-		t.format=format
-		t.capacity=-1
+	Function CreateStatic:TAudioSample( samples:Byte Ptr,length:Int,hertz:Int,format:Int )
+		Local t:TAudioSample = New TAudioSample
+		t.samples = samples
+		t.length = length
+		t.hertz = hertz
+		t.format = format
+		t.capacity = -1
 		Return t
 	End Function
 
@@ -122,8 +125,8 @@ Type TAudioSampleLoader
 	Field _succ:TAudioSampleLoader
 	
 	Method New()
-		_succ=sample_loaders
-		sample_loaders=Self
+		_succ = sample_loaders
+		sample_loaders = Self
 	End Method
 	
 	Rem
@@ -157,7 +160,7 @@ the audio sample will be played. @format should be one of:
 * &SF_STEREO16BE | Stereo signed 16 bit big endian
 ]
 End Rem
-Function CreateAudioSample:TAudioSample( length,hertz,format )
+Function CreateAudioSample:TAudioSample( length:Int,hertz:Int,format:Int )
 	Return TAudioSample.Create( length,hertz,format )
 End Function
 
@@ -170,7 +173,7 @@ deleted.
 
 See #CreateAudioSample for possile @format values.
 End Rem
-Function CreateStaticAudioSample:TAudioSample( samples:Byte Ptr,length,hertz,format )
+Function CreateStaticAudioSample:TAudioSample( samples:Byte Ptr,length:Int,hertz:Int,format:Int )
 	Return TAudioSample.CreateStatic( samples,length,hertz,format )
 End Function
 
@@ -180,24 +183,24 @@ returns: An audio sample object
 end rem
 Function LoadAudioSample:TAudioSample( url:Object )
 
-	Local stream:TStream=ReadStream( url )
-	If Not stream Return
-
-	Local pos:Long=stream.Pos()
-	If pos=-1 RuntimeError "Stream is not seekable"
+	Local stream:TStream = ReadStream( url )
+	If Not stream then Return Null
 
+	Local pos:Long = stream.Pos()
+	If pos = -1 Then RuntimeError "Stream is not seekable"
+	
 	Local sample:TAudioSample
-	Local loader:TAudioSampleLoader=sample_loaders
+	Local loader:TAudioSampleLoader = sample_loaders
 	
 	While loader
-		stream.Seek pos
+		stream.Seek( pos )
 		Try
-			sample=loader.LoadAudioSample( stream )
+			sample = loader.LoadAudioSample( stream )
 		Catch ex:TStreamException
 		End Try
-		If sample Exit
-		loader=loader._succ
+		If sample Then Exit
+		loader = loader._succ
 	Wend
-	stream.Close
+	stream.Close()
 	Return sample
 End Function

+ 0 - 12
blitz.mod/blitz_thread.h

@@ -63,18 +63,6 @@ extern pthread_mutexattr_t _bb_mutexattr;
 
 #endif
 
-#ifdef __APPLE__
-
-#include <mach/semaphore.h>
-#include <mach/task.h>
-typedef semaphore_t bb_sem_t;
-#define bb_sem_init(SEMPTR,COUNT) (semaphore_create( mach_task_self(),(SEMPTR),SYNC_POLICY_FIFO,(COUNT) )>=0)
-#define bb_sem_destroy(SEMPTR) semaphore_destroy( mach_task_self(),*(SEMPTR) )
-#define bb_sem_wait(SEMPTR) semaphore_wait( *(SEMPTR) )
-#define bb_sem_post(SEMPTR) semaphore_signal( *(SEMPTR) )
-
-#endif
-
 #ifdef __linux
 
 #include <semaphore.h>

+ 8 - 1
threads.mod/threads.bmx

@@ -6,15 +6,21 @@ bbdoc: System/Threads
 End Rem
 Module BRL.Threads
 
-ModuleInfo "Version: 1.01"
+ModuleInfo "Version: 1.02"
 ModuleInfo "License: zlib/libpng"
 ModuleInfo "Copyright: Blitz Research Ltd"
 
+ModuleInfo "History: 1.02"
+ModuleInfo "History: Changed to use macOS dispatch semphores."
 ModuleInfo "History: 1.01"
 ModuleInfo "History: Use Byte Ptr instead of Int handles."
 ModuleInfo "History: 1.00"
 ModuleInfo "History: Initial release."
 
+?Threaded And macos
+
+Import "threads_mac.m"
+
 ?Threaded
 
 Import "threads.c"
@@ -581,3 +587,4 @@ Function AtomicSwap:Int( target:Int Var,value:Int )
 End Function
 
 ?
+

+ 2 - 6
threads.mod/threads.c

@@ -42,6 +42,7 @@ int threads_TryLockMutex( bb_mutex_t *mutex ){
 }
 
 //***** Semaphores *****
+#ifndef __APPLE__
 bb_sem_t *threads_CreateSemaphore( int count ){
 	bb_sem_t *sem=malloc( sizeof(bb_sem_t) );
 	if( bb_sem_init( sem,count ) ) return sem;
@@ -61,7 +62,7 @@ void threads_WaitSemaphore( bb_sem_t *sem ){
 void threads_PostSemaphore( bb_sem_t *sem ){
 	bb_sem_post( sem );
 }
-
+#endif
 //***** CondVars *****
 #ifdef _WIN32
 
@@ -232,11 +233,6 @@ int threads_TimedWaitCond(pthread_cond_t *cond,bb_mutex_t *mutex, int millisecs)
 
 #endif
 
-#ifdef __APPLE__
-int threads_TimedWaitSemaphore( bb_sem_t *sem, int millisecs ){
-}
-#endif
-
 #if __linux
 int threads_TimedWaitSemaphore( bb_sem_t *sem, int millisecs ){
 	struct timespec ts;

+ 25 - 0
threads.mod/threads_mac.m

@@ -0,0 +1,25 @@
+
+#include <brl.mod/blitz.mod/blitz.h>
+
+#include <dispatch/dispatch.h>
+
+
+dispatch_semaphore_t threads_CreateSemaphore( int count ){
+	return dispatch_semaphore_create(count);
+}
+
+void threads_CloseSemaphore(dispatch_semaphore_t sem ){
+	dispatch_release(sem);
+}
+
+void threads_WaitSemaphore( dispatch_semaphore_t sem ){
+	dispatch_semaphore_wait( sem, DISPATCH_TIME_FOREVER );
+}
+
+void threads_PostSemaphore( dispatch_semaphore_t sem ){
+	dispatch_semaphore_signal( sem );
+}
+
+int threads_TimedWaitSemaphore( dispatch_semaphore_t sem, int millisecs ){
+	return dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, (int64_t)millisecs * 1000000));
+}