lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 04/09/2011 16.59, Luciano de Souza wrote:
[...]

Try this and see if it helps.
Warning: ALMOST UNTESTED CODE.

Change the path1/path2 locals to point to your files or make a better script by taking the paths from the cmd line!

Note: uses info from the link posted by Patrick:

https://ccrma.stanford.edu/courses/422/projects/WaveFormat/


Cheers!
-- Lorenzo

--[=======================================================[--

**WAV File Joiner**.

=========================================================

A simple WAV file joiner script.

Limitations: it performs all processing in memory, so it may
not handle HUGE files.

<br/> <br/>

=======================================================================

Copyright (C) 2011 Lorenzo Donati.  All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

=======================================================================
<br/> <br/>

@author Lorenzo Donati
]=======================================================]

local function ReadFile( fname )
	local fh = assert( io.open( fname, 'rb' ) )
	local content = fh:read '*a'
	fh:close()
	return content
end

local function WriteFile( fname, content )
	local fh = assert( io.open( fname, 'wb' ) )
	fh:write( content )
	fh:close()
end

local function UInt32ToNum( data )
	local b0 = data:sub( 1,1 ):byte()
	local b1 = data:sub( 2, 2 ):byte()
	local b2 = data:sub( 3, 3 ):byte()
	local b3 = data:sub( 4, 4 ):byte()
	return b0 + 256* ( b1 + 256 * ( b2 + 256 * b3 ) )
end

local function NumToUint32( x )
	local c0 = x % 256; x = ( x - c0 ) / 256
	local c1 = x % 256; x = ( x - c1 ) / 256
	local c2 = x % 256; x = ( x - c2 ) / 256
	local c3 = x
	return string.char( c0, c1, c2, c3 )
end

local function ParseHeader( data )
	local data = data:sub( 1, 44 )
	assert( data:sub( 13, 16 ) == 'fmt ', "unrecognized format" )
	assert( data:sub( 37, 40 ) == "data", "unrecognized format" )
	local subchunk = data:sub( 13,  36 )
	local Subchunk2Size = data:sub( 41, 44 )
	local size = UInt32ToNum( Subchunk2Size )
	assert( NumToUint32( size ) == Subchunk2Size, "conversion error" )
	return size, subchunk
end



-- Change these paths to suit your needs:
local path1 = "test.wav"
local path2 = "test2.wav"



local wav1 = ReadFile( path1 )
local wav2 = ReadFile( path2 )

local size1, subchunk1 = ParseHeader( wav1 )
local size2, subchunk2 = ParseHeader( wav2 )
assert( subchunk1 == subchunk2, "WAV file type mismatch - cannot join them" )

local totsize = size1 + size2
totsize = NumToUint32( totsize )
local buf = {}
buf[ #buf + 1 ] = wav1:sub( 1, 40 )
buf[ #buf + 1 ] = totsize
buf[ #buf + 1 ] = wav1:sub( 45, 45 + size1 - 1 )
buf[ #buf + 1 ] = wav2:sub( 45, 45 + size2 - 1 )

WriteFile( "out.wav", table.concat( buf ) )