/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.xtx.ut4converter.tools;

import com.sun.media.sound.WaveFileWriter;
import java.io.File;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioFileFormat.Type;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioFormat.Encoding;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

/**
 * 
 * @author XtremeXp
 */
public  class SoundConverter {
    
    final Logger logger;
    
    /**
     * 
     * @param logger Logger used in map converter
     */
    public SoundConverter(Logger logger){
        this.logger = logger;
    }
    
    /**
     * Get possible format allowed to convert to 16 bits
     * @param srcAudioFormat Audio format of source wave file that need to be converted to 16 bits
     * @return 
     */
    private synchronized AudioFormat getAudioFormat16bit(AudioFormat srcAudioFormat){
        
        for(Encoding encoding : AudioSystem.getTargetEncodings(srcAudioFormat.getEncoding())){
                
            for(AudioFormat audioFormat : AudioSystem.getTargetFormats(encoding, srcAudioFormat)){
                if(audioFormat.getSampleSizeInBits() == 16){
                    return audioFormat;
                } 
            }
        }
        
        return null;
    }
    
    /**
     * Converts .wav file to 16 bit sample size file.
     * UE4 editor does not support 8 bit sample size sounds unlike UE1, UE2, UE3 (?)
     * @param inWaveFile Sound file to be converted
     * @param outWaveFile Converted sound file
     */
    public synchronized void convertTo16BitSampleSize(File inWaveFile, File outWaveFile){
        
        AudioInputStream audioInputStream;
        AudioFormat srcAudioFormat;
        

        try {
            audioInputStream = AudioSystem.getAudioInputStream(inWaveFile);
            srcAudioFormat = audioInputStream.getFormat();
            
            // SampleSize ever 16 bits, just do a file copy
            if(srcAudioFormat.getSampleSizeInBits() == 16){
                Files.copy(inWaveFile.toPath(), outWaveFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
                return;
            }

            AudioFormat dstAudioFormat = getAudioFormat16bit(srcAudioFormat);
            
            if(dstAudioFormat == null){
                logger.log(Level.WARNING, "No sound conversion available for "+inWaveFile.getName());
                return;
            }
            
            AudioInputStream convertedIn = AudioSystem.getAudioInputStream(dstAudioFormat, audioInputStream);
            
            logger.info("Converting "+inWaveFile.getName()+" sound to 44.1 Khz / 16 bit");
            
            WaveFileWriter writer = new WaveFileWriter();
            writer.write(convertedIn, Type.WAVE, outWaveFile);

        } catch (Exception e){
            e.printStackTrace();
            logger.log(Level.SEVERE, "Error while converting sound file "+inWaveFile.getName());
        }
            
    }
}

@@ -262,24 +262,26 @@
 ileWriter;%0D%0A
+%0D%0A
 import java.
@@ -286,24 +286,53 @@
 a.io.File;%0D%0A
+import java.io.IOException;%0D%0A
 import java.
@@ -491,16 +491,18 @@
 ogger;%0D%0A
+%0D%0A
 import j
@@ -2094,17 +2094,80 @@
 utStream
-;
+ = null;%0D%0A        AudioInputStream convertedIn = null;%0D%0A        
 %0D%0A      
@@ -2951,33 +2951,16 @@
         
-AudioInputStream 
 converte
@@ -3326,42 +3326,514 @@
     
-e.printStackTrace();%0D%0A            
+logger.log(Level.SEVERE, %22Error while converting sound file %22+inWaveFile.getName(), e);%0D%0A        %7D %0D%0A        %0D%0A        finally %7B%0D%0A        %09%0D%0A        %09if(audioInputStream != null)%7B%0D%0A        %09%09try %7B%0D%0A%09%09%09%09%09audioInputStream.close();%0D%0A%09%09%09%09%7D catch (IOException e) %7B%0D%0A%09%09%09%09%09logger.log(Level.SEVERE, %22Error while converting sound file %22+inWaveFile.getName(), e);%0D%0A%09%09%09%09%7D%0D%0A        %09%7D%0D%0A        %09%0D%0A        %09if(convertedIn != null)%7B%0D%0A        %09%09try %7B%0D%0A        %09%09%09convertedIn.close();%0D%0A%09%09%09%09%7D catch (IOException e) %7B%0D%0A%09%09%09%09%09
 logg
@@ -3902,34 +3902,56 @@
 veFile.getName()
-);
+, e);%0D%0A%09%09%09%09%7D%0D%0A        %09%7D
 %0D%0A        %7D%0D%0A   

fix some ressources not automatically closed/freed after sound conversion