Differenze tra le versioni di "Fondere insieme due file WAV"

Da Gambas-it.org - Wikipedia.
Riga 9: Riga 9:
 
  '''Public''' Sub Main()
 
  '''Public''' Sub Main()
 
   
 
   
   Dim bb, bb1, bb2 As Byte[]
+
   Dim file_grande, file_piccolo As String
   Dim dati_finali As New Byte[]
+
   Dim fl As File
   Dim k As Byte
+
   Dim ss1, ss2 As Short[]
 
   Dim i As Integer
 
   Dim i As Integer
   Dim caput, file_grande, file_piccolo As String
+
   Dim sh As Short
 +
  Dim he As Byte[]
 
   
 
   
 
   
 
   
Riga 20: Riga 21:
 
   file_piccolo = "''/percorso/del/file.wav/più/piccolo/fra/i/due''"
 
   file_piccolo = "''/percorso/del/file.wav/più/piccolo/fra/i/due''"
 
    
 
    
   bb = Byte[].FromString(File.Load(file_grande))
+
   For i = 4 To 7
+
   fl = Open file_grande For Read
     bb[i] = 0
+
 
 +
  ss1 = New Short[(Stat(file_grande).Size - 44)]
 +
  Seek #fl, 44
 +
  ss1.Read(fl, 0, ss1.Count / 2)
 +
 
 +
  fl.Close
 +
 
 +
 
 +
  fl = Open file_piccolo For Read
 +
 
 +
  ss2 = New Short[(Stat(file_piccolo).Size - 44)]
 +
  Seek #fl, 44
 +
  ss2.Read(fl, 0, ss2.Count / 2)
 +
 
 +
  fl.Close
 +
 
 +
 
 +
   For i = 0 To ss1.Count / 2
 +
 +
<FONT color=gray>' ''Somma le due onde:''</font>
 +
     ss1[i] = CShort((CInt(ss1[i] + ss2[i])) / 2)
 +
   
 
   Next
 
   Next
  For i = 40 To 43
+
    bb[i] = 0
 
  Next
 
  bb.Extract(44, bb.Count - 44)
 
 
   
 
   
 
  <FONT color=gray>' ''Creiamo il blocco di intestazione del futuro file wav:''</font>
 
  <FONT color=gray>' ''Creiamo il blocco di intestazione del futuro file wav:''</font>
   caput = bb.ToString(0, bb.Count)
+
   he = CreaBloccoIntestazione(ss1.Count)
 
   
 
   
 +
  fl = Open "/tmp/nuovo.wav" For Create
 
   
 
   
  <FONT color=gray>' ''Prendiamo i soli dati audio grezzi del file wav di dimensione più grande fra i due:''</font>
+
  <FONT color=gray>' ''Crea il nuovo file wav:''</font>
   bb1 = Byte[].FromString(Right(File.Load(file_grande), Len(File.Load(file_grande)) - 44))
+
   he.Write(fl, 0, he.Count)
 +
  fl.Close
 
   
 
   
<FONT color=gray>' ''Prendiamo i soli dati audio grezzi del file wav di dimensione inferiore fra i due:''</font>
+
  fl = Open "/tmp/nuovo.wav" For Write Append
  bb2 = Byte[].FromString(Right(File.Load(file_piccolo), Len(File.Load(file_piccolo)) - 44))
 
 
   
 
   
 +
<FONT color=gray>' ''Crea il nuovo file wav:''</font>
 +
  ss1.Write(fl, Stat("/tmp/nuovo.wav").Size, ss1.Count / 2)
 
   
 
   
  For i = 0 To bb1.Max
 
 
   
 
   
    If i < bb1.Max Then
+
<FONT color=gray>' ''Va in chiusura:''</font>
     
+
  fl.Close
      If i <= bb2.Max Then
+
  ss1.Clear
        dati_finali.Add(bb1[i] + bb2[i])
+
  ss2.Clear
      Else
+
        dati_finali.Add(bb1[i])
+
'''End'''
      Endif
+
     
+
    Endif
+
'''Private''' Function CreaBloccoIntestazione(dati_grezzi As Integer) as Byte[]
   
+
  Next
+
Dim ini As String = "RIFF"
 +
Dim bh As Byte[] = [&57, &41, &56, &45, &66, &6D, &74, &20, &10, &00, &00, &00, &01, &00, &02, &00,
 +
                    &44, &AC, &00, &00, &10, &B1, &02, &00, &04, &00, &10, &00, &64, &61, &74, &61]
 +
Dim bb As New Byte[]
 +
Dim i, i2 As Integer
 +
 +
 +
  bb = Byte[].FromString(ini)
 +
 +
  i2 = dati_grezzi + 36
 +
 +
<FONT color=gray>' ''Imposta il valore dimensionale di 4 byte a partire dal 5° byte del futuro file: ''</font>
 +
  bb.Add(i2 And &FF)
 +
  bb.Add(Shr(i2 And &FF00&, 8))
 +
  bb.Add(Shr(i2 And &FF0000&, 16))
 +
  bb.Add(Shr(i2 And &FF000000&, 24))
 
   
 
   
 
   
 
   
<FONT color=gray>' ''Crea il nuovo file wav con i due file originari fusi insieme:''</font>
+
  bb = bb.Insert(bh)
  File.Save("/tmp/nuovo.wav", caput & dati_finali.ToString(0, dati_finali.Count))
 
 
   
 
   
 +
<FONT color=gray>' ''Imposta il valore dimensionale di 4 byte a partire dal 41° byte del futuro file''
 +
' ''e relativo alla dimensione dei dati audio grezzi:''</font>
 +
  bb.Add(dati_grezzi And &FF)
 +
  bb.Add(Shr(dati_grezzi And &FF00&, 8))
 +
  bb.Add(Shr(dati_grezzi And &FF0000&, 16))
 +
  bb.Add(Shr(dati_grezzi And &FF000000&, 24))
 
   
 
   
<FONT color=gray>' ''Va in chiusura:''</font>
+
  Return bb
  bb.Clear
 
  bb1.Clear
 
  bb2.Clear
 
  dati_finali.Clear
 
 
   
 
   
 
  '''End'''
 
  '''End'''

Versione delle 17:01, 29 gen 2014

Per fondere (mischiare) due file audio di formato WAV con le sole funzioni di Gambas, bisognerà prestare cura ad almeno i seguenti aspetti:

  • i due file da fondere devono avere uguale frequenza e risoluzione di campionamento ed uguale numero di canali;
  • eliminare l'intero primo blocco di intestazione al file avente dimensione inferiore;
  • azzerare oppure impostare i nuovi corretti valori che indicano le dimensioni del file wav, presenti nei byte n. 4, 5, 6, 7 e nei byte n. 40, 41, 42, 43 dell'header del file wav finale.


Di seguito un possibile semplice codice supponendo che i due file wav siano a 2 canali e a 16bit:

Public Sub Main()

 Dim file_grande, file_piccolo As String
 Dim fl As File
 Dim ss1, ss2 As Short[]
 Dim i As Integer
 Dim sh As Short
 Dim he As Byte[]


  file_grande = "/percorso/del/file.wav/più/grande/fra/i/due"

  file_piccolo = "/percorso/del/file.wav/più/piccolo/fra/i/due"
 

  fl = Open file_grande For Read
 
  ss1 = New Short[(Stat(file_grande).Size - 44)]
  Seek #fl, 44
  ss1.Read(fl, 0, ss1.Count / 2)
 
  fl.Close
 
 
  fl = Open file_piccolo For Read
 
  ss2 = New Short[(Stat(file_piccolo).Size - 44)]
  Seek #fl, 44
  ss2.Read(fl, 0, ss2.Count / 2)
 
  fl.Close
 
 
  For i = 0 To ss1.Count / 2

' Somma le due onde:
    ss1[i] = CShort((CInt(ss1[i] + ss2[i])) / 2)
   
  Next


' Creiamo il blocco di intestazione del futuro file wav:
  he = CreaBloccoIntestazione(ss1.Count)

  fl = Open "/tmp/nuovo.wav" For Create

' Crea il nuovo file wav:
  he.Write(fl, 0, he.Count)
  fl.Close

  fl = Open "/tmp/nuovo.wav" For Write Append

' Crea il nuovo file wav:
  ss1.Write(fl, Stat("/tmp/nuovo.wav").Size, ss1.Count / 2)


' Va in chiusura:
  fl.Close
  ss1.Clear
  ss2.Clear

End


Private Function CreaBloccoIntestazione(dati_grezzi As Integer) as Byte[]

Dim ini As String = "RIFF"
Dim bh As Byte[] = [&57, &41, &56, &45, &66, &6D, &74, &20, &10, &00, &00, &00, &01, &00, &02, &00,
                    &44, &AC, &00, &00, &10, &B1, &02, &00, &04, &00, &10, &00, &64, &61, &74, &61]
Dim bb As New Byte[]
Dim i, i2 As Integer


 bb = Byte[].FromString(ini)

 i2 = dati_grezzi + 36

' Imposta il valore dimensionale di 4 byte a partire dal 5° byte del futuro file: 
 bb.Add(i2 And &FF)
 bb.Add(Shr(i2 And &FF00&, 8))
 bb.Add(Shr(i2 And &FF0000&, 16))
 bb.Add(Shr(i2 And &FF000000&, 24))


 bb = bb.Insert(bh)

' Imposta il valore dimensionale di 4 byte a partire dal 41° byte del futuro file
' e relativo alla dimensione dei dati audio grezzi:
 bb.Add(dati_grezzi And &FF)
 bb.Add(Shr(dati_grezzi And &FF00&, 8))
 bb.Add(Shr(dati_grezzi And &FF0000&, 16))
 bb.Add(Shr(dati_grezzi And &FF000000&, 24))

 Return bb

End