Code covered by the BSD License  

Highlights from
Sound file converter GUI

image thumbnail

Sound file converter GUI

by

 

The program converts MATLAB supported soundfiles with the use of GUI.

ConverterPanel(varargin)
function varargout = ConverterPanel(varargin)
% CONVERTERPANEL MATLAB code for ConverterPanel.fig
%      CONVERTERPANEL, by itself, creates a new CONVERTERPANEL or raises the existing
%      singleton*.
%
%      H = CONVERTERPANEL returns the handle to a new CONVERTERPANEL or the handle to
%      the existing singleton*.
%
%      CONVERTERPANEL('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in CONVERTERPANEL.M with the given input arguments.
%
%      CONVERTERPANEL('Property','Value',...) creates a new CONVERTERPANEL or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before ConverterPanel_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to ConverterPanel_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help ConverterPanel

% Last Modified by GUIDE v2.5 03-Apr-2013 11:34:32

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @ConverterPanel_OpeningFcn, ...
                   'gui_OutputFcn',  @ConverterPanel_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before ConverterPanel is made visible.
function ConverterPanel_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to ConverterPanel (see VARARGIN)
warning('OFF');
clc;

handles.BrowsedInput = 0;
handles.BrowsedOutput = 0;

% Choose default command line output for ConverterPanel
handles.output = hObject;


% Update handles structure
guidata(hObject, handles);

% UIWAIT makes ConverterPanel wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = ConverterPanel_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in browse_output.
function browse_output_Callback(hObject, eventdata, handles)
% hObject    handle to browse_output (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


if handles.BrowsedOutput == 0
    output_dir = uigetdir;
    if output_dir ~= 0
        handles.BrowsedOutput = 1;
        handles.OutStartDir = output_dir;
    else
        output_dir = 'Directory';
    end
elseif handles.BrowsedOutput == 1
    output_dir = uigetdir(handles.OutStartDir);
    if output_dir == 0
        output_dir = handles.OutStartDir;
    elseif output_dir ~= 0
        handles.OutStartDir = output_dir;
    end
end

handles.browse_output = output_dir;

set(handles.dir_output, 'string', handles.browse_output);

% dir_output_Callback(hObject,eventdata,handles);

guidata(hObject, handles);


function dir_output_Callback(hObject, eventdata, handles)
% hObject    handle to dir_output (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of dir_output as text
%        str2double(get(hObject,'String')) returns contents of dir_output as a double

handles.browse_output = handles.dir_output;
handles.OutStartDir = get(handles.dir_output,'string');

guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function dir_output_CreateFcn(hObject, eventdata, handles)
% hObject    handle to dir_output (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in browse_input.
function browse_input_Callback(hObject, eventdata, handles)
% hObject    handle to browse_input (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if handles.BrowsedInput == 0
    input_dir = uigetdir;
    if input_dir ~= 0
        handles.BrowsedInput = 1;
        handles.InStartDir = input_dir;
    else
        input_dir = 'Directory';
    end
elseif handles.BrowsedInput == 1
    input_dir = uigetdir(handles.InStartDir);
    if input_dir == 0
        input_dir = handles.InStartDir;
    elseif input_dir ~= 0
        handles.InStartDir = input_dir;
    end
end

handles.browse_input = input_dir;

set(handles.dir_input, 'string', handles.browse_input);

% dir_input_Callback(hObject,eventdata,handles);

guidata(hObject, handles);


function dir_input_Callback(hObject, eventdata, handles)
% hObject    handle to dir_input (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of dir_input as text
%        str2double(get(hObject,'String')) returns contents of dir_input as a double

handles.browse_input = handles.dir_input;
handles.InStartDir = get(handles.dir_input,'string');

guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function dir_input_CreateFcn(hObject, eventdata, handles)
% hObject    handle to dir_input (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes when figure1 is resized.
function figure1_ResizeFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes when selected object is changed in filetype_input_panel.
function filetype_input_panel_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in filetype_input_panel 
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%	EventName: string 'SelectionChanged' (read only)
%	OldValue: handle of the previously selected object or empty if none was selected
%	NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)


% --- Executes when selected object is changed in filetype_output_panel.
function filetype_output_panel_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in filetype_output_panel 
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%	EventName: string 'SelectionChanged' (read only)
%	OldValue: handle of the previously selected object or empty if none was selected
%	NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in Convert.
function Convert_Callback(hObject, eventdata, handles)
% hObject    handle to Convert (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


if (exist('audioread') == 2 || exist('audioread') == 5) && (exist('audiowrite') == 2 || exist('audiowrite') == 5) && (exist('audioinfo') == 2 || exist('audioinfo') == 5)
    
else
    uiwait(errordlg('Needed functions: audioread, audiowrite and audioinfo does not exist. The program can therefor not run.','Function load error','modal'));
    close all;
    return
end

if exist(get(handles.dir_input, 'string'),'dir')
else
    uiwait(errordlg(['Input directory: ' get(handles.dir_input, 'string') ', does not exist'],'Error','modal'));
    browse_input_Callback(hObject, eventdata, handles);
    return;
end

if exist(get(handles.dir_output, 'string'),'dir')
else
    uiwait(errordlg(['Output directory: ' get(handles.dir_output, 'string') ', does not exist'],'Error','modal'));
    browse_output_Callback(hObject, eventdata, handles);
    return;
end


% if handles.SubFolConvert == 1
%     
% end


if strcmp(get(get(handles.filetype_output_panel,'SelectedObject'),'String'), 'Vorbis (.ogg, .oga)')
    Options.WindowStyle = 'modal';
    Quality = inputdlg('Enter Quality (0-100)','Quality',1,{'100'},Options);
    if isempty(Quality) || isempty(str2num(cell2mat(Quality))) || str2num(cell2mat(Quality))>100 || str2num(cell2mat(Quality))<0
        uiwait(errordlg([cell2mat(Quality) ' is not within the specified range (0-100)'],'Error','modal'))
        return
    end
end

if strcmp(get(get(handles.filetype_output_panel,'SelectedObject'),'String'), 'MPEG-4 (.m4a, .mp4)')
    Options.WindowStyle = 'modal';
    Quality = inputdlg('Enter BitRate (96, 128, 160, 192 kbps)','Quality',1,{'192'},Options);
    if isempty(Quality) || isempty(str2num(cell2mat(Quality))) || (str2num(cell2mat(Quality))~=96 && str2num(cell2mat(Quality))~=96 &&...
            str2num(cell2mat(Quality))~=128 && str2num(cell2mat(Quality))~=160 &&...
            str2num(cell2mat(Quality))~=192 && str2num(cell2mat(Quality))~=192 &&...
            str2num(cell2mat(Quality))~=192)
        uiwait(errordlg([cell2mat(Quality) 'kbps is not a valid BitRate'],'Error','modal'))
    return
    end
else
    Quality = {'0'};
end

% % MATLABPOOL------------------------------------
% 
% choice = questdlg('Run at full speed?', ...
% 	'Can you handle the pressure?', ...
% 	'Yes!','Nope','Yes!');
% switch choice
%     case 'Yes!'
%         disp(['Running at full speed'])
%         if matlabpool('size') == 0
%             matlabpool local;
%         end
%     case 'Nope'
%         if matlabpool('size') > 0
%             matlabpool close;
%         end
% end
% 
% % MATLABPOOL------------------------------------

clc;

temp = ['Processing'];
message{1} = temp;
temp = ['________________________________________________'];
message{2} = temp;
% messageBox = msgbox(message,'Activity');

OutputFileType = get(get(handles.filetype_output_panel,'SelectedObject'),'String');
OutputDirectory = get(handles.dir_output, 'string');

switch get(get(handles.filetype_input_panel,'SelectedObject'),'String');
    case 'Wave (.wav)';
            directory_input = dir([get(handles.dir_input, 'string') '\*.wav']);
            for i = 1:length(directory_input)
               message{i+2} = directory_input(i).name;
            end
            messageLength = length(message);
            message{messageLength+1} = ['________________________________________________'];
            messageBox = msgbox(message,'Activity','replace');
            for i = 1:length(directory_input)
                AudioInfo = audioinfo([get(handles.dir_input, 'string') '\' directory_input(i).name]);
                if isempty(AudioInfo.Title)
                    AudioInfo.Title = directory_input(i).name(1:end-4);
                end
                if isempty(AudioInfo.Artist)
                    AudioInfo.Artist = '-';
                end
                Comment = ['Converted from WAV with ' num2str(AudioInfo.BitsPerSample) ' bits/sample and ' num2str(AudioInfo.SampleRate/1000) ' samples/second'];
                [AudioData Fs] = audioread([get(handles.dir_input, 'string') '\' directory_input(i).name]);
                if max(max(abs(AudioData))) > 1
                    AudioData = (AudioData./max(max(abs(AudioData))))*0.999999999999;
                end
                WriteAudio(AudioData, Fs, AudioInfo, OutputFileType, OutputDirectory, directory_input(i).name(1:end-4),Quality,Comment)
                message{messageLength+2} = ([num2str(i/length(directory_input)*100) '% done']);
                messageBox = msgbox(message,'Activity','replace');
            end
    case 'Flac (.flac)';
            directory_input = dir([get(handles.dir_input, 'string') '\*.flac']);
            for i = 1:length(directory_input)
               message{i+2} = directory_input(i).name;
            end
            messageLength = length(message);
            message{messageLength+1} = ['________________________________________________'];
            messageBox = msgbox(message,'Activity','replace');
            for i = 1:length(directory_input)
                AudioInfo = audioinfo([get(handles.dir_input, 'string') '\' directory_input(i).name]);
                if isempty(AudioInfo.Title)
                    AudioInfo.Title = directory_input(i).name(1:end-5);
                end
                if isempty(AudioInfo.Artist)
                    AudioInfo.Artist = '-';
                end
                Comment = ['Converted from FLAC with ' num2str(AudioInfo.BitsPerSample) ' bits/sample and ' num2str(AudioInfo.SampleRate/1000) ' samples/second'];
                [AudioData Fs] = audioread([get(handles.dir_input, 'string') '\' directory_input(i).name]);
                if max(max(abs(AudioData))) > 1
                    AudioData = (AudioData./max(max(abs(AudioData))))*0.999999999999;
                end
                WriteAudio(AudioData, Fs, AudioInfo, OutputFileType, OutputDirectory, directory_input(i).name(1:end-5),Quality,Comment)
                message{messageLength+2} = ([num2str(i/length(directory_input)*100) '% done']);
                messageBox = msgbox(message,'Activity','replace');
            end
    case 'MPEG-4 (.m4a, .mp4)';
            directory_input = dir([get(handles.dir_input, 'string') '\*.m4a']);
            for i = 1:length(directory_input)
               message{i+2} = directory_input(i).name;
            end
            messageLength = length(message);
            message{messageLength+1} = ['________________________________________________'];
            messageBox = msgbox(message,'Activity','replace');
            for i = 1:length(directory_input)
                AudioInfo = audioinfo([get(handles.dir_input, 'string') '\' directory_input(i).name]);
                AudioInfo.BitsPerSample = 16;
                if isempty(AudioInfo.Title)
                    AudioInfo.Title = directory_input(i).name(1:end-4);
                end
                if isempty(AudioInfo.Artist)
                    AudioInfo.Artist = '-';
                end
                Comment = ['Converted from MP4 with ' num2str(AudioInfo.BitRate/1000) ' kbps'];
                [AudioData Fs] = audioread([get(handles.dir_input, 'string') '\' directory_input(i).name]);
                if max(max(abs(AudioData))) > 1
                    AudioData = (AudioData./max(max(abs(AudioData))))*0.999999999999;
                end
                WriteAudio(AudioData, Fs, AudioInfo, OutputFileType, OutputDirectory, directory_input(i).name(1:end-4),Quality,Comment)
                message{messageLength+2} = ([num2str(i/length(directory_input)*100) '% done']);
                messageBox = msgbox(message,'Activity','replace');
            end
    case 'Vorbis (.ogg)';
            directory_input = dir([get(handles.dir_input, 'string') '\*.ogg']);
            for i = 1:length(directory_input)
               message{i+2} = directory_input(i).name;
            end
            messageLength = length(message);
            message{messageLength+1} = ['________________________________________________'];
            messageBox = msgbox(message,'Activity','replace');
            for i = 1:length(directory_input)
                AudioInfo = audioinfo([get(handles.dir_input, 'string') '\' directory_input(i).name]);
                AudioInfo.BitsPersSample = 16;
                if isempty(AudioInfo.Title)
                    AudioInfo.Title = directory_input(i).name(1:end-4);
                end
                if isempty(AudioInfo.Artist)
                    AudioInfo.Artist = '-';
                end
                Comment = ['Converted from OGG with unknown quality'];
                [AudioData Fs] = audioread([get(handles.dir_input, 'string') '\' directory_input(i).name]);
                if max(max(abs(AudioData))) > 1
                    AudioData = (AudioData./max(max(abs(AudioData))))*0.999999999999;
                end
                WriteAudio(AudioData, Fs, AudioInfo, OutputFileType, OutputDirectory, directory_input(i).name(1:end-4),Quality,Comment)
                message{messageLength+2} = ([num2str(i/length(directory_input)*100) '% done']);
                messageBox = msgbox(message,'Activity','replace');
            end
    case 'MP3 (.mp3)';
            directory_input = dir([get(handles.dir_input, 'string') '\*.mp3']);
            for i = 1:length(directory_input)
               message{i+2} = directory_input(i).name;
            end
            messageLength = length(message);
            message{messageLength+1} = ['________________________________________________'];
            messageBox = msgbox(message,'Activity','replace');
            for i = 1:length(directory_input)
                AudioInfo = audioinfo([get(handles.dir_input, 'string') '\' directory_input(i).name]);
                AudioInfo.BitsPerSample = 16;
                if isempty(AudioInfo.Title)
                    AudioInfo.Title = directory_input(i).name(1:end-4);
                end
                if isempty(AudioInfo.Artist)
                    AudioInfo.Artist = '-';
                end
                Comment = ['Converted from MP3 with ' num2str(AudioInfo.BitRate/1000) ' kbps'];
                [AudioData Fs] = audioread([get(handles.dir_input, 'string') '\' directory_input(i).name]);
                if max(max(abs(AudioData))) > 1
                    AudioData = (AudioData./max(max(abs(AudioData))))*0.999999999999;
                end
                WriteAudio(AudioData, Fs, AudioInfo, OutputFileType, OutputDirectory, directory_input(i).name(1:end-4),Quality,Comment)
                message{messageLength+2} = ([num2str(i/length(directory_input)*100) '% done']);
                messageBox = msgbox(message,'Activity','replace');
            end
end
% --- Executes on button press in Quit.
function Quit_Callback(hObject, eventdata, handles)
% hObject    handle to Quit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


choice = questdlg('Are you sure that you want to quit?', ...
	'Abandon ship?', ...
	'Yes?','Of course not!','Of course not!');
switch choice
    case 'Yes?'
        disp(['SoundConverter ended by user'])
%         if matlabpool('size') > 0
%             message = ['Closing program'];
%             messageBox = msgbox(message,'Closing','replace');
%             matlabpool close;
%         end
        close all;
    case 'Of course not'
end
clc;

function WriteAudio(AudioData, Fs, AudioInfo, OutputFileType, OutputDirectory, FileName,Quality,Comment)

switch OutputFileType
    case 'Wave (.wav)';
        audiowrite([OutputDirectory '\' FileName '.wav'],AudioData,Fs,'BitsPerSample',AudioInfo.BitsPerSample,'Title',AudioInfo.Title,'Artist',AudioInfo.Artist,'Comment',Comment);
    case 'Flac (.flac)';
        audiowrite([OutputDirectory '\' FileName '.flac'],AudioData,Fs,'BitsPerSample',AudioInfo.BitsPerSample,'Title',AudioInfo.Title,'Artist',AudioInfo.Artist,'Comment',Comment);
    case 'MPEG-4 (.m4a, .mp4)';
        if Fs~=44100 || Fs~=48000
            AudioData = resample(AudioData,44100,Fs); %FIX!!!!!!!!!!!!!!!!!!!!!!!!!! Private investigations
            Fs = 44100;
        end
        audiowrite([OutputDirectory '\' FileName '.m4a'],AudioData,Fs,'BitRate',str2num(cell2mat(Quality)),'Title',AudioInfo.Title,'Artist',AudioInfo.Artist,'Comment',Comment);
    case 'Vorbis (.ogg, .oga)';
        audiowrite([OutputDirectory '\' FileName '.ogg'],AudioData,Fs,'Quality',str2num(cell2mat(Quality)),'Title',AudioInfo.Title,'Artist',AudioInfo.Artist,'Comment',Comment);
end


% --- Executes on button press in ReadMe.
function ReadMe_Callback(hObject, eventdata, handles)
% hObject    handle to ReadMe (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

ReadMe_Message{1} = ['Be aware of issues with this program. Tha author is not responsible for any dataloss that may occur'];
ReadMe_Message{2} = ['________________________________________________________'];
ReadMe_Message{3} = [''];
ReadMe_Message{4} = ['- For the time being conversion to .OGG has issues with long audio files.'];
ReadMe_Message{5} = [''];
ReadMe_Message{6} = ['- Conversion to MPEG-4 does not support 64, 256 and 320 kbps.'];
ReadMe_Message{7} = [''];
ReadMe_Message{8} = ['- Conversion from lossy format to lossless format is not adviced and is merely a convenient feature'];
ReadMe_Message{9} = [''];
ReadMe_Message{10} = ['- Running at full speed is not necessarily faster (Optimization needed).'];
ReadMe_Message{11} = [''];
ReadMe_Message{12} = ['- "Convert files in subfolders" means that every file further down i the chosen folder will get converted. Be aware that not choosing "Recreate folder structure" will make the output folder a mess.'];
ReadMe_Message{13} = [''];
ReadMe_Message{14} = ['- "Recreate folder structure" will copy the folder structure of the choosen input folder'];
ReadMe_Message{15} = [''];
msgbox(ReadMe_Message,'ReadMe');


% --- Executes on button press in SubFolConvert.
function SubFolConvert_Callback(hObject, eventdata, handles)
% hObject    handle to SubFolConvert (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of SubFolConvert
if get(handles.RecFolStr,'Value') == 1
    set(handles.RecFolStr,'Value',0)
end

guidata(hObject,handles);


% --- Executes on button press in RecFolStr.
function RecFolStr_Callback(hObject, eventdata, handles)
% hObject    handle to RecFolStr (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of RecFolStr
if get(handles.SubFolConvert,'Value') == 0
    options.Default = 'Yes';options.Interpreter = 'none';
    Question{1} = ['You can not choose to "Recreate folder structure" if "Convert files in subfolders" is not chosen.'];
    Question{2} = ['_______________________________________________________'];
    Question{3} = [''];
    Question{4} = ['Would you like to "Convert files in subfolders"?'];
    Choice = questdlg(Question,'Alternative','Yes','No',options);
    if strcmp(Choice,'Yes')
        set(handles.RecFolStr,'Value',1);
        set(handles.SubFolConvert,'Value',1);
    else
        set(handles.RecFolStr,'Value',0);
    end
end


guidata(hObject,handles);


% --- Executes on button press in Convert_Single.
function Convert_Single_Callback(hObject, eventdata, handles)
% hObject    handle to Convert_Single (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

switch get(get(handles.filetype_input_panel,'SelectedObject'),'String');
    case 'Wave (.wav)';
        if exist(get(handles.dir_input, 'string'),'dir')
            StartFileType = [get(handles.dir_input, 'string') '\*.wav'];
        else
            StartFileType = ['*.wav'];
        end
    case 'Flac (.flac)';
        if exist(get(handles.dir_input, 'string'),'dir')
            StartFileType = [get(handles.dir_input, 'string') '\*.flac'];
        else        
            StartFileType = ['*.flac'];
        end
    case 'MPEG-4 (.m4a, .mp4)';
        if exist(get(handles.dir_input, 'string'),'dir')
            StartFileType = [get(handles.dir_input, 'string') '\*.m4a'];
        else
            StartFileType = ['*.m4a'];
        end
    case 'Vorbis (.ogg)';
        if exist(get(handles.dir_input, 'string'),'dir')
            StartFileType = [get(handles.dir_input, 'string') '\*.ogg'];
        else
            StartFileType = ['*.ogg'];
        end
    case 'MP3 (.mp3)';
        if exist(get(handles.dir_input, 'string'),'dir')
            StartFileType = [get(handles.dir_input, 'string') '\*.mp3'];
        else
            StartFileType = ['*.mp3'];
        end
end
        
[FileName, PathName, FileType] = uigetfile({'*.wav','Wave';'*.flac','Flac';'*.m4a','MPEG-4';'*.ogg','OggVorbis';'*.mp3','MP3'}, 'MultiSelect','on','Pick files to convert',StartFileType);

if (exist('audioread') == 2 || exist('audioread') == 5) && (exist('audiowrite') == 2 || exist('audiowrite') == 5) && (exist('audioinfo') == 2 || exist('audioinfo') == 5)
    
else
    uiwait(errordlg('Needed functions: audioread, audiowrite and audioinfo does not exist. The program can therefor not run.','Function load error','modal'));
    close all;
    return
end

if ischar(FileName)
    FileName = cellstr(FileName);
    PathName = cellstr(PathName);
elseif iscell(FileName)
else
    return;
end

if exist(get(handles.dir_output, 'string'),'dir')
else
    uiwait(errordlg(['Output directory: ' get(handles.dir_output, 'string') ', does not exist'],'Error','modal'));
    browse_output_Callback(hObject, eventdata, handles);
    if strcmp(get(handles.dir_output,'String'),'Directory')
    return;
    end
end

if strcmp(get(get(handles.filetype_output_panel,'SelectedObject'),'String'), 'Vorbis (.ogg, .oga)')
    Options.WindowStyle = 'modal';
    Quality = inputdlg('Enter Quality (0-100)','Quality',1,{'100'},Options);
    if isempty(Quality) || isempty(str2num(cell2mat(Quality))) || str2num(cell2mat(Quality))>100 || str2num(cell2mat(Quality))<0
        uiwait(errordlg([cell2mat(Quality) ' is not within the specified range (0-100)'],'Error','modal'))
        return
    end
end

if strcmp(get(get(handles.filetype_output_panel,'SelectedObject'),'String'), 'MPEG-4 (.m4a, .mp4)')
    Options.WindowStyle = 'modal';
    Quality = inputdlg('Enter BitRate (96, 128, 160, 192 kbps)','Quality',1,{'192'},Options);
    if isempty(Quality) || isempty(str2num(cell2mat(Quality))) || (str2num(cell2mat(Quality))~=96 && str2num(cell2mat(Quality))~=96 &&...
            str2num(cell2mat(Quality))~=128 && str2num(cell2mat(Quality))~=160 &&...
            str2num(cell2mat(Quality))~=192 && str2num(cell2mat(Quality))~=192 &&...
            str2num(cell2mat(Quality))~=192)
        uiwait(errordlg([cell2mat(Quality) 'kbps is not a valid BitRate'],'Error','modal'))
    return
    end
else
    Quality = {'0'};
end

clc;

temp = ['Processing'];
message{1} = temp;
temp = ['________________________________________________'];
message{2} = temp;
% messageBox = msgbox(message,'Activity');

OutputFileType = get(get(handles.filetype_output_panel,'SelectedObject'),'String');
OutputDirectory = get(handles.dir_output, 'string');

switch FileType
    case 1;
            for i = 1:length(FileName)
               message{i+2} = FileName{i};
            end
            messageLength = length(message);
            message{messageLength+1} = ['________________________________________________'];
            messageBox = msgbox(message,'Activity','replace');
            for i = 1:length(FileName)
                AudioInfo = audioinfo([PathName{1} FileName{i}]);
                if isempty(AudioInfo.Title)
                    AudioInfo.Title = FileName{i}(1:end-4);
                end
                if isempty(AudioInfo.Artist)
                    AudioInfo.Artist = '-';
                end
                Comment = ['Converted from WAV with ' num2str(AudioInfo.BitsPerSample) ' bits/sample and ' num2str(AudioInfo.SampleRate/1000) ' samples/second'];
                [AudioData Fs] = audioread([PathName{1} FileName{i}]);
                if max(max(abs(AudioData))) > 1
                    AudioData = (AudioData./max(max(abs(AudioData))))*0.999999999999;
                end
                WriteAudio(AudioData, Fs, AudioInfo, OutputFileType, OutputDirectory, FileName{i}(1:end-4),Quality,Comment)
                message{messageLength+2} = ([num2str(i/length(FileName)*100) '% done']);
                messageBox = msgbox(message,'Activity','replace');
            end
    case 2;
            for i = 1:length(FileName)
               message{i+2} = FileName{i};
            end
            messageLength = length(message);
            message{messageLength+1} = ['________________________________________________'];
            messageBox = msgbox(message,'Activity','replace');
            for i = 1:length(FileName)
                AudioInfo = audioinfo([PathName{1} FileName{i}]);
                if isempty(AudioInfo.Title)
                    AudioInfo.Title = FileName{i}(1:end-5);
                end
                if isempty(AudioInfo.Artist)
                    AudioInfo.Artist = '-';
                end
                Comment = ['Converted from FLAC with ' num2str(AudioInfo.BitsPerSample) ' bits/sample and ' num2str(AudioInfo.SampleRate/1000) ' samples/second'];
                [AudioData Fs] = audioread([PathName{1} FileName{i}]);
                if max(max(abs(AudioData))) > 1
                    AudioData = (AudioData./max(max(abs(AudioData))))*0.999999999999;
                end
                WriteAudio(AudioData, Fs, AudioInfo, OutputFileType, OutputDirectory, FileName{i}(1:end-5),Quality,Comment)
                message{messageLength+2} = ([num2str(i/length(FileName)*100) '% done']);
                messageBox = msgbox(message,'Activity','replace');
            end
    case 3;
            for i = 1:length(FileName)
               message{i+2} = FileName{i};
            end
            messageLength = length(message);
            message{messageLength+1} = ['________________________________________________'];
            messageBox = msgbox(message,'Activity','replace');
            for i = 1:length(FileName)
                AudioInfo = audioinfo([PathName{1} FileName{i}]);
                AudioInfo.BitsPerSample = 16;
                if isempty(AudioInfo.Title)
                    AudioInfo.Title = FileName{i}(1:end-4);
                end
                if isempty(AudioInfo.Artist)
                    AudioInfo.Artist = '-';
                end
                Comment = ['Converted from MP4 with ' num2str(AudioInfo.BitRate/1000) ' kbps'];
                [AudioData Fs] = audioread([PathName{1} FileName{i}]);
                if max(max(abs(AudioData))) > 1
                    AudioData = (AudioData./max(max(abs(AudioData))))*0.999999999999;
                end
                WriteAudio(AudioData, Fs, AudioInfo, OutputFileType, OutputDirectory, FileName{i}(1:end-4),Quality,Comment)
                message{messageLength+2} = ([num2str(i/length(FileName)*100) '% done']);
                messageBox = msgbox(message,'Activity','replace');
            end
    case 4;
            for i = 1:length(FileName)
               message{i+2} = FileName{i};
            end
            messageLength = length(message);
            message{messageLength+1} = ['________________________________________________'];
            messageBox = msgbox(message,'Activity','replace');
            for i = 1:length(FileName)
                AudioInfo = audioinfo([PathName{1} FileName{i}]);
                AudioInfo.BitsPersSample = 16;
                if isempty(AudioInfo.Title)
                    AudioInfo.Title = FileName{i}(1:end-4);
                end
                if isempty(AudioInfo.Artist)
                    AudioInfo.Artist = '-';
                end
                Comment = ['Converted from OGG with unknown quality'];
                [AudioData Fs] = audioread([PathName{1} FileName{i}]);
                if max(max(abs(AudioData))) > 1
                    AudioData = (AudioData./max(max(abs(AudioData))))*0.999999999999;
                end
                WriteAudio(AudioData, Fs, AudioInfo, OutputFileType, OutputDirectory, FileName{i}(1:end-4),Quality,Comment)
                message{messageLength+2} = ([num2str(i/length(FileName)*100) '% done']);
                messageBox = msgbox(message,'Activity','replace');
            end
    case 5;
            for i = 1:length(FileName)
               message{i+2} = FileName{i};
            end
            messageLength = length(message);
            message{messageLength+1} = ['________________________________________________'];
            messageBox = msgbox(message,'Activity','replace');
            for i = 1:length(FileName)
                AudioInfo = audioinfo([PathName{1} FileName{i}]);
                AudioInfo.BitsPerSample = 16;
                if isempty(AudioInfo.Title)
                    AudioInfo.Title = FileName{i}(1:end-4);
                end
                if isempty(AudioInfo.Artist)
                    AudioInfo.Artist = '-';
                end
                Comment = ['Converted from MP3 with ' num2str(AudioInfo.BitRate/1000) ' kbps'];
                [AudioData Fs] = audioread([PathName{1} FileName{i}]);
                if max(max(abs(AudioData))) > 1
                    AudioData = (AudioData./max(max(abs(AudioData))))*0.999999999999;
                end
                WriteAudio(AudioData, Fs, AudioInfo, OutputFileType, OutputDirectory, FileName{i}(1:end-4),Quality,Comment)
                message{messageLength+2} = ([num2str(i/length(FileName)*100) '% done']);
                messageBox = msgbox(message,'Activity','replace');
            end
end

Contact us