import javax.swing.JOptionPane;
import jm.util.*;
public class Project2
{
public static void main(String [] args)
{
String InputName1, InputName2, OutputName, start, end;
String menu =
"1. Mix Two Files.\n2. Loop a File\n3. Insert Silence Into a File\n4. Fade Part of a File\n5. Chop a File";
int MenuSelection = Integer.parseInt(JOptionPane.showInputDialog(null,menu));
if (MenuSelection == 1)
{
float [] d1,d2;
InputName1 = getFileName("first ");
InputName2 = getFileName("second ");
OutputName = getOutName();
d1 = Read.audio(InputName1);
d2 = Read.audio(InputName2);
float [] mixedData;
if (d1.length > d2.length)
mixedData = mix(d1, d2);
if (d2.length > d1.length)
mixedData = mix(d2, d1);
Write.audio(mixedData, OutputName);
}
else if (MenuSelection == 2)
{
InputName1 = getFileName("");
start = startLoc();
end = endLoc();
OutputName = getOutName();
times = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter the number of times you want to loop this file:"));
float [] data = Read.audio(InputName1);
float [] result = loop(InputName1, start, end, times);
Write.audio(result, OutputName);
}
else if (MenuSelection == 3)
{
InputName1 = getFileName("");
start = startLoc();
end = endLoc();
OutputName = getOutName();
float [] data = Read.audio(InputName1);
float Silence = insertSilence(data, start, end);
Write.audio(Silence, OutputName);
}
else if (MenuSelection == 4)
{
InputName1 = getFileName("");
start = startLoc();
end = endLoc();
OutputName = getOutName();
float FadeSize = float.parseFloat(JOptionPane.showInputDialogue(null,"Please enter the fade size:");//having trouble with this line
float [] data = Read.audio(InputName1);
float [] fadedData = Fade(data, start, end, FadeSize);
Write.audio(fadedData, OutputName);
}
else if (MenuSelection == 5)
{
InputName1 = getFileName("");
start = startLoc();
end = endLoc();
OutputName = getOutName();
float [] data = Read.audio(InputName1);
float [] choppedData = chop(data,start,end);
Write.audio(choppedData, OutputName);
}
}
public static String getFileName(String p)
{
String tempName = JOptionPane.showInputDialog(null,"Enter " + p + "file name:","File Name",3);
return tempName;
}
public static String getOutName()
{
String tempName = JOptionPane.showInputDialog(null,"Enter output file name:","File Name",3);
return tempName;
}
//And even more trouble with these two methods here.
public static float startLoc()
{
float temp = float.parseFloat(JOptionPane.showInputDialog(null,"Enter start time:","Start Time",3));
return temp;
}
public static float endLoc()
{
float temp = float.parseFloat(JOptionPane.showInputDialog(null,"Enter end time:","End Time",3));
return temp;
}
public static float [] mix(float [] longest, float [] shortest)
{
float [] tempData = new float[longest.length];
for (int i = 0; i < shortest.length; i++)
tempData[i] = (longest[i] + shortest [i]) * 0.5f;
for (int i = shortest.length; i < longest.length; i++)
tempData[i] = longest[i] * 0.5f;
return tempData;
}
public static float [] loop(float [] data, int startL, int endL, int repeats)
{
int length = endL - startL;
float [] tempData = new float[length * repeats];
for (int i = 0; i < length; i++)
tempData[i] = data[startLocation + i];
for (int i = 0; i < repeats; i++)
{
for (int j = 0; j < length; j++)
tempdata[j + i * length] = tempdata[j];
}
return tempData;
}
public static float [] insertSilence(float [] data, int startL, int endL);
{
int length = endL - startL;
for (int i = 0; i < length; i++)
data[startL + i] = 0.0f;
return data;
}
public static float [] chop(float [] data, int start, int end)
{
int length = end - start;
float [] tempData = new float[length];
for (int i = 0; i < length; i ++)
tempData[i] = data[start + i];
return tempData;
}
public static float [] Fade(float [] data , int startL, int endL, int fadeSize)
{
int length = endL - startL;
float [] tempData = new float[length];
for(int i = 0; i < length; i++)
tempData[i] = data[startL + 1];
for(int i = 0; i < fadesize; i++)
tempData[i] *= (float)i/(float)fadesize;
return tempData;
}
}