X
xysawq
Gast
Hallo,
Entschuldigung, dass ich so knapp vor dem Wochenende eine frage habe, aber ich dachte ich bekomme dieses Problem diese Woche noch in den Griff, leider war dem nicht so.
Ich bekomme bei meinem (noch) kleinen Programm ständig den Fehler, das eine Conversion von einem AudioInputStream in einen anderen AudioInputStream mit anderem Format nicht möglich ist.
Error:
Nach einer Woche Sucherei auf Google und einem (meiner Meinung nach gut gelungenen) Lösungsweg bin ich keinen Schritt weiter, da diese Umwandlung eben einfach nicht funktionieren will.
Selbst wenn ich als Umwandlungs-Format das selbe wie das Ursprungs-Format nehme (also targetFormat und sourceFormat sind beide stream.getFormat() ) will er nicht, deshalb wird wohl der Fehler irgendwo im Programmierdetail versteckt sein, und ich lese da die ganze Zeit drüber hinweg.
Wie auch immer, hier habt ihr erstmal den Code, ich habe sogar ein paar kleine Kommentare hinzugefügt, damit ihr nicht erst rätseln müsst, was wofür zu gebrauchen ist, aber auch so ist es noch recht übersichtlich:
Viel Glück, und schonmal danke im Vorraus an die, die sich das überhaupt durchlesen
.
MfG, xysawq.
Ach ja, für die die es interessiert: Die sample.wav die ich benutze ist die prog_01c.wav aus diesem Archiv:
http://www.cse.dmu.ac.uk/~mward/sample-wav-files.zip
Entschuldigung, dass ich so knapp vor dem Wochenende eine frage habe, aber ich dachte ich bekomme dieses Problem diese Woche noch in den Griff, leider war dem nicht so.
Ich bekomme bei meinem (noch) kleinen Programm ständig den Fehler, das eine Conversion von einem AudioInputStream in einen anderen AudioInputStream mit anderem Format nicht möglich ist.
Error:
Code:
Exception in thread "main" java.lang.IllegalArgumentException: Unsupported conversion: PCM_SIGNED 8000.0 Hz, 8 bit, mono, 10 bytes/frame, from PCM_SIGNED 6000.0 Hz, 8 bit, stereo, 2 bytes/frame,
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at testframe.WaveFileHandler.createStream(WaveFileHandler.java:116)
at testframe.WaveFileHandler.main(WaveFileHandler.java:156)
Nach einer Woche Sucherei auf Google und einem (meiner Meinung nach gut gelungenen) Lösungsweg bin ich keinen Schritt weiter, da diese Umwandlung eben einfach nicht funktionieren will.
Selbst wenn ich als Umwandlungs-Format das selbe wie das Ursprungs-Format nehme (also targetFormat und sourceFormat sind beide stream.getFormat() ) will er nicht, deshalb wird wohl der Fehler irgendwo im Programmierdetail versteckt sein, und ich lese da die ganze Zeit drüber hinweg.
Wie auch immer, hier habt ihr erstmal den Code, ich habe sogar ein paar kleine Kommentare hinzugefügt, damit ihr nicht erst rätseln müsst, was wofür zu gebrauchen ist, aber auch so ist es noch recht übersichtlich:
Code:
import java.io.*;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.spi.FormatConversionProvider;
public class WaveFileHandler
{
private static AudioInputStream wavFileInputStream = null;
private static AudioInputStream formatedStream = null;
private static String testWavFile = "D:/temp/sample.wav";
//easy to use audio format creator
private static AudioFormat getAudioFormat()
{
AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
float sampleRate = 8000.0F;
int sampleSizeInBits = 8;
int channels = 1;
int frameSize = 10;
int frameRate = 8000;
boolean bigEndian = false;
return new AudioFormat(encoding,
sampleRate,
sampleSizeInBits,
channels,
frameSize,
frameRate,
bigEndian);
}
public static void readFile(String file)
{
//if no specific file is given (null) the test file is used
if(file == null)
{
try
{
wavFileInputStream = AudioSystem.getAudioInputStream(new File(testWavFile));
}
catch (UnsupportedAudioFileException e)
{
System.err.println("Audio file '" + testWavFile + "' is not a valid file or file type...");
}
catch (IOException e)
{
System.err.println("IOException while loading '" + testWavFile + "'...");
}
}
//if a file is given it is used
else
{
try
{
wavFileInputStream = AudioSystem.getAudioInputStream(new File(file));
}
catch (UnsupportedAudioFileException e)
{
System.err.println("Audio file '" + file + "' is not a valid file or file type...");
}
catch (IOException e)
{
System.err.println("IOException while loading '" + file + "'...");
}
}
}
public static void createStream()
{
//the target format is taken from the format creator
AudioFormat targetFormat = getAudioFormat();
//this is for debugging, so i can see the differences in the formats
System.out.println(wavFileInputStream.getFormat().getEncoding());
System.out.println(wavFileInputStream.getFormat().getSampleRate());
System.out.println(wavFileInputStream.getFormat().getSampleSizeInBits());
System.out.println(wavFileInputStream.getFormat().getChannels());
System.out.println(wavFileInputStream.getFormat().getFrameSize());
System.out.println(wavFileInputStream.getFormat().getFrameRate());
System.out.println(wavFileInputStream.getFormat().isBigEndian());
System.out.println("\n" + wavFileInputStream.getFormat().toString());
System.out.println("\n" + getAudioFormat().getEncoding());
System.out.println(getAudioFormat().getSampleRate());
System.out.println(getAudioFormat().getSampleSizeInBits());
System.out.println(getAudioFormat().getChannels());
System.out.println(getAudioFormat().getFrameSize());
System.out.println(getAudioFormat().getFrameRate());
System.out.println(getAudioFormat().isBigEndian());
System.out.println("\n" + getAudioFormat().toString());
//another debugging line to see if the change of encoding is possible, if not is is tried anyway
if(!AudioSystem.isConversionSupported(AudioFormat.Encoding.PCM_SIGNED, wavFileInputStream.getFormat()))
{
System.err.println("Audio stream can not be encoded...");
}
//creating a temporary stream (it's a debugging thing... when removed the same error happens)
AudioInputStream temp = null;
//change of encoding
temp = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED, wavFileInputStream);
//now i try to format the already new encoded stream
if(!AudioSystem.isConversionSupported(targetFormat, temp.getFormat()))
{
System.err.println("Audio stream can not be formated...");
}
//if it says it cant be formated it is tried anyway
formatedStream = AudioSystem.getAudioInputStream(targetFormat, temp);
//that is done for later resetting, don't mention this ;)
formatedStream.mark((int) (formatedStream.getFrameLength() * getAudioFormat().getFrameSize() + 1));
//if everything (would) be done without errors i could check it here
System.out.println("\n" + formatedStream.getFormat().getEncoding());
System.out.println(formatedStream.getFormat().getSampleRate());
System.out.println(formatedStream.getFormat().getSampleSizeInBits());
System.out.println(formatedStream.getFormat().getChannels());
System.out.println(formatedStream.getFormat().getFrameSize());
System.out.println(formatedStream.getFormat().getFrameRate());
System.out.println(formatedStream.getFormat().isBigEndian());
System.out.println("\n" + formatedStream.getFormat().toString());
}
//for external usage
public static AudioInputStream getStream()
{
return formatedStream;
}
//resetting... not really working
public static void resetStream()
{
try
{
formatedStream.reset();
}
catch (IOException e)
{
System.err.println("The Audio Stream could not be resetted ---> " + e);
}
}
//my little test
public static void main(String[] args)
{
readFile(null);
createStream();
byte[] temp1 = new byte[3];
int length = 0;
try
{
length = getStream().read(temp1, 0, 3);
System.out.println(length);
}
catch (IOException e)
{
System.err.println("IOException while reading several bytes from stream ---> " + e);
}
resetStream();
byte[] temp2 = new byte[3];
try
{
length = getStream().read(temp2, 0, 3);
System.out.println(length);
}
catch (IOException e)
{
System.err.println("IOException while reading several bytes from stream the 2nd time ---> " + e);
}
//if there is still a mistake hidden somewhere...
if(temp1 == temp2)
{
System.out.println("Everything worked...");
}
else
{
System.out.println("There has to be a mistake somewhere...");
}
}
}
Viel Glück, und schonmal danke im Vorraus an die, die sich das überhaupt durchlesen
MfG, xysawq.
Ach ja, für die die es interessiert: Die sample.wav die ich benutze ist die prog_01c.wav aus diesem Archiv:
http://www.cse.dmu.ac.uk/~mward/sample-wav-files.zip