Fondere insieme due file WAV: differenze tra le versioni
Vai alla navigazione
Vai alla ricerca
Nessun oggetto della modifica |
Nessun oggetto della modifica |
||
| Riga 15: | Riga 15: | ||
Dim he As Byte[] | Dim he As Byte[] | ||
fileWav_1 = "<FONT Color=gray>''/percorso/del/primo/file.wav''</font>" | |||
fileWAV_2 = "<FONT Color=gray>''/percorso/del/secondo/file.wav''</font>" | |||
fl = Open fileWAV_1 For Read | |||
ss1 = New Short[(Stat(fileWAV_1).Size - 44)] | ss1 = New Short[(Stat(fileWAV_1).Size - 44)] | ||
Seek #fl, 44 | Seek #fl, 44 | ||
ss1.Read(fl, 0, ss1.Count / 2) | ss1.Read(fl, 0, ss1.Count / 2) | ||
fl.Close | |||
fl = Open fileWAV_2 For Read | |||
ss2 = New Short[(Stat(fileWAV_2).Size - 44)] | ss2 = New Short[(Stat(fileWAV_2).Size - 44)] | ||
Seek #fl, 44 | Seek #fl, 44 | ||
ss2.Read(fl, 0, ss2.Count / 2) | ss2.Read(fl, 0, ss2.Count / 2) | ||
fl.Close | |||
ss3 = New Short[Max(ss1.Count, ss2.Count)] | |||
For i = 0 To (Max(ss1.Count, ss2.Count) / 2) - 1 | |||
<FONT color=gray>' ''Somma le due onde:''</font> | |||
ss3[i] = CShort((CInt(ss1[i]) + CInt(ss2[i])) / 2) | |||
Next | |||
<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> | ||
he = CreaBloccoIntestazione(ss3.Count) | |||
fl = Open "/tmp/nuovo.wav" For Create | |||
<FONT color=gray>' ''Crea il nuovo file wav:''</font> | <FONT color=gray>' ''Crea il nuovo file wav:''</font> | ||
he.Write(fl, 0, he.Count) | |||
fl.Close | |||
fl = Open "/tmp/nuovo.wav" For Write Append | |||
<FONT color=gray>' ''Crea il nuovo file wav:''</font> | <FONT color=gray>' ''Crea il nuovo file wav:''</font> | ||
ss3.Write(fl, Stat("/tmp/nuovo.wav").Size, ss3.Count / 2) | |||
<FONT color=gray>' ''Va in chiusura:''</font> | <FONT color=gray>' ''Va in chiusura:''</font> | ||
fl.Close | fl.Close | ||
| Riga 71: | Riga 57: | ||
'''End''' | '''End''' | ||
'''Private''' Function CreaBloccoIntestazione(dati_grezzi As Integer) as Byte[] | '''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] | &44, &AC, &00, &00, &10, &B1, &02, &00, &04, &00, &10, &00, &64, &61, &74, &61] | ||
Dim bb As New Byte[] | |||
Dim i As Integer | |||
bb = Byte[].FromString(ini) | bb = Byte[].FromString(ini) | ||
| Riga 91: | Riga 75: | ||
bb.Add(Shr(i And &FF0000&, 16)) | bb.Add(Shr(i And &FF0000&, 16)) | ||
bb.Add(Shr(i And &FF000000&, 24)) | bb.Add(Shr(i And &FF000000&, 24)) | ||
bb = bb.Insert(bh) | bb = bb.Insert(bh) | ||
<FONT color=gray>' ''Imposta il valore dimensionale di 4 byte a partire dal 41° byte del futuro file | <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(dati_grezzi And &FF) | ||
bb.Add(Shr(dati_grezzi And &FF00&, 8)) | bb.Add(Shr(dati_grezzi And &FF00&, 8)) | ||
Versione delle 17:58, 6 dic 2021
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;
- 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 fileWAV_1, fileWAV_2 As String
Dim fl As File
Dim ss1, ss2, ss3 As Short[]
Dim i As Integer
Dim he As Byte[]
fileWav_1 = "/percorso/del/primo/file.wav"
fileWAV_2 = "/percorso/del/secondo/file.wav"
fl = Open fileWAV_1 For Read
ss1 = New Short[(Stat(fileWAV_1).Size - 44)]
Seek #fl, 44
ss1.Read(fl, 0, ss1.Count / 2)
fl.Close
fl = Open fileWAV_2 For Read
ss2 = New Short[(Stat(fileWAV_2).Size - 44)]
Seek #fl, 44
ss2.Read(fl, 0, ss2.Count / 2)
fl.Close
ss3 = New Short[Max(ss1.Count, ss2.Count)]
For i = 0 To (Max(ss1.Count, ss2.Count) / 2) - 1
' Somma le due onde:
ss3[i] = CShort((CInt(ss1[i]) + CInt(ss2[i])) / 2)
Next
' Creiamo il blocco di intestazione del futuro file wav:
he = CreaBloccoIntestazione(ss3.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:
ss3.Write(fl, Stat("/tmp/nuovo.wav").Size, ss3.Count / 2)
' Va in chiusura:
fl.Close
he.Clear
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 As Integer
bb = Byte[].FromString(ini)
i = dati_grezzi + 36
' Imposta il valore dimensionale di 4 byte a partire dal 5° byte del futuro file:
bb.Add(i And &FF)
bb.Add(Shr(i And &FF00&, 8))
bb.Add(Shr(i And &FF0000&, 16))
bb.Add(Shr(i 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