jmusic project 2- shane barriscale

Upload: shanbarr

Post on 06-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 JMusic Project 2- Shane Barriscale

    1/7

    //Name: Shane Barriscale

    //Class: IMedia 2011//JMUSIC Project 2- Editing Audio Files Using a Menu Driven Interface (******Version 2 using showOptionDialog*****)//Student: Number 0540641

    import javax.swing.JOptionPane;//Import Java Tools for JOptionPaneimport jm.util.*;//Import Java Tools for audio

    publicclass JM22{

    //| ||| //\\ || |||\ ||

    //|| ||||| // \\ || ||||| || // || || || ///||\\\ || ||| || ||

    // || || || // \\ || ||| || || // ||| || // \\ || ||| ||||

    publicstaticvoid main(String [] args)//Declaring main method. Void, so returns no data{String inputFilename1, inputFilename2, outputFilename1;//Declaring three variables of String data typeint start, end, repeats, fade;//Declaring four variables of integer data typefloat [] inData1, inData2, outData1;//Declaring three values of float data type

    String[] opts = {"Quit","Cut and Fade","Cut and Loop","Cut","Insert Silence","Mix"};//Declaring String options for the/////////////////////////////////////////////////////////////////////////////////////////upcoming showOptionDialog box

    int select = JOptionPane.showOptionDialog (null,//Centre upcoming text

    "\n\nChoose an audio manipulation from the list below\n\n\n"////////////////////////////////////////////|\\\\\\\\\+ "Mix______________________Blend two tracks together and save to a new file\n\n"///////////////////////|\\\\\\\\\+ "Insert Silence_____________Add a silence to part of an existing track and save to a new file\n\n" ////|\\\\\\\\\+ "Cut______________________Cut a section out of an existing track and save to a new file\n\n" //Message Box Text\\+ "Cut and Loop_____________Cut a section out of an existing track, loop and save to a new file\n\n" ////|/////////+ "Cut and Fade In/Out_______Cut a section out of an existing track, "//////////////////////////////////|/////////+ "apply a fade in/fade out and save to a new file\n\n\n",//////////////////////////////////////////////|///////// "Shane Barriscale_JMusic Project 2",//Message box title

    0,//Set to 0. Not required for showOptionDialogJOptionPane.QUESTION_MESSAGE,//Icon type

    null,//Icon positionopts,//String options declared aboveopts[0]);//Highlight option button 'Quit'

  • 8/3/2019 JMusic Project 2- Shane Barriscale

    2/7

    //MIX SELECTION//////////////////////////////////if (select == 5)

    {inputFilename1 = getFilename("first audio ");//String inputFilename1 is replaced using the GetFilename methodinputFilename2 = getFilename("second audio ");//String inputFilename1 is replaced using the GetFilename method

    outputFilename1 = getFilename("output ");//String outputFilename is replaced using the GetFilename method

    inData1 = Read.audio(inputFilename1);//Add data from inputFilename1 to float inData1

    inData2 = Read.audio(inputFilename2);//Add data from inputFilename2 to float inData2if (inData1.length > inData2.length)//Based on the lenght of inputs, the order they are mixed in is determined

    outData1 = mix(inData1, inData2);//Blend the two data using the mix method(defined below) else

    outData1 = mix(inData2, inData1);//Blend the two data using the mix method(defined below)Write.audio(outData1, outputFilename1);//Write the mixed data to an output file, specified by the GetFilename method

    }

    //SILENCE SELECTION/////////////////elseif (select == 4){

    inputFilename1 = getFilename("");//String inputFilename1 is replaced using the GetFilename method below

    start = getVariable("start time in seconds ");//Integer start is replaced using the GetVariable method belowend = getVariable("end time in seconds ");//Integer end is replaced using the GetVariable method below

    outputFilename1 = getFilename("output ");//String outputFilename is replaced using the GetFilename method

    inData1 = Read.audio(inputFilename1);//Add data from inputFilename1 to float inData1outData1 = silence(inData1, start*44100, end*44100);//Add silence to data using getVariable integers

    //////////////////////////////////////////////////////Multiply by 44100 to convert from Hz to seconds

    Write.audio(outData1, outputFilename1);//Write the mixed data to an output file, specified by the GetFilename method}

    //CUT SELECTION/////////////////////elseif (select == 3){

    inputFilename1 = getFilename("");//String inputFilename1 is replaced using the GetFilename method below

    start = getVariable("start time in seconds ");//Integer start is replaced using the GetVariable method belowend = getVariable("end time in seconds");//Integer end is replaced using the GetVariable method below

    outputFilename1 = getFilename("output ");//String outputFilename is replaced using the GetFilename method

    inData1 = Read.audio(inputFilename1);//Add data from inputFilename1 to float inData1outData1 = cut(inData1, start*44100, end*44100);//Cut a section of track using getVariable values

    //////////////////////////////////////////////////Multiply by 44100 to convert from Hz to seconds

    Write.audio(outData1, outputFilename1);//Write the cut data to an output file, specified by the GetFilename method}

  • 8/3/2019 JMusic Project 2- Shane Barriscale

    3/7

    //CUT AND LOOP SELECTION////////////elseif (select == 2)

    {inputFilename1 = getFilename("");//String inputFilename1 is replaced using GetFilename method below

    start = getVariable("start time in seconds ");//Integer start is replaced using GetVariable method belowend = getVariable("end time in seconds ");//Integer end is replaced using GetVariable method belowrepeats = getVariable("number of repeats ");//Integer repeats is replaced using GetVariable method below

    outputFilename1 = getFilename("output ");//String outputFilename is replaced using GetFilename method

    inData1 = Read.audio(inputFilename1);//Add data from inputFilename1 to float inData1outData1 = loop(inData1, start*44100, end*44100, repeats);//Cut and loop a section of track using getVariable values

    ////////////////////////////////////////////////////////////Multiply by 44100 to convert from Hz to seconds

    Write.audio(outData1, outputFilename1);//Write the loop data to an output file, specified by the GetFilename method}

    //CUT AND FADE SELECTION////////////elseif (select == 1){

    inputFilename1 = getFilename("");//String inputFilename1 is replaced using GetFilename method below

    start = getVariable("start time in seconds");//Integer start is replaced using GetVariable method belowend = getVariable("end time in seconds ");//Integer end is replaced using GetVariable method belowfade = getVariable("fade in/fade out time in seconds ");//Integer fade is replaced using GetVariable method below

    outputFilename1 = getFilename("output ");//String outputFilename is replaced using GetFilename method

    inData1 = Read.audio(inputFilename1);//Add data from inputFilename1 to float inData1outData1 = fadedistance(inData1, start*44100, end*44100, fade*44100);//Cut a section of the track and fade using

    /////////////////////////////////////////////////////////////////////////getVariable values. Multiply by 44100 to////////////////////////////////////////////////////////////////////////////convert from Hz to seconds

    Write.audio(outData1, outputFilename1);//Write the fade data to an output file, specified by the GetFilename method}

    //EXIT MENU SELECTION////////////elseif (select == 0)

    {System.exit(0);//Exit the program without doing anything}

    }

  • 8/3/2019 JMusic Project 2- Shane Barriscale

    4/7

    //|||\\ ||||||| |||||||||| ||||||| || || ||||||| |||\ || //\\ ||| ||| |||||||

    /// || || || || || || ||||| || // \\ |||| |||| || // |||| |||||| || |||||| || || |||||| ||| || || ///||\\\ || || || || ||||||

    /// || || || || || || || ||| || || // \\ || || || || || //||||| ||||||| || || || ||||||| ||||||| ||| |||| // \\ || ||| || |||||||

    publicstatic String getFilename(String partialMessageText)//Takes the information in getFilename brackets above

    /////and declares it String type patialMessageText{String messageText = "Enter " + partialMessageText + "filename";//Prepares message to the user in the inputDialog boxString filename;//declares the users typed filename as a String called filenamefilename = JOptionPane.showInputDialog(null, messageText);//creates a JOptionPane for the information above//////////////////////////////////////////////////////////////that includes String message text and input String filename

    return filename;//return filename back to the getFilename in the main method}

    //|||\\ ||||||| |||||||||| \\ // //\\ ||||\\ || //\\ |||||| || |||||||

    /// || || \\ // // \\ || || || // \\ || || || || // |||| |||||| || \\ // ///||\\\ |||||// || ///||\\\ |||||| || ||||||

    /// || || || \\ // // \\ || \\ || // \\ || || || || //||||| ||||||| || \||/ // \\ || \\ || // \\ |||||| ||||||| |||||||

    publicstaticint getVariable(String partialMessageText)//Takes the information in getVariable brackets above

    /////and declares it String type patialMessageText{

    String messageText = "Enter " + partialMessageText;//Prepares message to the user in the inputDialog boxint number;//declares the users typed number as a Integer called numbernumber = Integer.parseInt(JOptionPane.showInputDialog(null, messageText));//creates a JOptionPane for the information///////////////////////////////////////////////////////////////////////////////above that includes String message text///////////////////////////////////////////////////////////////////////////////////and input Integer number, using/////////////////////////////////////////////////////////////////////////////////////Integer.parseInt to allow an Integer///////////////////////////////////////////////////////////////////////////////////////// to be used in the JOptionPanereturn number;}

  • 8/3/2019 JMusic Project 2- Shane Barriscale

    5/7

    //| ||| || || ||

    //|| ||||| || || || // || || || || ||

    // || || || || || ||// ||| || || || ||

    publicstaticfloat[] mix(float [] longest, float[] shortest)//mix method including both data brought down

    { float [] tempData = newfloat[longest.length];//Creates a tempData float array to hold input data

    ////////////////////////////////////////////////It's set to the longer data length so that when mixed, both will fit

    for (int i = 0; i < shortest.length; i++)//Process and iterates i from 0 to the value of the lenght of the shortest array{tempData[i] = (longest[i] + shortest[i]) * 0.5f;//Adds the longest array(as far as the lenght of the shortest array)

    ////////////////////////////////////////////////////and the full shortest array to tempData [i]. Multiplied by 0.5//////////////////////////////////////////////////////to bring the combined levels back to a normal range, since the/////////////////////////////////////////////////////////mixed tracks have double values for everything

    }

    for (int i = shortest.length; i < longest.length; i++)//Processes i from the end of the shortest array to the value//////////////////////////////////////////////////////////of the lenght of the longest array

    tempData[i] = longest[i] * 0.5f;//Adds the updated longest array to the tempData [i], from the value of the shortest////////////////////////////////////lenght, as defined in the second for loop

    return tempData;//Returns tempData array back to the mix method within the main method}

    //||||\\ || || ||||||| ||| || //||||\ ||||||| //| || || || ||||| || /// \ || //||||\\ || || |||||| ||| || || ||| |||||| /// || || || ||| || || \\\ / || //|||||/ || ||||||| ||||||| ||| |||| \\||||/ |||||||

    publicstaticfloat[] silence(float [] inData1, int start, int end)

    { int length = end - start;//Creates an integer of the lenght of the input end time - the start time for (int i = 0; i < length; i++)//Processes and iterates i from 0 to the value of lenght above

    {inData1[start + i] = 0.0f;//Adds the i value to inData1 from the value of the start time input by the user

    //////////////////////////////Multiplied by 0 to create an area of silence the lenght of i}

    return inData1;//Returns inData1 array to the silence method within the main method.///////////////////Not tempData since the full file is being used}

  • 8/3/2019 JMusic Project 2- Shane Barriscale

    6/7

    //|||\\ || || ||||||||

    // \ || || || // || || || // / || || || //||||/ \||||||/ ||

    publicstaticfloat[] cut(float [] inData1, int start, int end)

    { int length = end - start;//Creates an integer of the lenght of the input end time - the start time float [] tempData =newfloat[length];//Creates a tempData float array to hold input data to the value of lenght

    for (int i = 0; i < length; i++)//Processes and iterates i from 0 to the value of length above{tempData[i] = inData1[start + i];//Populates the tempData array from the start time declared above to the value of i

    } return tempData;//Returns tempData array back to the cut method within the main method

    }

    // /|||\ /|||\ |||||\ // /| |\ /| |\ || || // || || || || |||||/ // \| |/ \| |/ || //||||| \|||/ \|||/ ||

    publicstaticfloat[] loop(float [] inData1, int start, int end, int repeats)

    { int length = end - start;//Creates an integer of the lenght of the input end time - the start time

    float [] tempData = newfloat[length * repeats];//Creates a tempData float array to hold input data ////////////////////////////////////////////////////to the value of lenght multiply by repeats. This sets the //////////////////////////////////////////////////////array to the full value of the repeated inData1

    for (int i = 0; i < length; i++)//Processes and iterates i from 0 to the value of lenght above.{tempData[i] = inData1[start + i];//This only fills the the first part of the tempData array, since it is declared as

    /////////////////////////////////////being lenght*repeats, so it is repeat-times-longer than the data just processed}

    for (int i = 1; i < repeats; i++)//Processes and iterates i from 1 to the value of repeats input by the user{

    for(int j = 0; j < length; j++)//Each time the above iteration happens, j iterates and fills each subsequent repeat ///////////////////////////////////lenght in the full tempData array

    {tempData[j + i * length] = tempData[j];//Tells the tempData array where to place each repeated j increment in each

    ///////////////////////////////////////////subsequent repeat increment}}

    return tempData;//Returns tempData array back to the loop method within the main method}

  • 8/3/2019 JMusic Project 2- Shane Barriscale

    7/7

    //||||| //\\ |||||\ |||||||

    // // \\ || ||\ || //|||| ///||\\\ || || |||||| // // \\ || ||/ || // // \\ |||||/ |||||||

    publicstaticfloat[] fadedistance(float [] inData1, int start, int end, int fade)

    { int length = end - start;//Creates an integer of the lenght of the input end time - the start time float [] tempData = newfloat[length];//Creates a tempData float array to hold input data to the value of lenght

    for (int i = 0; i < length; i++)//Processes and iterates i from 0 to the value of lenght above{tempData[i] = inData1[start + i];//Populates the tempData array from the start time declared above to the value of i

    } for (int i = 0; i < fade; i++)//Processes and iterates i from 0 to the value of fade input by the user

    {tempData[i] *= (float)i / (float)fade;//This formula creates a fade in. When i increments, i/fade becomes

    ///////////////////////////////////////// a bigger number, so multiplying the two together creates a fade in ////////////////////////////////////////////i and fade are temporarily cast as float type to stop Java from rounding down

    } for (int i = 0; i < fade; i++)//Processes and iterates i from 0 to the value of fade input by the user again

    {tempData[tempData.length - i -1] *= (float)i / (float)fade;//This formula creates a fade out. When i increments, i/fade///////////////////////////////////////////////////////////////becomes a bigger number. This time, the tempData lenght

    //////////////////////////////////////////////////////////////////goes backwards because of minus i. So each i increment ///////////////////////////////////////////////////////////////////// i/fade becomes a bigger number, this time going ////////////////////////////////////////////////////////////////////////backwards from the end, so ,multiplying the two ///////////////////////////////////////////////////////////////////////////creates a fade out

    } return tempData;//Returns tempData array back to the fadedistance method within the main method}

    }