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

/**
     * @param ais
     * @param player
     * @param playSamples 经过测试 设置为1000比较合适,此时dropSamples设置为1000表示2x,dropSamples设置为2000表示3x,dropSamples设置为500表示1.5x,
     * @param dropSamples
     * @throws IOException
     */
    private static void handleCase2_fast_normal(AudioInputStream ais, SourceDataLine player,int playSamples,int dropSamples) throws IOException {
        System.out.println( "case2:" );
        int len;
        byte[] buf = new byte[4];
        boolean play = true;
        int step_play = playSamples;
        int step_drop = dropSamples;
        int count_play = 0;
        int count_drop = 0;
        int i=0;
        long t = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        while((len=ais.read(buf))!=-1) {
            i++;
            if( i < 2152526 ){
                continue;
            }
            if( i >= 3360000 ){
                break;
            }
            if( play ){
                //左声道
                buf[1] = ( byte ) ( buf[1] );
                //右声道
                buf[3] = ( byte ) ( buf[3] );
                player.write(buf, 0, len);
                count_play++;
                // System.out.println( "play play = " + play + ",count_play = " + count_play );
                if( count_play >= step_play ){
                    play = false;
                    count_play = 0;
                }
            }else {
                // do nothing
                count_drop++;
                // System.out.println( "          drop play = " + play + ",count_drop = " + count_drop );
                if( count_drop >= step_drop ){
                    play = true;
                    count_drop = 0;
                }
            }
            if( i % 80000==0 ){
                System.out.println( sdf.format( new Date()) );
            }
        }
        System.out.println( "step_play = " + step_play + ",step_drop = " + step_drop + " 时耗时:" + ( System.currentTimeMillis() - t ) + "毫秒" );
    }

主要是播放一小段采样,再丢弃一小段采样,实现倍速播放的同时,声音没有变尖锐的感觉,更接近本来的音色,更符合正常人类加速说话的效果

猜你喜欢

转载自blog.csdn.net/heshiyuan1406146854/article/details/130489903