Eine Bin-Datei einlesen und dann in eine Wav umwandeln von 44100hz und die Flanke hat 1000Baud , die dem Tape vom CPC464 entspricht.
Funktioniert schon halbswegs, kommt aber noch ein "Read_Error b" bei der Checksumme.
Obwohl die Checksumme jetzt stimmt.
Code: Alles auswählen
SetCurrentDirectory(GetPathPart(ProgramFilename()))
Global *tonspeicher.l,*datspeicher.c,tonlaenge.l,datlaenge.l,zaehler.l
Global byte_bits.c,secs.f,i.l,h.l,pegel_0.l,pegel_1.l,bytespersec.l
Global block.l,block_header.l,block_data.l,block_sync.l,block_vor_nach.l
Global samplerate.l,bitrate.l,channels.l,volume.l,n_samples.l,n_dat.l,text1.l,text2.l
Global n_anf.l,dat_seg.l
Global seed_crc.u,mask_crc.u,crc_tmp.u,i.l,j.l
Global Dim head_byte.c(65)
Declare erstelle_wav()
Declare lade_datei()
Declare bit_0()
Declare bit_1()
Declare w_byte()
Declare pegel_0()
Declare checken()
Macro HiByte(word)
(word >> 8) & $FF
EndMacro
Macro Lobyte(word)
word & $FF
EndMacro
Procedure pegel_0()
For h=0 To pegel_0
PokeW(*tonspeicher+n_samples,-volume)
n_samples + 2
Next
EndProcedure
Procedure bit_0()
For h=0 To pegel_0
PokeW(*tonspeicher+n_samples,-volume)
n_samples + 2
Next
For h=0 To pegel_0
PokeW(*tonspeicher+n_samples,volume)
n_samples + 2
Next
EndProcedure
Procedure bit_1()
For h=0 To pegel_1
PokeW(*tonspeicher+n_samples,-volume)
n_samples + 2
Next
For h=0 To pegel_1
PokeW(*tonspeicher+n_samples,volume)
n_samples + 2
Next
EndProcedure
Procedure w_byte()
For i=7 To 0 Step -1
If (byte_bits & (1<<i))
bit_1()
Else
bit_0()
EndIf
Next i
EndProcedure
Procedure checken()
seed_crc=$ffff
mask_crc=$1021
crc_tmp=seed_crc
For i=0 To 255
crc_tmp=crc_tmp ! (PeekB(*datspeicher+i)*256)
For j=0 To 7
If crc_tmp & $8000
crc_tmp=(crc_tmp *2) ! mask_crc
Else
crc_tmp=crc_tmp*2
EndIf
Next
Next
crc_tmp=(crc_tmp & $ffff) ! $ffff
Debug (crc_tmp & $0000ffff)
Debug $77A1
EndProcedure
Procedure lade_datei()
If ReadFile(0, "cpc.bin")
datlaenge = Lof(0)
dat_seg=Round(datlaenge/256,#PB_Round_Up)
If dat_seg
*datspeicher = AllocateMemory(256*dat_seg)
ReadData(0, *datspeicher, datlaenge)
Else
CloseFile(0)
ProcedureReturn #False
EndIf
CloseFile(0)
channels = 1
bitrate = 16
samplerate = 44100
volume = 32760
bytespersec = channels*bitrate /8*samplerate
secs = 10
block=Round((datlaenge/2048), #PB_Round_Up)
block_vor_nach=512*block
block_sync=257*block
block_header=65*block
block_data=(2048+16)*block
tonlaenge = (block_sync+block_header+block_data+block_vor_nach)*34*8+44
*tonspeicher = AllocateMemory(tonlaenge )
pegel_1=30
pegel_0=15
EndIf
EndProcedure
Procedure erstelle_wav()
PokeS(*tonspeicher,"RIFF")
PokeL(*tonspeicher+4,36+bytespersec*secs)
PokeS(*tonspeicher+8,"WAVE")
PokeS(*tonspeicher+12,"fmt ")
PokeL(*tonspeicher+16,16)
PokeW(*tonspeicher+20,1)
PokeW(*tonspeicher+22,channels)
PokeL(*tonspeicher+24,samplerate )
PokeL(*tonspeicher+28,bytespersec)
PokeW(*tonspeicher+32,bitrate/8*channels)
PokeW(*tonspeicher+34,bitrate)
PokeS(*tonspeicher+36,"data")
PokeL(*tonspeicher+40,bytespersec*secs)
n_samples=44
;datenübertragung
For n_anf=0 To 4000; vorspann "0"-Pegel
pegel_0()
Next
For n_anf=0 To 2048-1 ; systemvorspann mit 2048x "1"
bit_1()
Next
bit_0()
byte_bits=44 ; sync-headeranfang
w_byte()
head_byte(0)=Asc("a")
head_byte(1)=Asc("1")
head_byte(2)=0
head_byte(3)=0
head_byte(4)=0
head_byte(5)=0
head_byte(6)=0
head_byte(7)=0
head_byte(8)=0
head_byte(9)=0
head_byte(10)=0
head_byte(11)=0
head_byte(12)=0
head_byte(13)=0
head_byte(14)=0
head_byte(15)=0
head_byte(16)=1
head_byte(17)=255
head_byte(18)=2
head_byte(19)=lobyte(datleange)
head_byte(20)=hibyte(datleange)
head_byte(21)=0
head_byte(22)=$40
head_byte(23)=255
head_byte(24)=lobyte(datleange)
head_byte(25)=hibyte(datleange)
head_byte(26)=0
head_byte(27)=0
For n_anf=28 To 63
head_byte(n_anf)=0
Next
For n_anf=0 To 63
byte_bits=head_byte(n_anf)
w_byte()
Next
byte_bits=22 ; sync-datenanfang
w_byte()
; block mit 2048 byte (8 sectoren zu 256 byte + 2 checkbyte)
For n_dat=0 To 256-1
byte_bits=PeekB(*datspeicher+n_dat)
w_byte()
Next
checken()
byte_bits=lobyte(crc_tmp) ; 2x checkbyte
w_byte()
byte_bits=hibyte(crc_tmp)
w_byte()
For n_anf=0 To 32-1 ; nachspann mit 32x "1"
bit_1()
Next
For n_anf=0 To 4000; nachspann "0"-Pegel
pegel_0()
Next
; ende datenübertragung
CreateFile(0, "wavtest.wav")
WriteData(0,*tonspeicher,tonlaenge)
CloseFile(0)
EndProcedure
InitSprite()
InitSound()
OpenWindow(0,0,0,600,400,"sound-test",#PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),220,10,300,300,0,0,0)
text1=TextGadget(#PB_Any, 10,20,120,20, "E-Erstellen, S-Spielen")
text2=TextGadget(#PB_Any, 10,40,180,20, "")
ClearScreen(RGB(128,128,128))
Repeat
Event.l = WaitWindowEvent(2)
If GetAsyncKeyState_(#VK_E)
lade_datei()
erstelle_wav()
SetGadgetText(text2,"Datenlänge= "+Str(datlaenge)+ " Tonlänge= "+ Str(tonlaenge))
EndIf
If GetAsyncKeyState_(#VK_S)
SetGadgetText(text2,"spielen")
EndIf
Until Event = #PB_Event_CloseWindow
End