java 使用 SourceDataLine 倍速播放音频( 音调变尖 )

/**
     * @param ais
     * @param player
     * @param speed 支持 1、2、3
     * @throws IOException
     */ 
private static void handleCase2_fast_sharp(AudioInputStream ais, SourceDataLine player,int speed) throws IOException {
        System.out.println( speed +"x速播放" );
        System.out.println( "case2:" );
        int len;
        byte[] buf = new byte[4];
        int i=0;
        while((len=ais.read(buf))!=-1) {
            i++;
            if( i < 2152526 ){
                continue;
            }
            if( i >= 3360000 ){
                break;
            }
            if( i %speed==0 ){
                //左声道
                buf[1] = ( byte ) ( buf[1] );
                //右声道
                buf[3] = ( byte ) ( buf[3] );
                player.write(buf, 0, len);
            }
        }
    }

主要是播放一个采样、丢弃一个采样,效果就是实现倍速播放了,但是声音也变尖了,丢失了本来的音色,不符合正常人类加速说话的效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
mport java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import javax.sound.sampled.AudioFileFormat; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Control; import javax.sound.sampled.DataLine; import javax.sound.sampled.FloatControl; import javax.sound.sampled.Line; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.Mixer; import javax.sound.sampled.SourceDataLine; import javax.sound.sampled.UnsupportedAudioFileException; import javazoom.spi.PropertiesContainer; import org.tritonus.share.sampled.TAudioFormat; import org.tritonus.share.sampled.file.TAudioFileFormat; import static com.hadeslee.yoyoplayer.player.BasicPlayerEvent.*; /** * BasicPlayer is a threaded simple player class based on JavaSound API. * It has been successfully tested under J2SE 1.3.x, 1.4.x and 1.5.x. */ @SuppressWarnings("unchecked") public class BasicPlayer implements BasicController, Runnable { public static int EXTERNAL_BUFFER_SIZE = 4000 * 4; public static int SKIP_INACCURACY_SIZE = 512; protected Thread m_thread = null; protected Object m_dataSource; protected AudioInputStream m_encodedaudioInputStream; protected int encodedLength = -1; protected AudioInputStream m_audioInputStream; protected AudioFileFormat m_audioFileFormat; protected SourceDataLine m_line; protected FloatControl m_gainControl; protected FloatControl m_panControl; protected String m_mixerName = null; private int m_lineCurrentBufferSize = -1; private int lineBufferSize = -1; private long threadSleep = -1; private static Logger log = Logger.getLogger(BasicPlayer.class.getName()); /** * These variables are used to distinguish stopped, paused, playing states. * We need them to control Thread. */ // public static final int UNKNOWN = -1; // public static final int PLAYING = 0; // public static final int PAUSED = 1; // public static final int STOPPED = 2; // public static final int OPENED = 3; // public static final int SEEKING = 4; private int m_status = UNKNOWN; private Map empty_map = new HashMap(); private BasicPlayerEventLauncher laucher;//浜嬩欢鍒嗘淳鍣? /** * Constructs a Basic Player. */ public BasicPlayer() { m_dataSource = null; laucher = new BasicPlayerEventLauncher(); laucher.start(); reset(); } protected void reset() { m_status = UNKNOWN; if (m_audioInputStream != null) { synchronized (m_audioInputStream) { closeStream(); } } m_audioInputStream = null; m_audioFileFormat = null; m_encodedaudioInputStream = null; encodedLength = -1; if (m_line != null) { m_line.stop(); m_line.close(); m_line = null; } m_gainControl = null; m_panControl = null; } /** * Add listener to be notified. * @param bpl */ public void addBasicPlayerListener(BasicPlayerListener bpl) { laucher.addBasicPlayerListener(bpl); } /** * Return registered listeners. * @return */ public Collection getListeners() { return laucher.getBasicPlayerListeners(); } /** * Remove registered listener. * @param bpl */ public void removeBasicPlayerListener(BasicPlayerListener bpl) { laucher.removeBasicPlayerListener(bpl); } /** * Set SourceDataLine buffer size. It affects audio latency. * (the delay between line.write(data) and real sound). * Minimum value should be over 10000 bytes. * @param size -1 means maximum buffer size available. */ public void setLineBufferSize(int size) { lineBufferSize = size; } /** * Return SourceDataLine buffer size. * @return -1 maximum buffer size. */ public int getLineBufferSize() { return lineBufferSize; } /** * Return SourceDataLine current buffer size. * @return */ public int getLineCurrentBufferSize() { return m_lineCurrentBufferSize; } /** * Set thread sleep time. * Default is -1 (no sleep time). * @param time in milliseconds. */ public void setSleepTime(long time) { threadSleep = time; } /** * Return thread sleep time in milliseconds. * @return -1 means no sleep time. */ public long getSleepTime() { return threadSleep; } /** * Returns BasicPlayer status. * @return status */ public int getStatus() { return m_status; } /** * Open file to play. */ public void open(File file) throws BasicPlayerException { log.info("open(" + file + ")"); if (file != null) { m_dataSource = file; initAudioInputStream(); } } /** * Open URL to play. */ public void open(URL url) throws BasicPlayerException { log.info("open(" + url + ")"); if (url != null) { m_dataSource = url; initAudioInputStream(); } } /** * Open inputstream to play. */ public void open(InputStream inputStream) throws BasicPlayerException { log.info("open(" + inputStream + ")"); if (inputStream != null) { m_dataSource = inputStream; initAudioInputStream(); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值