creating audio sample

BlitzMax Forums/BlitzMax Beginners Area/creating audio sample

yossi(Posted 2014) [#1]
I have some questions about next code:

' createaudiosample.bmx

Local sample:TAudioSample=CreateAudioSample( 32,11025,SF_MONO8 )

For Local k=0 Until 32
sample.samples[k]=Sin(k*360/32)*127.5+127.5
Next

Local sound:TSound=LoadSound( sample,True )

PlaySound(sound)

Input

1. what is the mining of 32 in the first parameter of createaudiosample ?
2. what is the meaning of 11025 in the second parameter ?
3. what is the meaning of SF_MONO8 in the 3 parameter ?
4. what is the meaning of the values in sample.samples[] ?


Mr. Goober(Posted 2014) [#2]
1. The number of 'samples' in the sound.
2. The playback frequency (samples per second)
3. The sound format (in this case 8-bit monosound)
4. The values in the audio sample's sample array (this example has it so when played, it prodyces a sine wave sound).

Samples in an audio sample are merely numbers. The sample array is basically an array of bytes. In 8-bit monosound, values only range 0-255 for each sample in the sound. An 'audio sample' is a sound that has 'bit samples.'


yossi(Posted 2014) [#3]
is it true to say that CreateAudioSample is aim to create midi song of music instrument so we get that:
the first parameter represents the number of notes we play.
the second parameter represents how speed we play the song
and the third parameter represents the quality of the instrument playing the song ?

if so I understand that sample.samples[] actually hold the notes of the song ?

and if what I say is true then how can I set the music instrument (piano\guitar\harmonica...) ?


Mr. Goober(Posted 2014) [#4]
No, an Audio Sample is considered one sound. You can load the sound using LoadAudioSample("examplesound.wav") where .wav could also be .ogg or another supported audio format.

The array of samples inside of an audio sample are the actual data of the sound stored in memory. They don't represent notes like a MIDI file. The audio sample's "sample array" is meant to be played at the frequency mentioned above (in this example 11025 Hz). This is very fast. If you load a sound that lasts for 1 second and is played back at this frequency, then that means the sound has 11025 samples.

I don't know if BlitzMax has support for .midi files, but you're on the right track. You can use Audio Samples to create the instruments you want to play in a sequence, but you'll need to find a module for it, or make it yourself.


Midimaster(Posted 2014) [#5]
If you want to create MIDI songs, CreateAudioSample() is the wrong function for you.

For MIDI you have two possibilities:

1.
Use and play a complete MIDI-Song "sample.mid".

2.
Fire MIDI Events to the MIDI Device. Then you use Bruceys module "BAH.MOD/RTMIDI.MOD"

Here is a small sample about using MIDI:

SuperStrict

Import BaH.RtMidi

Graphics 300,200

' Midi-Out-Check und Start
		Global midiOut:TRtMidiOut = New TRtMidiOut.Create()
		Global message:Byte[] = New Byte[3]
		

		If midiOut.getPortCount() = 0 Then
			Print "No Input Ports"
			End
		End If
		
		For Local i:Int = 0 Until midiOut.getPortCount()
			Print "  Input port #" + i + " : " + midiOut.getPortName(i)
		Next
		
		Print "Opening " + midiOut.getPortName()
 		midiOut.openPort(0)
		
 
StartTon=36 ' von c
EndTon = 72 ' bis c''
Kanal=1     ' Töne=1 ,  Drums=9
Laut=90     ' Midi Volume
Lang=2000    ' Tonlänge: 500msec ~ ca 1/4 Note bei Tempo 120
Instrument=0  ' GM Instrument-Nr
SampleName="Piano"


Global Zeit%=MilliSecs()+500
Delay 200
ProgrammChange()
Ton=StartTon

Repeat
	If Zeit<MilliSecs()
		Select Mode
			Case 0
				'play
				SpielTon Ton, Laut, Kanal
				Zeit=Zeit + Lang
				Mode=1
				
			Case 1
				'stop
				SpielTon Ton, 0, Kanal				
				Zeit=Zeit+ Warte
				Mode=2
			Case 2
				HoleAlleSamplesAb
				Zeit=Zeit+ Warte
				Ton=Ton +1
				Mode=0
		End Select
	EndIf

	DrawText  "press esc to exit",100,100
    Flip 0
    Delay 15
    If Ton>=EndTon Then Exit
	
Until KeyHit(Key_Escape)

SpielTon Ton, 0, Kanal				
midiout.Free
End


Function SpielTon (Ton%, Laut% , Kanal%)
	message[0] = 144 + Kanal
	message[1] = Ton
	message[2] = Laut
	midiout.putMessage(message, 3)
End Function	


Function ProgrammChange()
	message[0] = 192+Kanal
	message[1] = Instrument
	message[2] = 0
	midiout.putMessage(message, 2)	
End Function



yossi(Posted 2014) [#6]
can I mathematically map a given note on a given instrument to a data value in sample.samples[] ?


Mr. Goober(Posted 2014) [#7]
No. The samples[] array is used to create sound waves. If samples[0] is 255 and samples[1] is 0, the difference between the two samples will generate a sound. Similarly, square waves are designed using this similar manner.

If 64 samples are allocated, with 0-31 being 0 and 32-63 being 255, this creates a clean, retro-sounding note when played back at a high frequency.

In contrast, if you happen to assign all sampels the same value, no sound will be played. This is because there is no difference in the samples, and cannot generate a sound of any sort. Sounds are generated by vibrations, and the samples array in an audio sample is used to emulate that.

EDIT:

This is a visual example of the sound used in the tutorial example.