LB Booster
« creating wav's ? »

Welcome Guest. Please Login or Register.
Oct 17th, 2017, 03:53am


Speed up Liberty BASIC programs by up to ten times!
Compile Liberty BASIC programs to compact, standalone executables!
Overcome many of Liberty BASIC's bugs and limitations!
LB Booster Resources
LB Booster documentation
LB Booster Home Page
LB Booster technical Wiki
Just BASIC forum
LB Umbrella forum
Liberty BASIC forum (the original)

« Previous Topic | Next Topic »
Pages: 1 2  Notify Send Topic Print
 hotthread  Author  Topic: creating wav's ?  (Read 862 times)
bluatigro
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 111
xx creating wav's ?
« Thread started on: Dec 15th, 2016, 11:22am »

i m trying to translate this c Code:
struct TWAVHeader
{
 char rID[4]; // "RIFF"
 dword rLen; // file size minus 8
 char wID[4]; // "WAVE"
 char fID[4]; // " fmt"
 dword fLen; // 16
 word FmtTag; // 1
 word Channels; // 1 = Mono, 2 = Stereo
 dword SampleRate; // 11025 = 11kHz, 22050 = 22 kHz, etc.
 dword AvgBytesPerSec; // SampleRate*Channels*BitsPerSample/8
 word BlockAlign; // Channels * BitsPerSample/8
 word BitsPerSample; // 8, 16, etc.
 char dID[4]; // "data"
 dword dLen; // length in bytes of the sound buffer
};

int Amp=127;
float Fr=440*pow(2,(float)1/4);
char *Buf;
long BufSize=61440;
long SampleRate=11025;


void Generate()
{
 for( long i=0; i255 ) Buf[i]=255;
 else if( y<0 ) Buf[i] = 0;
 else Buf[i] = y;
 }
}

void main()
{

 FILE *f;
 TWAVHeader wh;

 Buf = new char[BufSize];
 Generate();

 f = fopen("gen.wav","wb");
 strcpy(wh.rID,"RIFF");
 wh.rLen = sizeof(wh)+BufSize-8;
 strcpy(wh.wID,"WAVE");
 strcpy(wh.fID,"fmt ");
 wh.fLen = 16;
 wh.FmtTag = 1;
 wh.Channels = 1;
 wh.SampleRate = SampleRate;
 wh.AvgBytesPerSec = SampleRate;
 wh.BlockAlign = 1;
 wh.BitsPerSample = 8;
 strcpy(wh.dID,"data");
 wh.dLen = BufSize;
 fwrite(&wh,sizeof(wh),1,f);
 fwrite(Buf,1,BufSize,f);
 fclose(f);

 delete Buf;
}


 

into Code:
''bluatigro 15 dec 2016
struct wavh
  rID as char( 3 ) _ ''; // "RIFF"
  rlen as ulong _  ''; // file size minus 8
  wID as char( 3 ) _ ''; // "WAVE"
  fID as char( 3 ) _ ''; // " fmt"
  flen as ulong _   ''; // 16
  word FmtTag; // 1
  word Channels; // 1 = Mono, 2 = Stereo
  SampleRate as ulong _''; // 11025 = 11kHz, 22050 = 22 kHz, etc.
  AvgBytesPerSec as ulong  _''; // SampleRate*Channels*BitsPerSample/8
  word BlockAlign; // Channels * BitsPerSample/8
  word BitsPerSample; // 8, 16, etc.
  dID( 3 ) as ulong _''; // "data"
  dlen as ulong   ''; // length in bytes of the sound buffer
end type  

samplerate = 11025
sec = 1
bufsize = samplerate * sec
fr = 440

dim buf( bufsize )

pi = atn( 1 ) * 4

for i = 0 to bufmax
  buf(i) = sin( i * fr * pi * 2 / saplerate ) * 127 + 128
end sub

 open "440_1000.wav" for output as #1
 wh.rID "RIFF" 
 wh.rLen = sizeof(wh) + ( SampleRate * sec ) - 8
 wh.wID = "WAVE"
 wh.fID = "fmt "
 wh.fLen = 16
 wh.FmtTag = 1
 wh.Channels = 1
 wh.SampleRate = SampleRate
 wh.AvgBytesPerSec = SampleRate
 wh.BlockAlign = 1
 wh.BitsPerSample = 8
 wh.dID = "data"
 wh.dLen = BufSize
 fwrite(&wh,sizeof(wh),1,f);
 fwrite(Buf,1,BufSize,f);
 close #1
 
print "ready"


 
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1273
xx Re: creating wav's ?
« Reply #1 on: Dec 15th, 2016, 1:59pm »

on Dec 15th, 2016, 11:22am, bluatigro wrote:
i m trying to translate this c

Unfortunately LB/LBB won't let you do this:

Code:
    struct wavh, rID as char[4] 
    wavh.rID.struct = "RIFF" 

It appears to work (neither LB nor LBB gives you an error message) but instead of the string "RIFF" being written to the structure what actually happens is that only "RIF" is written. You can confirm it as follows:

Code:
    struct wavh, rID as char[4]
    wavh.rID.struct = "RIFF"
    print wavh.struct 

The reason it doesn't work as you would hope is that the CHAR[] structure member type in LB and LBB contains a NUL-terminated string, so char[4] has room only for a 3-character string plus the NUL.

As a result of this limitation you have to cheat slightly to get the string "RIFF" (without a NUL-termination) into the struct. The way to do it is as follows:

Code:
    struct wavh, rID as ulong
    wavh.rID.struct = hexdec("46464952") ' "RIFF" 

You can extend that technique to create the entire WAV header struct correctly. Here is the complete translated program to generate 1 second of 440 Hz tone (only 8-bits per sample so the quality isn't great):

Code:
    struct wavh,_ 
     rID as ulong, _            ' "RIFF"
     rLen as ulong, _           ' file size minus 8
     wID as ulong, _            ' "WAVE"
     fID as ulong, _            ' "fmt "
     fLen as ulong, _           ' 16
     FmtTag as word, _          ' 1
     Channels as word, _        ' 1 = Mono, 2 = Stereo
     SampleRate as ulong, _     ' 11025 = 11kHz, 22050 = 22 kHz, etc.
     AvgBytesPerSec as ulong, _ ' SampleRate*Channels*BitsPerSample/8
     BlockAlign as word,_       ' Channels * BitsPerSample/8
     BitsPerSample as word,_    ' 8, 16, etc.
     dID as ulong,_             ' "data"
     dLen as ulong              ' length in bytes of the sound buffer
 
    wavh.rID.struct = hexdec("46464952") ' "RIFF"
    wavh.wID.struct = hexdec("45564157") ' "WAVE"
    wavh.fID.struct = hexdec("20746D66") ' "fmt "
    wavh.dID.struct = hexdec("61746164") ' "data"

    samplerate = 11025
    sec = 1
    bufsize = samplerate * sec
    fr = 440
    
    wavh.rLen.struct = len(wavh.struct) + bufsize - 8
    wavh.fLen.struct = 16
    wavh.FmtTag.struct = 1
    wavh.Channels.struct = 1 ' Mono
    wavh.SampleRate.struct = samplerate
    wavh.BitsPerSample.struct = 8
    wavh.AvgBytesPerSec.struct = wavh.SampleRate.struct * _
                                 wavh.Channels.struct * wavh.BitsPerSample.struct/8
    wavh.BlockAlign.struct = wavh.Channels.struct * wavh.BitsPerSample.struct/8
    wavh.dLen.struct = bufsize

    pi = atn( 1 ) * 4

    open "440_1000.wav" for output as #1
    print #1, wavh.struct;
    for i = 1 to bufsize
        print #1, chr$(sin( i * fr * pi * 2 / samplerate ) * 127 + 128);
    next
    close #1
    
    playwave "440_1000.wav"
    print "Ready"
    end 

This runs in both LB4 and LBB.

Richard.
« Last Edit: Dec 15th, 2016, 2:31pm by Richard Russell » User IP Logged

bluatigro
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 111
xx Re: creating wav's ?
« Reply #2 on: Dec 16th, 2016, 10:48am »

@ richard :
thanks for info

update :
FIRST MUZIK

Code:
    struct wavh,_ 
     rID as ulong, _            ' "RIFF"
     rLen as ulong, _           ' file size minus 8
     wID as ulong, _            ' "WAVE"
     fID as ulong, _            ' "fmt "
     fLen as ulong, _           ' 16
     FmtTag as word, _          ' 1
     Channels as word, _        ' 1 = Mono, 2 = Stereo
     SampleRate as ulong, _     ' 11025 = 11kHz, 22050 = 22 kHz, etc.
     AvgBytesPerSec as ulong, _ ' SampleRate*Channels*BitsPerSample/8
     BlockAlign as word,_       ' Channels * BitsPerSample/8
     BitsPerSample as word,_    ' 8, 16, etc.
     dID as ulong,_             ' "data"
     dLen as ulong              ' length in bytes of the sound buffer
 
    wavh.rID.struct = hexdec("46464952") ' "RIFF"
    wavh.wID.struct = hexdec("45564157") ' "WAVE"
    wavh.fID.struct = hexdec("20746D66") ' "fmt "
    wavh.dID.struct = hexdec("61746164") ' "data"

    samplerate = 11025
    sec = 1 / 8
    bufsize = samplerate * sec

    for okt = 0 to 4
    for note = 0 to 7
    fr = hz( okt , note )
    
    wavh.rLen.struct = len(wavh.struct) + bufsize - 8
    wavh.fLen.struct = 16
    wavh.FmtTag.struct = 1
    wavh.Channels.struct = 1 ' Mono
    wavh.SampleRate.struct = samplerate
    wavh.BitsPerSample.struct = 8
    wavh.AvgBytesPerSec.struct = wavh.SampleRate.struct * _
                                 wavh.Channels.struct * wavh.BitsPerSample.struct/8
    wavh.BlockAlign.struct = wavh.Channels.struct * wavh.BitsPerSample.struct/8
    wavh.dLen.struct = bufsize

    pi = atn( 1 ) * 4

    open "wav\"+str$(fr)+".wav" for output as #1
    print #1, wavh.struct;
    for i = 1 to bufsize
        print #1, chr$(sin( i * fr * pi * 2 / samplerate ) * 127 + 128);
    next
    close #1
    next note
    next okt

    call play "c3 d3 e3 c3 c3 d3 e3 c3"

    print "Ready"
    end 
function hz( okt , note )
  hz = int( 55 * 2 ^ ( okt + note / 8 ) )
end function
sub playnote okt , note$
  note = instr( "abcdefg" , note$ )
  print "playing " ; okt ; " " ; note$
  playwave "wav\"+str$(hz(okt,note))+".wav" 
end sub
sub play muzik$
  i = 1
  while word$( muzik$ , i ) <> ""
    q$ = word$( muzik$ , i )
    note$ = left$( q$ , 1 )
    okt = val( right$( q$ , 1 ) )
    call playnote okt , note$
    i = i + 1
  wend
end sub
 


how do i do this whit 16 bit muzik ?
User IP Logged

bluatigro
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 111
xx Re: creating wav's ?
« Reply #3 on: Dec 16th, 2016, 11:03am »

update :
added #
Code:
    struct wavh,_ 
     rID as ulong, _            ' "RIFF"
     rLen as ulong, _           ' file size minus 8
     wID as ulong, _            ' "WAVE"
     fID as ulong, _            ' "fmt "
     fLen as ulong, _           ' 16
     FmtTag as word, _          ' 1
     Channels as word, _        ' 1 = Mono, 2 = Stereo
     SampleRate as ulong, _     ' 11025 = 11kHz, 22050 = 22 kHz, etc.
     AvgBytesPerSec as ulong, _ ' SampleRate*Channels*BitsPerSample/8
     BlockAlign as word,_       ' Channels * BitsPerSample/8
     BitsPerSample as word,_    ' 8, 16, etc.
     dID as ulong,_             ' "data"
     dLen as ulong              ' length in bytes of the sound buffer
 
    wavh.rID.struct = hexdec("46464952") ' "RIFF"
    wavh.wID.struct = hexdec("45564157") ' "WAVE"
    wavh.fID.struct = hexdec("20746D66") ' "fmt "
    wavh.dID.struct = hexdec("61746164") ' "data"

    samplerate = 11025
    sec = 1 / 8
    bufsize = samplerate * sec

    for okt = 0 to 4
    for note = 0 to 13
    fr = hz( okt , note )
    
    wavh.rLen.struct = len(wavh.struct) + bufsize - 8
    wavh.fLen.struct = 16
    wavh.FmtTag.struct = 1
    wavh.Channels.struct = 1 ' Mono
    wavh.SampleRate.struct = samplerate
    wavh.BitsPerSample.struct = 8
    wavh.AvgBytesPerSec.struct = wavh.SampleRate.struct * _
                                 wavh.Channels.struct * wavh.BitsPerSample.struct/8
    wavh.BlockAlign.struct = wavh.Channels.struct * wavh.BitsPerSample.struct/8
    wavh.dLen.struct = bufsize

    pi = atn( 1 ) * 4

    open "wav\"+str$(fr)+".wav" for output as #1
    print #1, wavh.struct;
    for i = 1 to bufsize
        print #1, chr$(sin( i * fr * pi * 2 / samplerate ) * 127 + 128);
    next
    close #1
    next note
    next okt

    call play "c3 d3 e3 c3 c3 d3 e3 c3"

    print "Ready"
    end 
function hz( okt , note )
  hz = int( 55 * 2 ^ ( okt + note / 16 ) )
end function
sub playnote okt , note$
  note = ( instr( "a a#b b#c c#d d#e e#f f#g g#" , note$ ) - 1 ) / 2
  print "playing " ; okt ; " " ; note$
  playwave "wav\"+str$(hz(okt,note))+".wav" 
end sub
sub play muzik$
  i = 1
  while word$( muzik$ , i ) <> ""
    q$ = word$( muzik$ , i )
    note$ = left$( q$ , len( q$ ) - 1 )
    okt = val( right$( q$ , 1 ) )
    call playnote okt , note$
    i = i + 1
  wend
end sub



 
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1273
xx Re: creating wav's ?
« Reply #4 on: Dec 16th, 2016, 2:46pm »

on Dec 15th, 2016, 1:59pm, Richard Russell wrote:
You can extend that technique to create the entire WAV header struct correctly.

Rod has written elsewhere that "You don't need a struct to create the header for a .wav file". Of course you don't need one, but using a struct is preferable to any other method. After all that's how the original C code was written, and it makes the program much clearer and easier to understand, as well as reducing the likelihood of a mistake being made.

As an illustration of the benefits of using a struct you just need to compare this code:

Code:
    wavh.BlockAlign.struct=wavh.Channels.struct*wavh.BitsPerSample.struct/8 

with this:

Code:
    blockAlign=(bitsPerSample*channels)/8
    number=blockAlign
    byte2=int(number/256)
    byte1=number-(byte2*256)
    header$=header$+chr$(byte1)+chr$(byte2) ' block align 

Don't return to the stone age! Use structs to the full; you know it makes sense!

Richard.
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1273
xx Re: creating wav's ?
« Reply #5 on: Dec 16th, 2016, 2:55pm »

on Dec 16th, 2016, 10:48am, bluatigro wrote:
how do i do this whit 16 bit muzik ?

Here is the original program modified for 16-bits:

Code:
    struct wavh,_
     rID as ulong, _            ' "RIFF"
     rLen as ulong, _           ' file size minus 8
     wID as ulong, _            ' "WAVE"
     fID as ulong, _            ' "fmt "
     fLen as ulong, _           ' 16
     FmtTag as word, _          ' 1
     Channels as word, _        ' 1 = Mono, 2 = Stereo
     SampleRate as ulong, _     ' 11025 = 11kHz, 22050 = 22 kHz, etc.
     AvgBytesPerSec as ulong, _ ' SampleRate*Channels*BitsPerSample/8
     BlockAlign as word,_       ' Channels * BitsPerSample/8
     BitsPerSample as word,_    ' 8, 16, etc.
     dID as ulong,_             ' "data"
     dLen as ulong              ' length in bytes of the sound buffer

    wavh.rID.struct = hexdec("46464952") ' "RIFF"
    wavh.wID.struct = hexdec("45564157") ' "WAVE"
    wavh.fID.struct = hexdec("20746D66") ' "fmt "
    wavh.dID.struct = hexdec("61746164") ' "data"

    samplerate = 11025
    sec = 1
    bufsize = samplerate * sec * 2
    fr = 440

    wavh.rLen.struct = len(wavh.struct) + bufsize - 8
    wavh.fLen.struct = 16
    wavh.FmtTag.struct = 1
    wavh.Channels.struct = 1 ' Mono
    wavh.SampleRate.struct = samplerate
    wavh.BitsPerSample.struct = 16
    wavh.AvgBytesPerSec.struct = wavh.SampleRate.struct * _
                                 wavh.Channels.struct * wavh.BitsPerSample.struct/8
    wavh.BlockAlign.struct = wavh.Channels.struct * wavh.BitsPerSample.struct/8
    wavh.dLen.struct = bufsize

    pi = atn( 1 ) * 4

    open "440_1000.wav" for output as #1
    print #1, wavh.struct;
    for i = 1 to bufsize
        sample = sin( i * fr * pi * 2 / samplerate ) * 32767
        print #1, chr$(sample and 255);chr$((sample and 65280)/256);
    next
    close #1

    playwave "440_1000.wav"
    print "Ready"
    end 

Richard.
User IP Logged

bluatigro
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 111
xx Re: creating wav's ?
« Reply #6 on: Dec 17th, 2016, 10:32am »

update :
16 bit muzik !!

error :
my melody stops halfway

Code:
    struct wavh,_
     rID as ulong, _            ' "RIFF"
     rLen as ulong, _           ' file size minus 8
     wID as ulong, _            ' "WAVE"
     fID as ulong, _            ' "fmt "
     fLen as ulong, _           ' 16
     FmtTag as word, _          ' 1
     Channels as word, _        ' 1 = Mono, 2 = Stereo
     SampleRate as ulong, _     ' 11025 = 11kHz, 22050 = 22 kHz, etc.
     AvgBytesPerSec as ulong, _ ' SampleRate*Channels*BitsPerSample/8
     BlockAlign as word,_       ' Channels * BitsPerSample/8
     BitsPerSample as word,_    ' 8, 16, etc.
     dID as ulong,_             ' "data"
     dLen as ulong              ' length in bytes of the sound buffer

    wavh.rID.struct = hexdec("46464952") ' "RIFF"
    wavh.wID.struct = hexdec("45564157") ' "WAVE"
    wavh.fID.struct = hexdec("20746D66") ' "fmt "
    wavh.dID.struct = hexdec("61746164") ' "data"
global samplerate
    samplerate = 11025
    sec = 16
global bufsize
    bufsize = samplerate * sec
global pi , tel
    pi = atn( 1 ) * 4
    dim fr( bufsize )

    call addnote 3 , "e" , 1/2
    call addnote 3 , "g" , 1/2

    call addnote 3 , "c" , 1/2
    call addnote 3 , "e" , 1/2

    call addnote 3 , "e" , 1/4
    call addnote 3 , "d" , 1/4
    call addnote 3 , "f" , 1/2

    call addnote 3 , "f" , 1

    call addnote 3 , "d" , 1/2
    call addnote 3 , "f" , 1/4
    call addnote 3 , "f" , 1/4

    call addnote 3 , "b" , 1/2
    call addnote 3 , "d" , 1/2

    call addnote 3 , "d" , 1/4
    call addnote 3 , "c" , 1/4
    call addnote 3 , "e" , 1/2

    call addnote 3 , "e" , 1

    call addnote 3 , "e" , 1/4
    call addnote 3 , "e" , 1/4
    call addnote 3 , "g" , 1/4
    call addnote 3 , "g" , 1/4

    call addnote 3 , "c" , 1/2
    call addnote 3 , "e" , 1/4
    call addnote 3 , "e" , 1/4

    call addnote 3 , "e" , 1/4
    call addnote 3 , "d" , 1/4
    call addnote 3 , "f" , 1/2

    call addnote 3 , "f" , 1

    call addnote 3 , "d" , 1/2
    call addnote 3 , "f" , 1/4
    call addnote 3 , "f" , 1/4

    call addnote 3 , "b" , 1/2
    call addnote 3 , "d" , 1/2

    call addnote 3 , "d" , 1/4
    call addnote 3 , "c" , 1/4
    call addnote 3 , "c" , 1/2

    call addnote 3 , "c" , 1

    call savewav "wav\marianne"

    print "Ready"
    playwave "wav\marianne.wav" , async
    print "playing wav"
    end
function hz( okt , note )
  hz = 55 * 2 ^ ( okt + note / 16 )
end function
sub addnote okt , note$ , l
  note = ( instr( "a a#b b#c c#d d#e e#f f#g g#" , note$ ) - 1 ) / 2
  if tel >= bufsize then exit sub
  for i = tel to tel + int( l * samplerate )
    if note < 0 then
      fr(i) = 0
    else
      fr(i) = hz( okt , note )
    end if
  next i
  tel = tel + int( l * samplerate )
end sub
sub savewav file$
    wavh.rLen.struct = len(wavh.struct) + bufsize - 8
    wavh.fLen.struct = 16
    wavh.FmtTag.struct = 1
    wavh.Channels.struct = 1 ' Mono
    wavh.SampleRate.struct = samplerate
    wavh.BitsPerSample.struct = 16
    wavh.AvgBytesPerSec.struct = wavh.SampleRate.struct * _
                                 wavh.Channels.struct * wavh.BitsPerSample.struct/8
    wavh.BlockAlign.struct = wavh.Channels.struct * wavh.BitsPerSample.struct/8
    wavh.dLen.struct = bufsize

    open file$ + ".wav" for output as #wav
    print #wav , wavh.struct;
    for i = 1 to bufsize
        sample = sin( i * fr(i) * pi * 2 / samplerate ) * 32767
        low = sample and 255
        high = ( sample and 65280 ) / 256
        print #wav , chr$( low ) ; chr$( high ) ;
    next i
    close #wav
end sub

 
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1273
xx Re: creating wav's ?
« Reply #7 on: Dec 17th, 2016, 1:32pm »

on Dec 17th, 2016, 10:32am, bluatigro wrote:
my melody stops halfway

Your calculation of bufsize is off by a factor-of-two:

Code:
global bufsize
    bufsize = samplerate * sec 

This is the number of samples, not the number of bytes! It's easy to do the calculation properly because you already know the 'average bytes per second' value from the structure. So here is the corrected sum:

Code:
    bufsize = wavh.AvgBytesPerSec.struct * sec 

Needless to say you must be very careful where in the program you perform this calculation (after the variables referenced in the right-hand-side are set, but before the bufsize variable is used)! LB's behavior of assuming an undefined variable has the value zero, rather than reporting an error, is extremely unhelpful in this regard.

Edit: There are 12 semitones in an octave, not 16 !!!!

Richard.
« Last Edit: Dec 17th, 2016, 1:39pm by Richard Russell » User IP Logged

bluatigro
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 111
xx Re: creating wav's ?
« Reply #8 on: Dec 19th, 2016, 09:02am »

richard wrote Quote:
Edit: There are 12 semitones in an octave, not 16 !!!!


i dont know whitch of the #'s dont exist
i m not a muzik pro
User IP Logged

bluatigro
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 111
xx Re: creating wav's ?
« Reply #9 on: Dec 19th, 2016, 09:52am »

update :
key's for up|down

has a error

Code:
    struct wavh,_
     rID as ulong, _            ' "RIFF"
     rLen as ulong, _           ' file size minus 8
     wID as ulong, _            ' "WAVE"
     fID as ulong, _            ' "fmt "
     fLen as ulong, _           ' 16
     FmtTag as word, _          ' 1
     Channels as word, _        ' 1 = Mono, 2 = Stereo
     SampleRate as ulong, _     ' 11025 = 11kHz, 22050 = 22 kHz, etc.
     AvgBytesPerSec as ulong, _ ' SampleRate*Channels*BitsPerSample/8
     BlockAlign as word,_       ' Channels * BitsPerSample/8
     BitsPerSample as word,_    ' 8, 16, etc.
     dID as ulong,_             ' "data"
     dLen as ulong              ' length in bytes of the sound buffer

    wavh.rID.struct = hexdec("46464952") ' "RIFF"
    wavh.wID.struct = hexdec("45564157") ' "WAVE"
    wavh.fID.struct = hexdec("20746D66") ' "fmt "
    wavh.dID.struct = hexdec("61746164") ' "data"

    wavh.fLen.struct = 16
    wavh.FmtTag.struct = 1
    wavh.Channels.struct = 2 ' stereo
    wavh.SampleRate.struct = samplerate
    wavh.BitsPerSample.struct = 16
    wavh.AvgBytesPerSec.struct = wavh.SampleRate.struct * _
                                 wavh.Channels.struct * wavh.BitsPerSample.struct/8
    wavh.BlockAlign.struct = wavh.Channels.struct * wavh.BitsPerSample.struct/8

global samplerate , sec
    samplerate = 11025
    sec = 5
global bufsize
    bufsize = wavh.AvgBytesPerSec.struct * sec
global pi , tel
    pi = atn( 1 ) * 4
    dim fr( bufsize ) , fl( bufsize )
dim l$( sec * 8 ) , r$( sec * 8 )
global wavfile$ , txtfile$
menu #m , "file" _
     , "new" , [new] _
     , "load txt" , [loadTxt] _
     , "save txt" , [saveTxt] _
     , "save wav" , [saveWav] _
     , "play wav" , [play] _
     , "exit" , [quit]
WindowWidth = 1024
WindowHeight = 768
global winx , winy
winx = WindowWidth
winy = WindowHeight
global chanel , red , green , screeny
red = 0
green = 1
menu #m , "chanel" _
     , "red" , [red] _
     , "green" , [green]
nomainwin
open "muzik creator 0.2" for graphics as #m
  #m "trapclose [quit]"
  #m "when leftButtonDown [leftDown]"
  #m "when rightButtonDown [rightDown]"
  #m "when characterInput [key]"
  #m "setfocus"
  call drawmuzik
  timer 100 , [timer]
wait
[timer]
  scan
wait
[quit]
  close #m
end
[new]
  for i = 0 to 40
    l$(i) = "-0"
    r$(i) = "-0"
  next i
  screeny = 0
  screenx = 0
  call drawmuzik
wait
[loadTxt]
''   filedialog "load txt" , ".txt" ; txtfile$
''   open txtfile$ for input as #txtin
''   ''todo read l$() + r$() from file
''   close #txtin
wait
[saveTxt]
''   filedialog "save txt" , ".txt" ; txtfile$
''   open txtfile$ for output as #txtout
''   ''todo write l$() and r$() to file
''   close #txtout
wait
[leftDown]
  x = int( MouseX / 30 )
  y = int( MouseY / 30 ) - 2
  x = int( x / 2 ) * 2
  if y < 0 then wait
  n$ = mid$( "a a#b b#c c#d d#e e#f f#g g#" , x+1 , 2 )
  if chanel = red then
    l$( y ) = n$ ; 3
  else
    r$( y ) = n$ ; 3
  end if
  call drawmuzik
wait
[rightDown]
  x = int( MouseX / 30 ) - 2 + screenx
  y = int( MouseY / 30 ) - 2 + screeny
  if y < 0 then wait
  if chanel = red then
    l$( y ) = "-0"
  else
    r$( y ) = "-0"
  end if
  call drawmuzik
wait
[saveWav]
    for i = 0 to 40
      wl$ = l$( i )
      wr$ = r$( i )
      lo = val( right$( wl$ , 1 ) )
      ln$ = left$( wl$ , len( wl$ ) - 1 )
      ro = val( right$( wr$ , 1 ) )
      rn$ = left$( wr$ , len( wr$ ) - 1 )
      call add lo , ln$ , ro , rn$
    next i
    filedialog "save wav" , "*.wav" , wavfile$
    call savewav wavfile$
wait
[play]
    playwave wavfile$ 
wait
[red]
  chanel = red
wait
[green]
  chanel = green
wait
[key]
  select case right$( InkeyS , 1 )
    case chr$( _VK_UP )
      if screeny > 0 then
        screenyb = sreeny - 1
      end if
    case chr$( _VK_DOWN )
      if screeny < sec * 8 - 14 then
        screeny = screeny + 1
      end if
    case chr$( _VK_LEFT )
    case chr$( _VK_RIGHT )
    case else
  end select
  call drawmuzik
wait
sub drawmuzik
  #m "fill black"
  #m "font 20 bold"
  #m "backcolor blue"
  q$ = "a a#b b#c c#d d#e e#f f#g g#"
  for i = 1 to len( q$ ) step 2
    call drawnote int(i/2) , -1 , mid$( q$ , i , 2 ) , "blue"
  next i
  #m "color blue"
  for i = 0 to 14
    #m "down"
    #m "line ";i*30+60;" 0 ";i*30+60;"  ";14*30+60
    #m "up"
  next i
  for i = 0 to 14
    #m "down"
    #m "line 0 ";i*30+60;" ";14*30+60;" ";i*30+60
    #m "up"
  next i
  #m "color black"
  #m "font 20 bold"
  for i = screeny to screeny + 14
    l$ = left$( l$(i) , len( l$(i) ) - 1 )
    r$ = left$( r$(i) , len( l$(i) ) - 1 )
    ln = ( instr( q$ , l$ ) - 1 ) / 2
    rn = ( instr( q$ , r$ ) - 1 ) / 2
    #m "goto 0 " ; i * 30 + 60 - screeny * 30
    #m "down"
    #m "backcolor blue"
    #m "\" ; nr$( i , 2 )
    #m "up"
    if ln = rn then
      call drawnote ln , i-screeny , l$ , "yellow"
    else
      call drawnote ln , i-screeny , l$ , "red"
      call drawnote rn , i-screeny , r$ , "green"
    end if
  next i
end sub
function nr$( no , m )
  nr$ = right$( "00000000" ; no , m )
end function
sub drawnote x , y , n$ , kl$
  if x < 0 then exit sub
  #m "goto ";x*30+60;" ";y*30+60
  #m "backcolor ";kl$
  #m "down"
  #m "\";n$
  #m "up"
end sub
function hz( okt , note )
  hz = 55 * 2 ^ ( okt + note / 16 )
end function
sub add lokt , lnote$ , rokt , rnote$
  lnote = ( instr( "a a#b b#c c#d d#e e#f f#g g#" , lnote$ ) - 1 ) / 2
  rnote = ( instr( "a a#b b#c c#d d#e e#f f#g g#" , rnote$ ) - 1 ) / 2
  if tel >= bufsize then exit sub
  for i = tel to tel + int( 1/8 * samplerate )
    if lnote < 0 then
      fl(i) = 0
    else
      fl(i) = hz( lokt , lnote )
    end if
    if rnote < 0 then
      fr(i) = 0
    else
      fr(i) = hz( rokt , rnote )
    end if
  next i
  tel = tel + int( 1/8 * samplerate )
end sub
sub savewav file$
    wavh.rLen.struct = len(wavh.struct) + bufsize - 8
    wavh.dLen.struct = bufsize

    open file$ for output as #wav
    print #wav , wavh.struct;
    for i = 1 to bufsize
        sample = sin( i * fl(i) * pi * 2 / samplerate ) * 32767
        low = sample and 255
        high = ( sample and 65280 ) / 256
        print #wav , chr$( low ) ; chr$( high ) ;
        sample = sin( i * fr(i) * pi * 2 / samplerate ) * 32767
        low = sample and 255
        high = ( sample and 65280 ) / 256
        print #wav , chr$( low ) ; chr$( high ) ;
    next i
    close #wav
end sub
 
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1273
xx Re: creating wav's ?
« Reply #10 on: Dec 19th, 2016, 10:40am »

on Dec 19th, 2016, 09:02am, bluatigro wrote:
i dont know whitch of the #'s dont exist
i m not a muzik pro

You can look at a piano keyboard (I know you have vision problems but I assume you are able to see the white and black keys OK). The 12 semitones (starting from A) are:

Code:
A
  A# or B flat
B
C
  C# or D flat
D
  D# or E flat
E
F
  F# or G flat
G
  G# or A flat 

That's 7 white notes and 5 black notes in the octave.

I like to point out to my musical friends that the value 8 doesn't come into it at all, so why is it called an octave? It should be called a septave!

Richard.

User IP Logged

bluatigro
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 111
xx Re: creating wav's ?
« Reply #11 on: Dec 22nd, 2016, 08:45am »

update :
wav muzik maker 12 0.3

error :
mouse not red good
key not red good

Code:
    struct wavh,_
     rID as ulong, _            ' "RIFF"
     rLen as ulong, _           ' file size minus 8
     wID as ulong, _            ' "WAVE"
     fID as ulong, _            ' "fmt "
     fLen as ulong, _           ' 16
     FmtTag as word, _          ' 1
     Channels as word, _        ' 1 = Mono, 2 = Stereo
     SampleRate as ulong, _     ' 11025 = 11kHz, 22050 = 22 kHz, etc.
     AvgBytesPerSec as ulong, _ ' SampleRate*Channels*BitsPerSample/8
     BlockAlign as word,_       ' Channels * BitsPerSample/8
     BitsPerSample as word,_    ' 8, 16, etc.
     dID as ulong,_             ' "data"
     dLen as ulong              ' length in bytes of the sound buffer

    wavh.rID.struct = hexdec("46464952") ' "RIFF"
    wavh.wID.struct = hexdec("45564157") ' "WAVE"
    wavh.fID.struct = hexdec("20746D66") ' "fmt "
    wavh.dID.struct = hexdec("61746164") ' "data"

global samplerate , sec , notestr$
    samplerate = 11025
    sec = 5
    notestr$ = "a a# b c c# d d#e e#f g g#"

    wavh.fLen.struct = 16
    wavh.FmtTag.struct = 1
    wavh.Channels.struct = 2 ' stereo
    wavh.SampleRate.struct = samplerate
    wavh.BitsPerSample.struct = 16
    wavh.AvgBytesPerSec.struct = wavh.SampleRate.struct * _
                                 wavh.Channels.struct * wavh.BitsPerSample.struct/8
    wavh.BlockAlign.struct = wavh.Channels.struct * wavh.BitsPerSample.struct/8

global bufsize
    bufsize = wavh.AvgBytesPerSec.struct * sec
global pi , tel
    pi = atn( 1 ) * 4
    dim fr( bufsize ) , fl( bufsize )
dim l$( sec * 8 ) , r$( sec * 8 )
global wavfile$ , txtfile$
menu #m , "file" _
     , "new" , [new] _
     , "load txt" , [loadTxt] _
     , "save txt" , [saveTxt] _
     , "save wav" , [saveWav] _
     , "play wav" , [play] _
     , "exit" , [quit]
WindowWidth = 1024
WindowHeight = 768
global winx , winy
winx = WindowWidth
winy = WindowHeight
global chanel , red , green , screeny , screenx
red = 0
green = 1
menu #m , "chanel" _
     , "red" , [red] _
     , "green" , [green]
menu #m , "help"_
     , "?" , [help] _
     , "about" , [info]
nomainwin
open "wav muzik maker 12 0.3" for graphics as #m
  #m "trapclose [quit]"
  #m "when leftButtonDown [leftDown]"
  #m "when rightButtonDown [rightDown]"
  #m "when characterInput [key]"
  #m "setfocus"
  call drawmuzik
  timer 100 , [timer]
wait
[timer]
  scan
wait
[quit]
  close #m
end
[new]
  for i = 0 to 40
    l$(i) = "-0"
    r$(i) = "-0"
  next i
  screeny = 0
  screenx = 0
  call drawmuzik
wait
[loadTxt]
''   filedialog "load txt" , ".txt" ; txtfile$
''   open txtfile$ for input as #txtin
''   ''todo read l$() + r$() from file
''   close #txtin
wait
[saveTxt]
''   filedialog "save txt" , ".txt" ; txtfile$
''   open txtfile$ for output as #txtout
''   ''todo write l$() and r$() to file
''   close #txtout
wait
[leftDown]
  x = int( MouseX / 30 ) - 2 + screenx
  y = int( MouseY / 30 ) - 2 + screeny
  x = int( x / 2 ) * 2
  if y < 0 then wait
  n$ = mid$( notestr$ , x+1 , 2 )
  if chanel = red then
    l$( y ) = n$ ; 3
  else
    r$( y ) = n$ ; 3
  end if
  call drawmuzik
wait
[rightDown]
  x = int( MouseX / 30 ) - 2 + screenx
  y = int( MouseY / 30 ) - 2 + screeny
  if y < 0 then wait
  if chanel = red then
    l$( y ) = "-0"
  else
    r$( y ) = "-0"
  end if
  call drawmuzik
wait
[saveWav]
  tel = 0
    for i = 0 to 40
      wl$ = l$( i )
      wr$ = r$( i )
      lo = val( right$( wl$ , 1 ) )
      ln$ = left$( wl$ , len( wl$ ) - 1 )
      ro = val( right$( wr$ , 1 ) )
      rn$ = left$( wr$ , len( wr$ ) - 1 )
      call add lo , ln$ , ro , rn$
    next i
    filedialog "save wav" , "*.wav" , wavfile$
    call savewav wavfile$
wait
[play]
    playwave wavfile$ 
wait
[red]
  chanel = red
wait
[green]
  chanel = green
wait
[help]
  notice chr$(13) _
  + "Welkome by wav muzik maker 0.2 ." + chr$(13) _
  + "Instructions : " + chr$(13) _
  + "use mouse left to slect a note ." + chr$(13) _
  + "use mouse right to unselect a note ." + chr$(13) _
  + "use cursor key's to move selection of muzik ." 
wait
[info]
  notice chr$(13) _
  + "Made by bluatigro ." + chr$(13) _
  + "whit help from rod ."
wait
[key]
  select case right$( InkeyS , 1 )
    case chr$( _VK_UP )
      if screeny > 0 then
        screenyb = sreeny - 1
      end if
    case chr$( _VK_DOWN )
      if screeny < sec * 8 - 14 then
        screeny = screeny + 1
      end if
    case chr$( _VK_LEFT )
    case chr$( _VK_RIGHT )
    case else
  end select
  call drawmuzik
wait
sub drawmuzik
  #m "fill black"
  #m "font 20 bold"
  #m "backcolor cyan"
  for i = 1 to len( notestr$ ) step 2
    call drawnote int(i/2) , -1 , mid$( notestr$ , i , 2 ) , "cyan"
  next i
  #m "color cyan"
  for i = 0 to 14
    #m "down"
    #m "line ";i*30+60;" 0 ";i*30+60;"  ";14*30+60
    #m "up"
  next i
  for i = 0 to 14
    #m "down"
    #m "line 0 ";i*30+60;" ";14*30+60;" ";i*30+60
    #m "up"
  next i
  #m "color black"
  #m "font 20 bold"
  for i = screeny to screeny + 14
    l$ = left$( l$(i) , len( l$(i) ) - 1 )
    r$ = left$( r$(i) , len( l$(i) ) - 1 )
    ln = ( instr( q$ , l$ ) - 1 ) / 2
    rn = ( instr( q$ , r$ ) - 1 ) / 2
    call drawnote 0 , i - screeny , nr$( i , 2 ) , "cyan"
    if ln = rn then
      call drawnote ln , i-screeny , l$ , "yellow"
    else
      call drawnote ln , i-screeny , l$ , "red"
      call drawnote rn , i-screeny , r$ , "green"
    end if
  next i
end sub
function nr$( no , m )
  nr$ = right$( "00000000" ; no , m )
end function
sub drawnote x , y , n$ , kl$
  if x < 0 then exit sub
  #m "goto ";x*30+60;" ";y*30+60
  #m "backcolor ";kl$
  #m "down"
  #m "\";n$
  #m "up"
end sub
function hz( okt , note )
  hz = 55 * 2 ^ ( okt + note / 12 )
end function
sub add lokt , lnote$ , rokt , rnote$
  lnote = ( instr( notestr$ , lnote$ ) - 1 ) / 2
  rnote = ( instr( notestr$ , rnote$ ) - 1 ) / 2
  if tel >= bufsize then exit sub
  for i = tel to tel + int( 1/8 * samplerate )
    if lnote < 0 then
      fl(i) = 0
    else
      fl(i) = hz( lokt , lnote )
    end if
    if rnote < 0 then
      fr(i) = 0
    else
      fr(i) = hz( rokt , rnote )
    end if
  next i
  tel = tel + int( 1/8 * samplerate )
end sub
sub savewav file$
    wavh.rLen.struct = len(wavh.struct) + bufsize - 8
    wavh.dLen.struct = bufsize

    open file$ for output as #wav
    print #wav , wavh.struct;
    for i = 1 to bufsize
        sample = sin( i * fl(i) * pi * 2 / samplerate ) * 32767
        low = sample and 255
        high = ( sample and 65280 ) / 256
        print #wav , chr$( low ) ; chr$( high ) ;
        sample = sin( i * fr(i) * pi * 2 / samplerate ) * 32767
        low = sample and 255
        high = ( sample and 65280 ) / 256
        print #wav , chr$( low ) ; chr$( high ) ;
    next i
    close #wav
end sub
 
User IP Logged

bluatigro
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 111
xx Re: creating wav's ?
« Reply #12 on: Dec 22nd, 2016, 09:54am »

update :
mouse and key's fixed

wav stereo muzik maker 1.0 ready

todo :
[loadTxt]
[saveTxt]
scroling left|right

Code:
    struct wavh,_
     rID as ulong, _            ' "RIFF"
     rLen as ulong, _           ' file size minus 8
     wID as ulong, _            ' "WAVE"
     fID as ulong, _            ' "fmt "
     fLen as ulong, _           ' 16
     FmtTag as word, _          ' 1
     Channels as word, _        ' 1 = Mono, 2 = Stereo
     SampleRate as ulong, _     ' 11025 = 11kHz, 22050 = 22 kHz, etc.
     AvgBytesPerSec as ulong, _ ' SampleRate*Channels*BitsPerSample/8
     BlockAlign as word,_       ' Channels * BitsPerSample/8
     BitsPerSample as word,_    ' 8, 16, etc.
     dID as ulong,_             ' "data"
     dLen as ulong              ' length in bytes of the sound buffer

    wavh.rID.struct = hexdec("46464952") ' "RIFF"
    wavh.wID.struct = hexdec("45564157") ' "WAVE"
    wavh.fID.struct = hexdec("20746D66") ' "fmt "
    wavh.dID.struct = hexdec("61746164") ' "data"

global samplerate , sec , notestr$
    samplerate = 11025
    sec = 5
    notestr$ = "a a#b c c#d d#e e#f g g#"

    wavh.fLen.struct = 16
    wavh.FmtTag.struct = 1
    wavh.Channels.struct = 2 ' stereo
    wavh.SampleRate.struct = samplerate
    wavh.BitsPerSample.struct = 16
    wavh.AvgBytesPerSec.struct = wavh.SampleRate.struct * _
                                 wavh.Channels.struct * wavh.BitsPerSample.struct/8
    wavh.BlockAlign.struct = wavh.Channels.struct * wavh.BitsPerSample.struct/8

global bufsize
    bufsize = wavh.AvgBytesPerSec.struct * sec
global pi , tel
    pi = atn( 1 ) * 4
    dim fr( bufsize ) , fl( bufsize )
dim l$( sec * 8 ) , r$( sec * 8 )
global wavfile$ , txtfile$
menu #m , "file" _
     , "new" , [new] _
     , "load txt" , [loadTxt] _
     , "save txt" , [saveTxt] _
     , "save wav" , [saveWav] _
     , "play wav" , [play] _
     , "exit" , [quit]
WindowWidth = 1024
WindowHeight = 768
global winx , winy
winx = WindowWidth
winy = WindowHeight
global chanel , red , green , screeny , screenx
red = 0
green = 1
menu #m , "chanel" _
     , "red" , [red] _
     , "green" , [green]
menu #m , "help"_
     , "?" , [help] _
     , "about" , [info]
nomainwin
open "wav stereo muzik maker 1.0" for graphics as #m
  #m "trapclose [quit]"
  #m "when leftButtonDown [leftDown]"
  #m "when rightButtonDown [rightDown]"
  #m "when characterInput [key]"
  #m "setfocus"
  call drawmuzik
  timer 100 , [timer]
wait
[timer]
  scan
wait
[quit]
  close #m
end
[new]
  for i = 0 to 40
    l$(i) = "-0"
    r$(i) = "-0"
  next i
  screeny = 0
  screenx = 0
  call drawmuzik
wait
[loadTxt]
''   filedialog "load txt" , ".txt" ; txtfile$
''   open txtfile$ for input as #txtin
''   ''todo read l$() + r$() from file
''   close #txtin
wait
[saveTxt]
''   filedialog "save txt" , ".txt" ; txtfile$
''   open txtfile$ for output as #txtout
''   ''todo write l$() and r$() to file
''   close #txtout
wait
[leftDown]
  x = int( MouseX / 30 ) - 2 + screenx
  y = int( MouseY / 30 ) - 2 + screeny
  if y < 0 then wait
  n$ = mid$( notestr$ , x*2+1 , 2 )
  if chanel = red then
    l$( y ) = n$ ; 3
  else
    r$( y ) = n$ ; 3
  end if
  call drawmuzik
wait
[rightDown]
  x = int( MouseX / 30 ) - 2 + screenx
  y = int( MouseY / 30 ) - 2 + screeny
  if y < 0 then wait
  if chanel = red then
    l$( y ) = "-0"
  else
    r$( y ) = "-0"
  end if
  call drawmuzik
wait
[saveWav]
  tel = 0
    for i = 0 to 40
      wl$ = l$( i )
      wr$ = r$( i )
      lo = val( right$( wl$ , 1 ) )
      ln$ = left$( wl$ , len( wl$ ) - 1 )
      ro = val( right$( wr$ , 1 ) )
      rn$ = left$( wr$ , len( wr$ ) - 1 )
      call add lo , ln$ , ro , rn$
    next i
    filedialog "save wav" , "*.wav" , wavfile$
    call savewav wavfile$
wait
[play]
    filedialog "play wav" , "*.wav" , wavfile$
    playwave wavfile$ 
wait
[red]
  chanel = red
wait
[green]
  chanel = green
wait
[help]
  notice chr$(13) _
  + "Welkome by wav muzik maker 0.2 ." + chr$(13) _
  + "Instructions : " + chr$(13) _
  + "use mouse left to slect a note ." + chr$(13) _
  + "use mouse right to unselect a note ." + chr$(13) _
  + "use cursor key's to move selection of muzik ." 
wait
[info]
  notice chr$(13) _
  + "Made by bluatigro ." + chr$(13) _
  + "whit help from rod ."
wait
[key]
  select case right$( Inkey$ , 1 )
    case chr$( _VK_UP )
      if screeny > 0 then
        screeny = screeny - 1
      end if
    case chr$( _VK_DOWN )
      if screeny < sec * 8 - 14 then
        screeny = screeny + 1
      end if
    case chr$( _VK_LEFT )
    case chr$( _VK_RIGHT )
    case else
  end select
  call drawmuzik
wait
sub drawmuzik
  #m "fill black"
  #m "font 20 bold"
  for i = 1 to 24 step 2
    call drawnote int(i/2) , -1 _
    , mid$( notestr$ , i , 2 ) _
    , "blue" , "white"
  next i
  #m "color blue"
  for i = 0 to 14
    #m "down"
    #m "line ";i*30+60;" 0 ";i*30+60;"  ";14*30+60
    #m "up"
  next i
  for i = 0 to 14
    #m "down"
    #m "line 0 ";i*30+60;" ";14*30+60;" ";i*30+60
    #m "up"
  next i
  for i = screeny to screeny + 14
    l$ = left$( l$(i) , len( l$(i) ) - 1 )
    r$ = left$( r$(i) , len( r$(i) ) - 1 )
    ln = ( instr( notestr$ , l$ ) - 1 ) / 2
    rn = ( instr( notestr$ , r$ ) - 1 ) / 2
    call drawnote -2 , i - screeny _
    , nr$( i , 2 ) , "blue" , "white"
    if ln = rn then
      call drawnote ln , i-screeny _
      , l$ , "yellow" , "black"
    else
      call drawnote ln , i-screeny _
      , l$ , "red" , "white"
      call drawnote rn , i-screeny _
      , r$ , "green" , "white"
    end if
  next i
end sub
function nr$( no , m )
  nr$ = right$( "00000000" ; no , m )
end function
sub drawnote x , y , n$ , kl$ , kl2$
  #m "goto ";x*30+60;" ";y*30+60
  #m "color ";kl2$
  #m "backcolor ";kl$
  #m "down"
  #m "\";n$
  #m "up"
end sub
function hz( okt , note )
  hz = 55 * 2 ^ ( okt + note / 12 )
end function
sub add lokt , lnote$ , rokt , rnote$
  lnote = ( instr( notestr$ , lnote$ ) - 1 ) / 2
  rnote = ( instr( notestr$ , rnote$ ) - 1 ) / 2
  if tel >= bufsize then exit sub
  for i = tel to tel + int( 1/8 * samplerate )
    if lnote < 0 then
      fl(i) = 0
    else
      fl(i) = hz( lokt , lnote )
    end if
    if rnote < 0 then
      fr(i) = 0
    else
      fr(i) = hz( rokt , rnote )
    end if
  next i
  tel = tel + int( 1/8 * samplerate )
end sub
sub savewav file$
    wavh.rLen.struct = len(wavh.struct) + bufsize - 8
    wavh.dLen.struct = bufsize

    open file$ for output as #wav
    print #wav , wavh.struct;
    for i = 1 to bufsize
        sample = sin( i * fl(i) * pi * 2 / samplerate ) * 32767
        low = sample and 255
        high = ( sample and 65280 ) / 256
        print #wav , chr$( low ) ; chr$( high ) ;
        sample = sin( i * fr(i) * pi * 2 / samplerate ) * 32767
        low = sample and 255
        high = ( sample and 65280 ) / 256
        print #wav , chr$( low ) ; chr$( high ) ;
    next i
    close #wav
end sub

 
User IP Logged

pnlawrence
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 157
xx Re: creating wav's ?
« Reply #13 on: Dec 26th, 2016, 4:59pm »

I'm almost sure that you have "tongue firmly in cheek", but since I am not even a music minor I risked asking this question to one over the holidays. Summarizing the answer produces this.

Quote:
There are eight notes in a normal scale. Eight scale notes are one octave apart. There wasn't a piano when they started writing music, so the number of keys is irrelevant.


So, if you were kidding, does this sound right or was I bamboozled by a youngster ... again? :D

PS. This is why you, Richard, are important. Your place is to de-bamboozle BASIC for the rest of us.


on Dec 19th, 2016, 10:40am, Richard Russell wrote:
You can look at a piano keyboard (I know you have vision problems but I assume you are able to see the white and black keys OK). The 12 semitones (starting from A) are:

Code:
A
  A# or B flat
B
C
  C# or D flat
D
  D# or E flat
E
F
  F# or G flat
G
  G# or A flat 

That's 7 white notes and 5 black notes in the octave.

I like to point out to my musical friends that the value 8 doesn't come into it at all, so why is it called an octave? It should be called a septave!

Richard.

User IP Logged

Paul " Tex " Lawrence LXVTruth lies dormant in our future history.
Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1273
xx Re: creating wav's ?
« Reply #14 on: Dec 26th, 2016, 9:55pm »

on Dec 26th, 2016, 4:59pm, pnlawrence wrote:
Summarizing the answer produces this: "There are eight notes in a normal scale. Eight scale notes are one octave apart".

There are seven (not eight) 'notes' in an octave: A, B C, D, E, F and G. If anybody claims there are eight, ask them how many notes there are in two octaves! Or three octaves. That tends to make them think. cheesy

The correct answer is of course that there are 7 notes in an octave, 14 in two octaves and 21 in three octaves. Count them yourself.

Another question you can try asking is: if there are 8 notes in an octave, how many semitones are there? To be consistent they would have to answer 13 (the correct answer is of course 12).

I would personally argue that a 'good' musician also needs to be something of a mathematician. They should understand that the equally-tempered scale is logarithmic, that is the ratio between the frequencies of any two consecutive semitones is the same (it has the value 21/12), hence 12 of those steps constitutes an octave.

The value 8 doesn't come into it anywhere.

Richard.
User IP Logged

Pages: 1 2  Notify Send Topic Print
« Previous Topic | Next Topic »

Donate $6.99 for 50,000 Ad-Free Pageviews!


This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls