function [] = ProcessDatabase( databaseName, functionName, sampleStartNum, OVERWRITE )
% run from D:\Dropbox\BUPTResearch2011\EmotionDetectionCode\Code
machineName =  getenv('COMPUTERNAME');

switch machineName
    case 'LAPTOP'
    % for Dawn's laptop
        root = 'D:\Dropbox\'
    case 'SLATE1'
        % for Dawn's Slate
        root = 'E:\Dropbox\'
    case 'DAWNBLACK-PC'
        % for Dawn's work PC
        root = 'C:\Users\dawn\Dropbox\'
end

addpath ([root 'BUPTResearch2011\emotionDetectionCode\Code'])
addpath ([root 'BUPTResearch2011\emotionDetectionCode\Code\Descriptors\Matlab\MPEG7'])
addpath ([root 'BUPTResearch2011\emotionDetectionCode\Code\Descriptors\Matlab\Common'])
addpath ([root 'BUPTResearch2011\emotionDetectionCode\Code\Descriptors\Matlab\MPEG7\FromWeb'])
addpath ([root 'BUPTResearch2011\emotionDetectionCode\Code\Descriptors\PRAAT'])
addpath ([root 'BUPTResearch2011\emotionDetectionCode\Code\Collation'])
addpath ([root 'BUPTResearch2011\emotionDetectionCode\Code\General'])

switch databaseName
    case 'ChineseOpera'
        cd([root 'BUPTResearch2011/Data/Opera/TestDatabase'])
        startEmotion = 4;
    case 'MandarinSpeech'
        cd([root 'BUPTResearch2011/Data/Database/EditedRecording'])
        startEmotion = 3;
    case 'SpeechTestFiles'
        cd([root 'BUPTResearch2011\Data\SpeechTestFiles\MyAnnotatedFiles'])
        startEmotion = 3;
end

fileStructure = dir;

% how many samples do we have?
noOfSamples = size( fileStructure );
firstfileOpen = 1;

if( sampleStartNum < 3 )
    sampleStartNum = 3;
end

for sampleNum = sampleStartNum : noOfSamples(1)
    sampleDirName = fileStructure(sampleNum).name
    if( fileStructure(sampleNum).isdir == 1 ) % only directories
        cd( sampleDirName );
        % how many emotions for that sample?
        validEmotionList = dir;
        noOfEmotions = size( validEmotionList );

        for emotionNum = startEmotion : noOfEmotions(1)
            emotionName = validEmotionList(emotionNum).name;
            cd( emotionName );
            % how many samples for that emotion?
            sampleNames = dir;
            noOfSamples = size( sampleNames );

            for sampleNumber = 3 : noOfSamples(1)
                if( sampleNames(sampleNumber).isdir == 0 ) % skip directories
                    currentSampleName = sampleNames(sampleNumber).name
                    % get the file type
                    extName = currentSampleName(length( currentSampleName ) - 3: end);
                    % is it a .wav file?
                    if strcmp( extName, '.wav' )
                        newDirName = currentSampleName( 1 : length( currentSampleName ) - 4 );
                        % if none exists, make a new directory for all the results of
                        % our calculations.
                        mkdir( [ newDirName '_metrics'] );
                        cd ( [ newDirName '_metrics'] );

                         % call your function here
                        switch functionName
                            % functions that start with 'get' simply read
                            % from existing text files
                            % functions that start with 'detect' check if
                            % the results file already exists, if they do
                            % not (or the OVERWRITE flag is true) then the
                            % results are calculated afresh.
                            
    %                         case 'getsampleDependantThresholds'
    %                             statsFileName = '../../../../FeatureSets/singingsampleDependantThresholds.txt';
    %                             statsFileID = fopen( statsFileName, 'a' );
    %                             % is this the first sample in this emotion for
    %                             % this sample?
    %                             if( sampleNumber == 3 )
    %                                 if ( firstfileOpen == 0 )
    %                                     if(strfind( oldDirName , 'fem') > 0 )
    %                                         sampleName = oldDirName( 6 : end );
    %                                         meanET = mean( energyThreshold );
    %                                         meanSCT = mean( spectralCentroidThreshold );
    %                                         fprintf( statsFileID, '%s \t %f \t %f \n', sampleName, meanET, meanSCT );
    %                                     elseif(strfind( oldDirName , 'male') > 0 )
    %                                         sampleName = oldDirName( 6 : end );
    %                                         meanET = mean( energyThreshold );
    %                                         meanSCT = mean( spectralCentroidThreshold );
    %                                         fprintf( statsFileID, '%s \t %f \t %f \n', sampleName, meanET, meanSCT );
    %                                     end
    %                                 end
    %                                 
    %                                 spectralCentroidThreshold = [];
    %                                 energyThreshold = [];
    %                                 firstfileOpen = 0;
    %                             end
    %                             
    %                             [x, fs, frameLength, noOfFrames] = openFile( currentSampleName );
    %                             [st , et] = getsampleDependantThresholds( newDirName, x, fs, statsFileID, frameLength, noOfFrames );
    %                             spectralCentroidThreshold = [spectralCentroidThreshold st];
    %                             energyThreshold = [energyThreshold et];
    %                             oldDirName = newDirName;
    %   
    %                             fclose( statsFileID );
    %                         case 'getPitchStatistics'
    %                             statsHeaderFileName = '../../../../FeatureSets/singingHeaderPitchStats.txt';
    %                             statsHeaderFileID = fopen( statsHeaderFileName, 'w' );
    %                             if( firstfileOpen == 1 )
    %                                 fprintf( statsHeaderFileID, 'name \t unvoiced to voiced frame ratio \t mean voiced pitch \t median voiced pitch \t stdev voiced pitch \t variance voiced pitch \t min voiced pitch \t max voiced pitch \t range voiced pitch \t mean voiced pitch grad \t median voiced pitch grad \t stdev pitch grad \t variance voiced pitch grad \t min voiced pitch grad \t max voiced pitch grad \t range voiced pitch grad \n');
    %                                 firstfileOpen = 0;
    %                             end
    %                             fclose(statsHeaderFileID);
    %                             
    %                             statsFileName = '../../../../FeatureSets/singingPitchStats.txt';
    %                             statsFileID = fopen( statsFileName, 'a' );
    %                             
    %                             [x, fs, frameLength, noOfFrames] = openFile( currentSampleName );
    %                             getPitchStatistics( newDirName, x, fs, statsFileID, frameLength, noOfFrames );
    %                             fclose( statsFileID );
    %                         
    %                       
    %                              
    %                             
    %                                
    %                         %%%%%%%%%%%%%%% careful %%%%%%%%%%%%%%%    
    %                         case 'getHNRStatistics'
    %                             statsHeaderFileName = '../../../../FeatureSets/singingHeaderHNRStats.txt';
    %                             statsHeaderFileID = fopen( statsHeaderFileName, 'w' );
    %                             if( firstfileOpen == 1 )
    %                                 fprintf( statsHeaderFileID, 'name \t \t \t \t \t \n');
    %                                 firstfileOpen = 0;
    %                             end
    %                             fclose(statsHeaderFileID);
    %                             
    %                             statsFileName = '../../../../FeatureSets/singingHNRStats.txt';
    %                             statsFileID = fopen( statsFileName, 'a' );
    %                             
    %                             [x, fs, frameLength, noOfFrames] = openFile( currentSampleName );
    %                             getHNRStatistics( newDirName, x, fs, statsFileID, frameLength, noOfFrames );
    %                             fclose( statsFileID );
    %                         
    %                             
    %                             
    %                             
    %                         case 'getEnergyStatistics'
    %                             statsFileName = '../../../../FeatureSets/singingEnergyStats.txt';
    %                             statsFileID = fopen( statsFileName, 'a' );
    %                             if( firstfileOpen == 1 )
    %                                 fprintf( statsFileID, 'name \t mean energy \t mean voiced energy \t mean unvoiced energy \n');
    %                                 firstfileOpen = 0;
    %                             end
    %                             [x, fs, frameLength, noOfFrames] = openFile( currentSampleName );
    %                             getEnergyStatistics( newDirName, x, fs, statsFileID, frameLength, noOfFrames );
    %                             fclose( statsFileID );
    %                             
    %                             
    % %                         %%%%% for energy-related features %%%%%    
    % %                         case 'getOtherEnergyStatistics'
    % %                             statsFileName = '../../../../FeatureSets/singingOtherEnergyStats.txt';
    % %                             statsFileID = fopen( statsFileName, 'a' );
    % %                             if( firstfileOpen == 1 )
    % %                                 fprintf( statsFileID, 'name \t derivation of non silent energy \t derivation of voiced energy \t derivation of unvoiced energy \n');
    % %                                 firstfileOpen = 0;
    % %                             end
    % %                             [x, fs, frameLength, noOfFrames] = openFile( currentSampleName );
    % %                             getOtherEnergyStatistics( newDirName, x, fs, statsFileID, frameLength, noOfFrames );
    % %                             fclose( statsFileID );
    % 
    %                             
    %                             
    %                         case 'getBasicDescriptors'
    %                             % includes audio waveform and audio power
    %                             statsFileName = '../../../../FeatureSets/singingbasicDescriptors.txt';
    %                             statsFileID = fopen( statsFileName, 'a' );
    %                             if( firstfileOpen == 1 )
    %                                 fprintf( statsFileID, 'name \t meanAWF \t STDAWF \t minAWF \t maxAWF \t rangeAWF \t meanAP \t STDAP \t minAP \t maxAP \t rangeAP \t meanHR \t STDHR \t minHR \t maxHR \t rangeHR \n');
    %                                 firstfileOpen = 0;
    %                             end
    % 
    %                             getBasicDescriptors( newDirName, statsFileID );
    %                             fclose( statsFileID );
    %                         case 'getBasicSpectralDescriptors'
    %                             % includes audio spectrum envelope, audio spectrum
    %                             % centroid, audio spectrum spread, audio spectrum flatness
    %                              statsFileName = '../../../../FeatureSets/singingbasicSpectralDescriptors.txt';
    %                              statsFileID = fopen( statsFileName, 'a' );
    %                              % Add a header column
    %                             if( firstfileOpen == 1 )
    %                                  fprintf( statsFileID, ' name \t meanASE \t STDASE \t minASE \t maxASE \t rangeASE \t meanASC \t STDASC \t minASC \t maxASC \t rangeASC \t meanASS \t STDASS \t minASS \t maxASS \t rangeASS \t meanASF \t STDASF \t minASF \t maxASF \t rangeASF \n');
    %                                 % fprintf( statsFileID, ' name \t meanAWE \t STDAWE \t minAWE \t maxAWE \t rangeAWE \t meanAP \t STDAP \t minAP \t maxAP \t rangeAP \n');
    %                                  firstfileOpen = 0;
    %                              end
    %  
    %                              getBasicSpectralDescriptors1( newDirName, statsFileID );
    %                              fclose( statsFileID );
    %                             disp('NOT FINISHED');
    %                         case 'getTemporalTimbralDescriptors'
    %                             
    %                             % includes log attack time and temporal
    %                             % centroid
    %                             statsFileName = '../../../../FeatureSets/singingtemporalTimbralDescriptors.txt';
    %                             statsFileID = fopen( statsFileName, 'a' );
    %                             % Add a header column
    %                             if( firstfileOpen == 1)
    %                                 fprintf( statsFileID, ' name \t temporal centriod \t log attack time \n');
    %                                 firstfileOpen = 0;
    %                             end
    % 
    %                             getTemporalTimbralDescriptors( newDirName, statsFileID );
    %                             fclose( statsFileID );
    %                         case 'getSpectralTimbralDescriptors'
    %                             % collate the Harmonic Spectral Centroid,
    %                             % Harmonmic Spectral Deviation, Harmonic
    %                             % Spectral Spread, Harmonic Spectral Variation
    %                             % and Spectral Centroid
    %                             
    %                             statsFileName = '../../../../FeatureSets/singingspectralTimbralDescriptors.txt';
    %                             statsFileID = fopen( statsFileName, 'a' );
    %                             % Add a header column
    %                              if( firstfileOpen == 1)
    %                                  fprintf( statsFileID, ' name \t HSC \t HSD \t HSS \t HSV \t SC \n');
    %                                  firstfileOpen = 0;
    %                              end
    % 
    %                             getSpectralTimbralDescriptors( newDirName, statsFileID );
    %                             fclose( statsFileID );
    %                         case 'getMFCCs'
    %                             if( firstfileOpen )
    %                                 statsFileName = '../../../../FeatureSets/singingMFCCDescriptorsHeader.txt';
    %                                 statsFileID = fopen( statsFileName, 'w' );
    % 
    %                                 % Add a header file
    % %                                 putMFCCHeader( statsFileID );
    %                                 fclose( statsFileID );
    %                             end
    %                             
    %                             statsFileName = '../../../../FeatureSets/singingMFCCDescriptors.txt';
    %                             statsFileID = fopen( statsFileName, 'a' );
    %                         
    %                             getMFCCStatistics( newDirName, statsFileID );
    %                             fclose( statsFileID );
    %                        
    %                         %lin  
                            case 'getPRAAT'
                                % collate all Shimmer values                
                                statsFileName = '../../../../../../../TestResults/singingPRAATStats.txt';
                                
                                % Add a header row
                                if( firstfileOpen == 1 )
                                    statsFileID = fopen( statsFileName, 'w' );
                                    fprintf( statsFileID, 'Metrics calculated using the PRAAT software. \n' );
                                    fprintf( statsFileID, ' name \t JITTER: ddp \t local \t ppq5 \t rap \t SHIMMER: local \t dda \t apq3 \t apq5 \t apq11 \n');
                                    fprintf( statsFileID, 'For each of the BURG FORMANTS: numberOfFormants \t meanFrequency \t varFrequency \t minFrequency \t maxFrequency \n');
                                    fprintf( statsFileID, 'meanFrequencyDerivative \t varienceFrequencyDerivative \t minFrequencyDerivative \t max FrequencyDerivative \n');
                                    fprintf( statsFileID, 'meanFrequency2ndDerivative \t varienceFrequency2ndDerivative \t minFrequency2ndDerivative \t max Frequency2ndDerivative \n');
                                    fprintf( statsFileID, 'meanBandwidth \t varBandwidth \t minBandwidth \t maxBandwidth \n');
                                    fprintf( statsFileID, 'meanBandwidthDerivative \t varBandwidthDerivative \t minBandwidthDerivative \t maxBandwidthDerivative \n');
                                    fprintf( statsFileID, 'meanBandwidth2ndDerivative \t varBandwidth2ndDerivative \t minBandwidth2ndDerivative \t maxBandwidth2ndDerivative \n');
                                    fprintf( statsFileID, 'For the mean of all three formants: meanFrequency \t varFrequency \t minFrequency \t maxFrequency \n');
                                    fprintf( statsFileID, 'meanFrequencyDerivative \t varienceFrequencyDerivative \t minFrequencyDerivative \t max FrequencyDerivative \n');
                                    fprintf( statsFileID, 'meanFrequency2ndDerivative \t varienceFrequency2ndDerivative \t minFrequency2ndDerivative \t max Frequency2ndDerivative \n');
                                    firstfileOpen = 0;
                                else
                                    statsFileID = fopen( statsFileName, 'a' );
                                end

                                get_PRAAT( newDirName, statsFileID );
                                fclose( statsFileID );

    %                         case 'getJitter'
    %                             % collate Jitter                 
    %                             statsFileName = '../../../../FeatureSets/singingJitterStats.txt';
    %                             statsFileID = fopen( statsFileName, 'a' );
    %      
    %                             getJitter( newDirName, statsFileID );
    %                             fclose( statsFileID );       
    %                            
    % 
    %                                                         
    %                         %%%%% get all the pitch and energy related features
                            
                                
    %                         case 'getFeaturesOfDuration'
    %                             % collate number, mean and ratio duration of
    %                             % unvoiced and voiced sounds, median and 
    %                             % standard deviation number of voiced sounds
    %                             statsFileName = '../../../../FeatureSets/singingDurationStats.txt';
    %                             statsFileID = fopen( statsFileName, 'a' );
    %                             getFeaturesOfDuration( newDirName, statsFileID );
    %                             fclose( statsFileID );
    %                            
    %                         case 'getFeaturesOfRelativePitch'
    %                             % collate relative pitch maximum, minimum and
    %                             % the position of those pitches
    %                             statsFileName = '../../../../FeatureSets/singingRelativePitchStats.txt';
    %                             statsFileID = fopen( statsFileName, 'a' );
    %                             getFeaturesOfRelativePitch( newDirName, statsFileID );
    %                             fclose( statsFileID );
    %                             
    %                         case 'getFeaturesOfRelativeEnergyGradient'
    %                             % collate the value and position of relative
    %                             % maximum of energy gradient
    %                             statsFileName = '../../../../FeatureSets/singingRelativeEnergyGradientStats.txt';
    %                             statsFileID = fopen( statsFileName, 'a' );
    %                             getFeaturesOfRelativeEnergyGradient( newDirName, statsFileID );
    %                             fclose( statsFileID );
    %                                 
    %                             
    %                                                        
                            case 'calculatePitch'
                                detect_pitch( currentSampleName, OVERWRITE );
                                
                            case 'calculateSilence'
                                detect_silence( currentSampleName, OVERWRITE );
                                
                            case 'calculateVUV'
                                % find the voiced and unvoiced frames using
                                % the harmonic ratio and pitch information
                                detect_VoicedUnvoiced( currentSampleName, OVERWRITE );
                                
    %                         case 'calculateAudioWaveform'
    % 
    %                             [x, fs, frameLength, noOfFrames] = openFile( currentSampleName );
    %                             AW_Detection( currentSampleName, x, fs, frameLength, noOfFrames );
    %                             
    %                         case 'calculateAudioSpectrumEnvelope'
    %                             
    %                             [x, fs, frameLength, noOfFrames] = openFile( currentSampleName );
    %                             ASE_Detection( currentSampleName, x, fs, frameLength, noOfFrames );
    %                             
    %                         case 'calculateAudioSpectrumCentriodAndSpread'
    % 
    %                             [x, fs, frameLength, noOfFrames] = openFile( currentSampleName );
    %                             ASC_ASS_Detection( currentSampleName, x, fs, frameLength, noOfFrames );
    % 
    %                         case 'calculateAudioSpectrumBasisAndProjection'
    % 
    %                             [x, fs, frameLength, noOfFrames] = openFile( currentSampleName );
    %                             ASB_ASP_Detection( currentSampleName, x, fs, frameLength, noOfFrames );
    % 
                            case 'calculateAudioPower'
    
%                                 [x, fs, frameLength, noOfFrames] = openFile( currentSampleName );
                                detect_AudioPower( currentSampleName, x, fs, frameLength, noOfFrames );
    % 
    %                         case 'calculateLogAttackTIme'
    %                             [x, fs, frameLength, noOfFrames] = openFile( currentSampleName );
    %                             LAT_Detection( currentSampleName, x, fs, frameLength, noOfFrames );
    % 
    %                         case 'calculateTemporalCentroid'
    % 
    %                             [x, fs, frameLength, noOfFrames] = openFile( currentSampleName );
    %                             TC_Detection( currentSampleName, x, fs, frameLength, noOfFrames );
    % 
    %                         case 'calculateHarmonicRatio'
    %                             
    %                             % can't find this function yet
    %                             [x, fs, frameLength, noOfFrames] = openFile( currentSampleName );
    %                             HR_Detection( currentSampleName, x, fs, frameLength, noOfFrames );
    %                             
                            case 'calculateHarmonicNoiseRatio'
                                detect_HNR( currentSampleName, OVERWRITE );
    %                             
    %                         case 'calculateSpectralTimbralDescriptors'
    %                             
    %                             % these are best calculated together.
    %                             [x, fs, frameLength, noOfFrames] = openFile( currentSampleName );
    %                             spectralTimbralDescriptors( currentSampleName, x, fs, frameLength, noOfFrames );
                                
                            case 'calculateAudioSpectrumFlatness'
                                
                                [x, fs, frameLength, noOfFrames] = openFile( currentSampleName );
                                AudioSpectralFlatness_Detection( currentSampleName, x, fs, frameLength, noOfFrames );
                                
    %                         case 'calculateMFCCs'
    %                             
    %                             [x, fs, frameLength, noOfFrames] = openFile( currentSampleName );
    %                             MFCC_Detection( currentSampleName, x, fs, frameLength, noOfFrames );
    %                             
    %                         case 'calculatesampleDependantThresholds'
    %                             
    %                             [x, fs, frameLength, noOfFrames] = openFile( currentSampleName );
    %                             sampleDependantThresholds(  currentSampleName, x, fs, frameLength, noOfFrames );
    %                             
    %                             
    %                         %%%%% pitch related features %%%%%    
    %                         case 'calculatesampleVoicedSoundDuration'
    %                             
    %                             [x, fs, frameLength, noOfFrames] = openFile( currentSampleName );
    %                             getVoicedSoundDuration( currentSampleName, x, fs, noOfFrames, frameLength );
    %                             
    %                         case 'calculatesampleRelativePitch'
    %                             
    %                             singingpitchStatsFileName = 'singingpitchStats.txt';
    %                             singingpitchStatsFileID = fopen( singingpitchStatsFileName, 'r' );
    %                             [x, fs, frameLength, noOfFrames] = openFile( currentSampleName );
    %                             getRelativePitch( singingpitchStatsFileID, currentSampleName, x, fs, noOfFrames, frameLength, 7 );
    %                             fclose(singingpitchStatsFileID);
    % 
    %                         case 'calculatesampleRelativePitchFeatures'
    %                             
    %                             singingpitchStatsFileName1 = '../../../../FeatureSets/singingpitchStats.txt';
    %                             singingpitchStatsFileID1 = fopen( singingpitchStatsFileName1, 'r' );
    %                             singingpitchStatsFileName2 = 'singingpitchStats.txt';
    %                             singingpitchStatsFileID2 = fopen( singingpitchStatsFileName2, 'r' );
    %                             [averageValues1 averageValues2 averageValues3] = getRelativePitchFeatures( singingpitchStatsFileID1, 7 );
    %                             getEachRelativePitchFeature( singingpitchStatsFileID2, currentSampleName, 7, averageValues1, averageValues2, averageValues3 );
    %                             fclose(singingpitchStatsFileID1);
    %                             fclose(singingpitchStatsFileID2);
    %                             
    %                         case 'calculatesamplePitchPosition'
    %                             
    %                             singingpitchStatsFileName = 'singingpitchStats.txt';
    %                             singingpitchStatsFileID = fopen( singingpitchStatsFileName, 'r' );
    %                             [x, fs, frameLength, noOfFrames] = openFile( currentSampleName );
    %                             getPitchPosition( singingpitchStatsFileID, currentSampleName, x, fs, noOfFrames, frameLength, 7 );
    %                             fclose(singingpitchStatsFileID);
    %                           
    %                             
    %                             
    %                         %%%%% energy related features %%%%%
    %                         case 'calculatesampleRelativeEnergyByMeanAll'
    %                             
    %                             singingenergyStatsFileName = '../../../../FeatureSets/singingenergyStats.txt';
    %                             singingenergyhStatsFileID = fopen( singingenergyStatsFileName, 'r' );
    %                             [x, fs, frameLength, noOfFrames] = openFile( currentSampleName );
    %                             getRelativeEnergyByMeanAll( singingenergyhStatsFileID, currentSampleName, x, fs, noOfFrames, frameLength, 3 );
    %                             fclose(singingenergyhStatsFileID);
    %                          
    %                         case 'calculatesampleRelativeEnergyByMeanVoiced'
    %                             
    %                             singingenergyStatsFileName = '../../../../FeatureSets/singingenergyStats.txt';
    %                             singingenergyhStatsFileID = fopen( singingenergyStatsFileName, 'r' );
    %                             [x, fs, frameLength, noOfFrames] = openFile( currentSampleName );
    %                             getRelativeEnergy( singingenergyhStatsFileID, currentSampleName, x, fs, noOfFrames, frameLength, 3 );
    %                             fclose(singingenergyhStatsFileID);
    %                             
    %                         case 'calculatesampleRelativeEnergyGradientByMeanAll'
    %                             
    %                             statsFileName = '../../../../FeatureSets/singingenergyStats.txt';
    %                             statsFileID = fopen( statsFileName, 'r' );
    %                             [x, fs, frameLength, noOfFrames] = openFile( currentSampleName );
    %                             getRelativeEnergyGradient( statsFileID, newDirName, x, fs, noOfFrames, frameLength, 3 );
    %                             fclose( statsFileID );    
    %                             
    %                         case 'calculatesampleRelativeEnergyGradientFeaturesByMeanAll'
    %                             
    %                             [x, fs, frameLength, noOfFrames] = openFile( currentSampleName );
    %                             getRelativeEnergyGradientFeatures( currentSampleName, x, fs, noOfFrames, frameLength );
    %                             

                            end

                            cd ../
                        end
                    end

            end
            cd ../

        end
        cd ../
    end
    
end

cd ../

