Ooops
October 29, 2020, 02:25:07 AM

Author Topic: [bb] Streaming sounds by King Dave [ 1+ years ago ]  (Read 1189 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Streaming sounds by King Dave [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : Streaming sounds
Author : King Dave
Posted : 1+ years ago

Description : Small example to show how you can stream sounds (ie. play before the entire file is available, eg. while it is downloading from a website)

Uses a file on your hard disk and slowly transfers it to a tempory file while playing

Tested with .wav .mp3 and .ogg all worked with no problems

Only problem is, if PlaySound reaches somewhere before its been copied over, the music will stop and can't be resumed from that point


Code :
Code: BlitzBasic
  1. ;Title:         Streaming sounds
  2. ;Version:       1.00
  3.  
  4. ;Author:        David Blasdell (aka. King Dave)
  5. ;       Email:  admin@netcharger.co.uk
  6. ;       Web:    www.netcharger.co.uk or www.freeworldonline.co.uk
  7.  
  8. ;Description:
  9. ;       Example source code to demonstrate how to stream sound files using PlayMusic(filename$)
  10. ;       (ie. playing the file before the entire file is downloaded)
  11. ;       Transfers a file from somewhere on your hard disk to a tempory file, while it is playing
  12. ;       Could be used to play music while it downloads off of a website
  13.  
  14. ;Notes:
  15. ;       If you enter too lower bytes per second value, the music may stop playing before the whole file is transfered
  16.  
  17.  
  18. Graphics 640,480,16,2                   ;Set graphics to 640x480x16 window mode
  19. SetBuffer BackBuffer()                  ;Set buffer to back buffer
  20. SetFont LoadFont("Arial",14)    ;Use Arial fsize 14 as the font
  21.  
  22. filename$=Input$("Enter location of the file to be streamed: ")                         ;Get file to use (most things that can be played by PlayMusic() should work)
  23. bps=Int(Input$("Enter how many bytes to copy per second (eg. 10000): "))/4      ;Will transfer this number of bytes to the stream every second
  24.  
  25. file_in=ReadFile(filename$)                                                                                                     ;Read input file
  26. If Not file_in Then RuntimeError "Failed to read from '"+filename$+"'!"         ;If failed to read from file, say so
  27. fsize=FileSize(filename$)                                                                                                       ;Retrieve total fsize of file
  28. bytes=fsize                                                                                                                                     ;Used to count down bytes left to read
  29.  
  30. ;Retrieves file extension
  31. For a=Len(filename$) To 1 Step -1
  32.         If Mid$(filename$,a,1)="." Then ext$=Right$(filename$,Len(filename$)-a):Exit
  33. Next
  34.  
  35. temp_file$=SystemProperty("tempdir")+"stream."+ext$                                                     ;Create a tempory filename to use
  36. file_out=WriteFile(temp_file$)                                                                                          ;Write to a tempory file
  37. If Not file_out Then RuntimeError "Failed to write to '"+temp_file$+"'!"        ;If failed to write to file, say so
  38.  
  39. bank=CreateBank(bps*8)                          ;Create a bank big enough for 2xbps worth of bytes
  40.  
  41. ReadBytes(bank,file_in,0,bps*8)         ;Read bytes from input stream
  42. bytes=bytes-(bps*8)                                     ;Reduce byte count
  43. WriteBytes(bank,file_out,0,bps*8)       ;Write bytes to output stream (need a fair amount or bytes ready before starting PlayMusic(), this sends 2 seconds worth)
  44.  
  45. ResizeBank(bank,bps)                            ;Reduce bank fsize to 1/4 of the bytes per second speed
  46.  
  47. music=PlayMusic(temp_file$)                     ;Start playing the stream
  48.  
  49. While ChannelPlaying(music)=1                           ;Main loop, repeats while music is still playing
  50.         If KeyHit(1) Then End                                                                           ;End the program if escape is hit
  51.         If stream_delay<MilliSecs() Then If Eof(file_in)=0                      ;Every 250 milliseconds more data will be written to the output file
  52.                 If bytes<bps Then a=bytes Else a=bps                                                            ;Checks how much can be read   
  53.                 ReadBytes(bank,file_in,0,a)                                                                                     ;Reads bytes from the input file
  54.                 WriteBytes(bank,file_out,0,a)                                                                           ;Writes bytes to the output file
  55.                 bytes=bytes-a                                                                                                           ;Subtracts amount moved from byte counter
  56.                 stream_delay=MilliSecs()+250                                                                            ;Waits for 250 milliseconds
  57.                 p#=100-((Float#(bytes)/Float#(fsize))*100)                                                      ;Calculates percentage of file sent
  58.                 DrawProgress("Streaming... ("+(fsize-bytes)+" of "+fsize+")",p#)        ;Update screen
  59.         Else
  60.                 If Not done Then DrawProgress("File fully transfered",100):done=1       ;Displays once the file has been fully transfered for playing
  61.         EndIf
  62. Wend
  63.  
  64. StopChannel music               ;Stop music if still playing
  65.  
  66. CloseFile file_in               ;Close input file
  67. CloseFile file_out              ;Close output file
  68. DeleteFile temp_file$   ;Delete the tempory file
  69.  
  70. ;Works out wether the sound was fully played or not
  71. Cls
  72. Color 255,255,255
  73. If bytes=0
  74.         Text 5,5,"Entire file was streamed succesfully"
  75. Else
  76.         Text 5,5,"Only some of the file was streamed, try giving a higher bytes per second value"
  77. EndIf
  78. Flip
  79.  
  80. FlushKeys()
  81. WaitKey
  82.  
  83. End
  84.  
  85. ;Draw a progress with description and percentage
  86. Function DrawProgress(t$,p#)
  87.         w=GraphicsWidth()
  88.         Cls
  89.         Color 255,255,255
  90.         Text 2,2,t$
  91.         Color 255,0,0
  92.         Rect 9,20,w-18,16,0
  93.         Color 0,150,0
  94.         Rect 10,21,(p#/100)*(w-20),14,1
  95.         Color 0,0,255
  96.         Text w/2,28,Int(Floor#(p#))+"%",1,1
  97.         Flip 0
  98. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal