saveWav.lua 1.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
--[[This software purpose is to train convolutional neural networks for voice presentation attack detection.

Copyright (c) 2017 Idiap Research Institute, http://www.idiap.ch/
Written by Hannah Muckenhirn <hannah.muckenhirn@idiap.ch>,

This file is part of CNN-voice-PAD.

CNN-voice-PAD is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3 as
published by the Free Software Foundation.

CNN-voice-PAD is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with CNN-voice-PAD. If not, see <http://www.gnu.org/licenses/>.--]]

Hannah MUCKENHIRN committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
-- use: torch saveWav.lua listfile_real listfile_attack outputfile
-- read all wavefile in listfile, load them, store them in a table and save the table in binary in outputfile

require "sndfile"

function wav_load(filename_real, filename_attack)
	-- open txt file with filenames
	data_filenames={}
    i=0
	for line in io.lines(filename_real) do	
		table.insert(data_filenames,line);
        i=i+1
	end
        for line in io.lines(filename_attack) do	
		table.insert(data_filenames,line);
        i=i+1
	end
	
	local nSeq=#data_filenames;

	local data={}
	-- load audio files
	for j=1,nSeq do
		f=sndfile.SndFile(data_filenames[j]);
		local nSample=f:info().frames;
		local sRate=f:info().samplerate;
		local seq=torch.ShortTensor(nSample,1);
		f:readShort(seq);
		f:close();
		table.insert(data,seq:squeeze());
	end	

	return data
end
data=wav_load(arg[1], arg[2])
torch.save(arg[3],data)