WAV creation try

General FreeBASIC programming questions.
Post Reply
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

WAV creation try

Post by bluatigro »

this works [ it produces a file ]

Code: Select all

'' bluatigro 29 sept 2017
'' wav creator try

''create wav type
type TWav 
public :
  as long chunkid    ''RIFF
  as long chunksize 
  as long formatwav 
  as long subchunk1id 
  as long subchunk1size 
  as short audioformat  '' 1
  as short numchannels  '' 1 = mono 2 stereo
  as long samplerate    ''8000 41000
  as long byterate 
  as short blockalign 
  as short bitpersample ''8 16
  as long subchunk2id 
  as long subchunk2size 
  declare function toString() as string
end type
function shortStr( x as ushort ) as string
  dim as byte a , b 
  a = int( x / 256 ) and 255
  b = x and 255
  return chr( a ) + chr( b )
end function
function longStr( x as ulong ) as string
  dim as byte a , b , c , d
  a = int( x / 256 ^ 3 ) and 255
  b = int( x / 256 ^ 2 ) and 255
  c = int( x / 256 ) and 255
  d = x and 255
  return chr( a ) + chr( b ) + chr( c ) + chr( d )
end function
function TWav.toString() as string
  return longStr( chunkid ) _    ''RIFF
  + longStr( chunksize ) _ 
  + longStr( formatwav ) _ 
  + longStr( subchunk1id ) _
  + longStr( subchunk1size ) _
  + shortStr( audioformat ) _  '' 1
  + shortStr( numchannels ) _ '' 1 = mono 2 stereo
  + longStr( samplerate ) _    ''8000 41000
  + longStr( byterate ) _ 
  + shortStr( blockalign ) _
  + shortStr( bitpersample ) _ ''8 16
  + longStr( subchunk2id ) _ 
  + longStr( subchunk2size )
end function



const as long numsamples = 41000 '' 1 sec
const as double pi = atn( 1 ) * 4
 
dim shared as short wavdata( numsamples )
dim shared as TWav wav

wav.chunkid = vallng( "&52494646" ) ''RIFF
wav.formatwav = vallng( "&57415645" ) ''WAVE
wav.subchunk1id = vallng( "&666d7420") ''fmt
wav.subchunk1size = 16
wav.audioformat = 1
wav.numchannels = 1 ''1 = mono 2 = stereo
wav.samplerate = 41000
wav.bitpersample = 16
wav.blockalign = wav.numchannels * wav.bitpersample / 8
wav.byterate = numsamples _
* wav.numchannels * wav.bitpersample / 8
wav.subchunk2id = vallng( "&64617461" ) ''data
wav.subchunk2size = numsamples _
* wav.numchannels * wav.bitpersample / 8
wav.chunksize = _
4 + (8 + wav.subchunk1size ) _
+ (8 + wav.subchunk2size )
dim as integer i
for i = 0 to numsamples
  '' create note A
  wavdata( i ) = int( sin( 440 * pi * 2 / wav.samplerate ) _
  * 256 * 127 + 256 * 128 )
next i

open "note_440_1000.wav" for output as #1
  print #1 , wav.toString()
  for i = 0 to numsamples
    print #1 , shortStr( wavdata( i ) )
  next i
close #1
dim as string in
input "[ wav ready : push return ]" ; in
print "[ game over ]"
 
this gives a cryptic error

Code: Select all

'' OpenAL-based .wav player example

#include "AL/al.bi"
#include "AL/alut.bi"

'' Initialize OpenAL
alutInit(0, 0)

''
'' Load a .wav into an OpenAL buffer
''
dim as string sound_file = "note_440_1000.wav"

Dim As ALuint buffer
buffer = alutCreateBufferFromFile( SOUND_FILE )

If( alutGetError( ) <> ALUT_ERROR_NO_ERROR ) Then
    Print "Error: Loading the .wav failed!" : Sleep : End 1
End If

''
'' Setup sound sources -- each one can have a 3D position & velocity.
''
Dim As ALuint source
alGenSources(1, @source)

Dim As ALfloat SourcePos(0 To 2) = {0.0, 0.0, 0.0}
Dim As ALfloat SourceVel(0 To 2) = {0.0, 0.0, 0.0}

alSourcei(source, AL_BUFFER, buffer)
alSourcef(source, AL_PITCH, 1.0)
alSourcef(source, AL_GAIN, 1.0)
alSourcefv(source, AL_POSITION, @SourcePos(0))
alSourcefv(source, AL_VELOCITY, @SourceVel(0))

''
'' Setup the listener's 3D position etc.
''
Dim As ALfloat listener_position(0 To 2) = {0.0, 0.0, 0.0}
Dim As ALfloat listener_velocity(0 To 2) = {0.0, 0.0, 0.0}
Dim As ALfloat listener_orientation(0 To 5) = {0.0, 0.0, -1.0, _ '' look at
                                               0.0, 0.0, 1.0}    '' up vector
alListenerfv(AL_POSITION, @listener_position(0))
alListenerfv(AL_VELOCITY, @listener_velocity(0))
alListenerfv(AL_ORIENTATION, @listener_orientation(0))

''
'' Play the sound (change to AL_FALSE to disable looping)
''
alSourcei(source, AL_LOOPING, AL_TRUE)
alSourcePlay(source)

Print "Sound is playing, press ESC to exit and anything else to pause..."
Dim As String key
Dim As Integer active = -1
Do
    key = Inkey()
    If (Len(key) > 0) Then
        If (key = Chr(27)) Then
            Exit Do
        End If

        If (active) Then
            alSourcePause(source)
            active = 0
        Else
            alSourcePlay(source)
            active = -1
        End If
    End If
Loop

alSourceStop(source)

''
'' Clean up
''
alDeleteBuffers(1, @buffer)
alDeleteSources(1, @source)
alutExit()

i got the last from a rebuild exampe
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: WAV creation try

Post by bluatigro »

update :
it produces 1 string whitout ',' now

Code: Select all

'' bluatigro 30 mrt 2020
'' wav creator try
const as long numsamples = 41000 '' 1 sec

dim shared as short wavdata( numsamples )

''create wav type
type TWav 
public :
  as long chunkid    ''RIFF
  as long chunksize 
  as long formatwav 
  as long subchunk1id 
  as long subchunk1size 
  as short audioformat  '' 1
  as short numchannels  '' 1 = mono 2 stereo
  as long samplerate    ''8000 41000
  as long byterate 
  as short blockalign 
  as short bitpersample ''8 16
  as long subchunk2id 
  as long subchunk2size 
  declare function toString() as string
end type
function shortStr( x as ushort ) as string
  dim as byte a , b 
  a = int( x / 256 ) and 255
  b = x and 255
  return chr( a ) + chr( b )
end function
function longStr( x as ulong ) as string
  dim as byte a , b , c , d
  a = int( x / 256 ^ 3 ) and 255
  b = int( x / 256 ^ 2 ) and 255
  c = int( x / 256 ) and 255
  d = x and 255
  return chr( a ) + chr( b ) + chr( c ) + chr( d )
end function
function TWav.toString() as string
  dim as string uit = longStr( chunkid ) _    ''RIFF
  + longStr( chunksize ) _ 
  + longStr( formatwav ) _ 
  + longStr( subchunk1id ) _
  + longStr( subchunk1size ) _
  + shortStr( audioformat ) _  '' 1
  + shortStr( numchannels ) _ '' 1 = mono 2 stereo
  + longStr( samplerate ) _    ''8000 41000
  + longStr( byterate ) _ 
  + shortStr( blockalign ) _
  + shortStr( bitpersample ) _ ''8 16
  + longStr( subchunk2id ) _ 
  + longStr( subchunk2size )
  dim as integer i
  for i = 0 to numsamples
    uit += shortStr( wavData( i ) )
  next i
  return uit
end function

const as double pi = atn( 1 ) * 4
 
dim shared as TWav wav

wav.chunkid = vallng( "&52494646" ) ''RIFF
wav.formatwav = vallng( "&57415645" ) ''WAVE
wav.subchunk1id = vallng( "&666d7420") ''fmt
wav.subchunk1size = 16
wav.audioformat = 1
wav.numchannels = 1 ''1 = mono 2 = stereo
wav.samplerate = 41000
wav.bitpersample = 16
wav.blockalign = wav.numchannels * wav.bitpersample / 8
wav.byterate = numsamples _
* wav.numchannels * wav.bitpersample / 8
wav.subchunk2id = vallng( "&64617461" ) ''data
wav.subchunk2size = numsamples _
* wav.numchannels * wav.bitpersample / 8
wav.chunksize = _
4 + (8 + wav.subchunk1size ) _
+ (8 + wav.subchunk2size )
dim as integer i
for i = 0 to numsamples
  '' create note A 1 sec long
  wavdata( i ) = int( sin( 440 * pi * 2 / wav.samplerate ) _
  * 256 * 127 + 256 * 128 )
next i

open "note_440_1000.wav" for output as #1
  print #1 , wav.toString()
close #1
dim as string in
input "[ wav ready : push return ]" ; in
print "[ game over ]"
 
Post Reply