
------------------------------------------------------------------------
--# Converts audio file formats, requires <b>ffmpeg</b>!
------------------------------------------------------------------------
-- formats handled are in the constant x below.
-- You can, of course, add more formats if you want.
-- Put the default input format first on the list,
-- and the default output format last, so you won't
-- have to select them each time you run this program.
------------------------------------------------------------------------

include GtkEngine.e
include GtkEvents.e
include std/sort.e
include std/task.e
include std/os.e

constant x = {"wma","wav","ogg","mp3"}

object folder, files

constant win = create(GtkWindow)
	connect(win,"destroy","Quit")
	set(win,"border width",10)
	set(win,"default size",600,250)
	set(win,"position",GTK_WIN_POS_CENTER)

constant panel = create(GtkBox,VERTICAL)
	add(win,panel)

constant top = create(GtkBox,HORIZONTAL)
	pack(panel,top)

constant path = create(GtkFileChooserButton, -- select music folder;
	"Select a folder",GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
	connect(path,"current-folder-changed","LoadList")
	set(path,"tooltip text","Select a music folder")
	pack(top,path,TRUE,TRUE)

constant extension1 = create(GtkComboBoxText) -- input file format;
	for i = 1 to length(x) do
		set(extension1,"append text",x[i])
	end for
	set(extension1,"tooltip text","Select input format")
	set(extension1,"active",1)
	pack(top,extension1)

constant extension2 = create(GtkComboBoxText) -- output file format;
	for i = 1 to length(x) do
		set(extension2,"append text",x[i])
	end for
	set(extension2,"tooltip text","Select output format")
	set(extension2,"active",length(x))
	pack(top,extension2)

constant scroller = create(GtkScrolledWindow)
	pack(panel,scroller,TRUE,TRUE)

constant store = create(GtkListStore,{gSTR,gINT,gINT}) -- file list;

constant tv = create(GtkTreeView)
	add(scroller,tv)
	set(tv,"hover selection",TRUE)
	set(tv,"headers clickable",TRUE)
	set(tv,"rules hint",TRUE)
	set(tv,"model",store)

constant selection = get(tv,"selection")
	set(selection,"mode",GTK_SELECTION_BROWSE)

constant col1 = create(GtkTreeViewColumn)
	constant ren1 = create(GtkCellRendererText)
	set(col1,"title","Filename")
	set(col1,"pack start",ren1)
	set(col1,"add attribute",ren1,"markup",1)
	set(col1,"sort column id",1)
	set(tv,"append column",col1)

constant col2 = create(GtkTreeViewColumn)
constant ren2 = create(GtkCellRendererToggle)
	set(col2,"title","Converted")
	set(col2,"pack start",ren2)
	set(col2,"add attribute",ren2,"active",2)
	set(tv,"append column",col2)

constant box = create(GtkButtonBox),
	btn1 = create(GtkButton,"gtk-quit","Quit"),
	btn2 = create(GtkButton,"gtk-help","Help"),
	btn3 = create(GtkButton,"gtk-ok","Convert")
	set(box,"margin top",10)
	set(btn1,"tooltip text","Click to exit")
	set(btn2,"tooltip text","Click for help")
	set(btn3,"tooltip text","Click to start conversion")
	add(box,{btn1,btn2,btn3})
	pack(panel,-box)

show_all(win)

if system_exec("which ffmpeg",0) != 0 then
	set(btn3,"sensitive",FALSE)
	Warn(win,"Error","Cannot find ffmpeg program","Please install one!")
end if

main()

------------------------------------------
global function LoadList()
------------------------------------------
object ext_in = get(extension1,"active")
	folder = get(path,"filename")
	ext_in = x[ext_in]
	
	files = dir(folder & "/*." & ext_in)
	if atom(files) then return -1 end if
	
	files = sort(vslice(files,1))
	set(store,"clear")
	set(store,"data",files)
	
return 1
end function

---------------------------
global function Convert() 
---------------------------
atom iter
object in, out, 
	ext1 = x[get(extension1,"active")],
	ext2 = x[get(extension2,"active")],
	parameters = " -ab 128k "

	switch ext2 do
		case "ogg" then parameters = " -acodec libvorbis -ab 128k "
		case "mp3" then parameters = " -acodec libmp3lame -ab 128k "
	end switch

	set(path,"sensitive",FALSE)
	set(extension1,"sensitive",FALSE)
	set(extension2,"sensitive",FALSE)
	set(tv,"scroll to point",0,1)

	for i = 1 to length(files) do
		in = filebase(files[i])
		out = filebase(files[i])
		iter = get(store,"iter from string",sprintf("%d",i-1))

		set(store,"col data",iter,1,
			sprintf("<span color='red'>%s.%s</span>",{in,ext1}))
		while events:pending() do
			main_iteration()
		end while
		
			system_exec(sprintf("ffmpeg -y -i \"%s/%s.%s\" %s \"%s/%s.%s\"\n",
				{folder,in,ext1,parameters,folder,out,ext2}),0)
				
		set(store,"col data",iter,1,
			sprintf("<span color='darkgreen'>%s.%s</span>",{out,ext2}))

		set(store,"col data",iter,2,"active",TRUE)
		set(tv,"scroll to point",0,(i-1)*25)
		while events:pending() do
			main_iteration()
		end while
	end for

	set(path,"sensitive",TRUE)
	set(extension1,"sensitive",TRUE)
	set(extension2,"sensitive",TRUE)

return 1
end function

------------------------------------------------------------
global function Help()
------------------------------------------------------------
return Info(win,"Audio Converter", "Converts audio files","""
_____to/from your choice of output formats
	 <span font-size='smaller'>
	 It does <i>not</i> overwrite the original, 
	 just creates a copy in the new format,
	 unless the input and output filenames are the same. 
	 
	 Run this program from an x-term 
	 if you want to monitor the ffmpeg progress. 
	 
	 Select a directory in the box at top,
	 and the input and output formats using the drop-down
	 options on the right.</span>"""
)
end function



