Browse Source

Added AudioData.SetSample and audiodata test.

Mark Sibly 8 years ago
parent
commit
062bba0110
2 changed files with 49 additions and 13 deletions
  1. 25 13
      modules/std/audio/audiodata.monkey2
  2. 24 0
      modules/std/tests/audiodata.monkey2

+ 25 - 13
modules/std/audio/audiodata.monkey2

@@ -59,35 +59,47 @@ Class AudioData Extends Resource
 		Return BytesPerSample( _format ) * _length
 	End
 	
-	#rem monkeydoc Gets a sample at a given sample index.
-	
+	#rem monkeydoc Sets a sample at a given sample index.
+
 	@index must be in the range [0,Length).
 	
 	#end
-	Method GetSample:Float( index:Int,channel:Int=0 )
+	Method SetSample( index:Int,sample:Float,channel:Int=0 )
 		DebugAssert( index>=0 And index<_length )
 		Select _format
 		Case AudioFormat.Mono8
-			Return _data[index]/128.0-1
+			_data[index]=Clamp( sample * 128.0 + 128.0,0.0,255.0 )
 		Case AudioFormat.Stereo8
-			Return _data[index*2+(channel&1)]/128.0-1
+			_data[index*2+(channel&1)]=Clamp( sample * 128.0 + 128.0,0.0,255.0 )
 		Case AudioFormat.Mono16
-			Return Cast<Short Ptr>( _data )[index]/32767.0
+			Cast<Short Ptr>( _data )[index]=Clamp( sample * 32768.0,-32768.0,32767.0 )
 		Case AudioFormat.Stereo16
-			Return Cast<Short Ptr>( _data )[index*2+(channel&1)]/32767.0
+			Cast<Short Ptr>( _data )[index*2+(channel&1)]=Clamp( sample * 32768.0,-32768.0,32767.0 )
 		End
-		Return 0
 	End
 	
-	#rem monkeydoc @hidden Sets a sample at a given sample index.
-
+	#rem monkeydoc Gets a sample at a given sample index.
+	
 	@index must be in the range [0,Length).
 	
 	#end
-	Method SetSample( index:Int,channel:Int=0,sample:Float )
-		DebugAssert( index>=0 And index<_length )
+	Method GetSample:Float( index:Int,channel:Int=0 )
 		
-		RuntimeError( "TODO!" )
+		'Ok, note that this never returns quite +1.0 as there is one less int above 0 than below
+		'eg: range of signed ints is [-128,+127]
+		'
+		DebugAssert( index>=0 And index<_length )
+		Select _format
+		Case AudioFormat.Mono8
+			Return ( _data[index] - 128.0 ) / 128.0
+		Case AudioFormat.Stereo8
+			Return ( _data[index*2+(channel&1)] - 128.0 ) / 128.0
+		Case AudioFormat.Mono16
+			Return Cast<Short Ptr>( _data )[index]/32768.0
+		Case AudioFormat.Stereo16
+			Return Cast<Short Ptr>( _data )[index*2+(channel&1)]/32768.0
+		End
+		Return 0
 	End
 	
 	#rem monkey Loads audio data from a file.

+ 24 - 0
modules/std/tests/audiodata.monkey2

@@ -0,0 +1,24 @@
+
+#Import "<std>"
+
+Using std..
+
+Function Main()
+	
+	Local data:=New AudioData[4]
+	
+	data[0]=New AudioData( 1,AudioFormat.Mono8,11025 )
+	data[1]=New AudioData( 1,AudioFormat.Stereo8,11025 )
+	data[2]=New AudioData( 1,AudioFormat.Mono16,11025 )
+	data[3]=New AudioData( 1,AudioFormat.Stereo16,11025 )
+	
+	For Local i:=0 Until 4
+		Print "i="+i
+		For Local f:=-1.0 To 1.0 Step .125
+			Print "SetSample="+f
+			data[i].SetSample( 0,f )
+			Print "GetSample="+data[i].GetSample( 0 )
+		Next
+	Next
+
+End