TBankStream

Started by Baggey, August 18, 2023, 07:45:39

Previous topic - Next topic

Baggey

Im trying to get my head around TBankStream

I have some code here to create a Stream and Write to it. Im using Byte Ptr for faster access and it also relates to my Emulated Memory Bank in my Emulators.

The thing is when you try to write something else it Sort of repeats its self.

Is A TBankStream only sequential ie, Can i not alter the Stream.Pos to what ever i like?

Im also finding if i CloseStream i cant access it? There must be away of reopening the same Stream?

Anyway here is the code im using to try and open a TBankStream and point to it for read and writing as i need to.

SuperStrict

Global Bank:TBank = CreateBank(1)  'This bank will resize itself with stream
Global BankPtr:Byte Ptr

Global Stream:TBankStream = CreateBankStream(Bank)

WriteString(Stream, " Hello World")

'CloseOurStream(stream, BankPtr, Bank)

BankPtr = bank.buf()
Bankptr = LockBank(bank)

For Local i:Int = 0 Until BankSize(bank)
  Print Chr(bankPtr[i])
'Print (PeekByte(bank , i) )
Next

'Stream.StreamPos=0

WriteString(Stream, " GoodBye World")

BankPtr = bank.buf()
Bankptr = LockBank(bank)

''CloseOurStream(stream, BankPtr, Bank)

For Local i:Int = 0 Until BankSize(bank)
  Print Chr(bankPtr[i])
'Print (PeekByte(bank , i) )
Next



Function CloseOurStream(stream:TBankStream, BankPtr:Byte Ptr, bank:TBank)
     
      ' Basically closes our Stream and Appends our BankPtr to the correct start address
      ' As the WriteString(Stream, "........") will resize our Bank for us Atomatically!
      CloseStream(stream)
      BankPtr = bank.buf()
      Bankptr = LockBank(bank)

End Function

Kind Regards Baggey
Running a PC that just Aint fast enough!? i7 4Ghz Quad core 32GB ram  2x1TB SSD and NVIDIA Quadro K1200 on Acer 24" . DID Technology stop! Or have we been assimulated!

Windows10, Parrot OS, Raspberry Pi Black Edition! , ZX Spectrum 48k, C64, Enterprise 128K, The SID chip. Im Misunderstood!

Midimaster

Be careful with the combination of TBank and Pointers!

When you created a pointer with LockBank() the bank will not auto-resize in future WriteString()-Streams. Before using WriteString() again you have to Unlock() the Bank
...back from North Pole.

Baggey

Ive played around with TBank, TStream, Byte Ptr in order to better understand how to manipulate and make a file.

The created File is a .txt. This however could be anything, even your own format or formats that exist to today.

I did this so i could better understand how to use BlitzMaxNG so i could write files from a TBank using TStream

if you find this useful then use it. If you dont ignore it.

Here is some runable code in BlitmazNG. Warining it writes a data.txt file! Comment it out if you dont want to.


'
' The code below messes with TBanks, Tstreams and Pointers
' with this we can create any type of file from our TBank
' if you find it useful use it if you dont ignore it! Baggey
'

SuperStrict

Global WaveBank:TBank = CreateBank(1)  'This bank will resize itself with stream
Global WavePtr:Byte Ptr

Global Stream:TStream = CreateBankStream(WaveBank)
'Global StreamPtr:Byte Ptr

'OpenStream stream

' Note, Every time a stream is used we must update the BankPtr
Stream.WriteString(" Hello World")
Waveptr = LockBank(Wavebank)

' Will write "G" to WaveBank Position "0"
WavePtr[0]=Asc("G")

Print "Bank size"+(BankSize(WaveBank)+1)
Print "Stream pos"+stream.pos()

' Sets the position of the next stream write to 6
' Only if if pos 6 exists!
SeekStream(stream,6)
WriteByte(stream, Asc"&")


Print "Bank size"+BankSize(WaveBank) ' At this point you get 12


' To write further into the Bank that dosent exist yet! We must Resize it!
' Now let's Resize Bank to another 20 bytes on!
' First we must Unlock it to Resize it
UnlockBank(Wavebank)
Local NewSize:Size_T=BankSize(WaveBank)+20
ResizeBank(WaveBank,(NewSize))
WavePtr = LockBank(Wavebank)


Print "Bank REsized"+BankSize(WaveBank) ' At this point you get 32


' Because we Resized it there will be garbage here so we change it to hold Spaces
' So i wanted to start the stream at new position 20
' Someone might ask why not just write 8 spaces!?
' Im messing with the code to see what can be done and for understanding
For Local i:Int=12 Until 20
       WavePtr[i]=Asc(" ")
Next i

SeekStream(stream,20)

WriteString(Stream, "Goodbye World")
Waveptr = LockBank(Wavebank)


Print "Bank Ready"


For Local i:Int = 0 Until BankSize(Wavebank)
  Print Chr(WavePtr[i])
    'Print (PeekByte(Wavebank , i) )
Next

' Finally Save and make a File
WaveBank.Save("data.txt") ' This could be .txt   .p   .sna   .z80   .tzx anything even your own type is possible
' if you use other types of file that exist you may need the header ie, Such as the wav header below which midimaster provided.
' otherwise other programs wont regonise it!
'
' I suppose there are formats for .bmp  .png etc...

' Finally close the stream not sure why this is done?
CloseStream(stream)


Rem

' .wav Header Info

Stream.WriteString "RIFF"               ' "RIFF" file description header (4 bytes)
Stream.WriteInt    (sampleSize + 40)    ' file size - 8 (4 bytes)
Stream.WriteString "WAVE"               ' "WAVE" description header (4 bytes)
Stream.WriteString "fmt "               ' "fmt " description header (4 bytes)
Stream.WriteInt    16                   ' size of WAVE section chunk (4 bytes)
Stream.WriteShort  1                    ' WAVE type format (2 bytes)
Stream.WriteShort  1                    ' mono/stereo (2 bytes)
Stream.WriteInt    23336                ' sample rate (4 bytes)
Stream.WriteInt    23336                ' avg bytes/sec (4 bytes)
Stream.WriteShort  1                    ' Block alignment (2 bytes)
Stream.WriteShort  8                    ' Bits/sample (2 bytes)
Stream.WriteString "data"               ' "data" description header (4 bytes)
Stream.WriteInt    sampleSize           ' size of data chunk (4 bytes)

end rem

Kind Regards Baggey
Running a PC that just Aint fast enough!? i7 4Ghz Quad core 32GB ram  2x1TB SSD and NVIDIA Quadro K1200 on Acer 24" . DID Technology stop! Or have we been assimulated!

Windows10, Parrot OS, Raspberry Pi Black Edition! , ZX Spectrum 48k, C64, Enterprise 128K, The SID chip. Im Misunderstood!