flash - 在 Adob​​e AIR/Actionscript 3.0 中录制 WAV - **问题**

标签 flash actionscript-3 audio air wav

我正在尝试从我的耳机端口直接录制到 Adob​​e Flash Builder/AIR 中的麦克风端口(使用辅助电缆),但我遇到了几个问题:

1. 与我在 iTunes 中播放的原始 MP3 文件相比,录音听起来永远不会“完整”。 PCM应该是一种未压缩的格式,所以质量应该是无损的。

2. 保存的录音(WAV)总是我录音的一半。
(例如:如果我录制一分钟,我会得到一个 0:30 的 wav 文件,而后半部分的录音不存在)

这是我正在使用的代码。任何想法为什么我可能会遇到这些问题?

import com.adobe.audio.format.WAVWriter;

import flash.display.NativeWindowSystemChrome;
import flash.events.Event;
import flash.events.SampleDataEvent;
import flash.events.TimerEvent;
import flash.filters.BitmapFilterQuality;
import flash.filters.DropShadowFilter;
import flash.media.Microphone;
import flash.media.Sound;
import flash.system.Capabilities;
import flash.utils.Timer;

import mx.controls.Alert;
import mx.core.UIComponent;
import mx.core.Window;
import mx.events.CloseEvent;

import ui.AudioVisualization;

private var shadowFilter:DropShadowFilter;
private var newWindow:Window;

// MICROPHONE STUFFZ
[Bindable] private var microphoneList:Array = Microphone.names; // Set up list of microphones
protected var microphone:Microphone;                            // Initialize Microphone
protected var isRecording:Boolean = false;                      // Variable to check if we're recording or not
protected var micRecording:ByteArray;                           // Variable to store recorded audio data

public var file:File = File.desktopDirectory;
public var stream:FileStream = new FileStream();

public var i:int = 0;
public var myTimer:Timer;


// [Start] Recording Function
protected function startMicRecording():void
{
    if(isRecording == true) stopMicRecording();
    else
    {
        consoleTA.text += "\nRecording started...";
        consoleTA.scrollToRange(int.MAX_VALUE, int.MAX_VALUE);

        isRecording = true;
        micRecording = new ByteArray();
        microphone = Microphone.getMicrophone(comboMicList.selectedIndex);
        microphone.gain = 50;
        microphone.rate = 44;      
        microphone.setUseEchoSuppression(false); 
        microphone.setLoopBack(false);  

        // Start timer to measure duration of audio clip (runs every 1 seconds)
        myTimer = new Timer(1000);
        myTimer.start();

        // Set amount of time required to register silence
        var userSetSilence:int;
        if(splitCB.selected == true){
            userSetSilence = splitNS.value; // if checkbox is checked, use the value from the numeric stepper
        }
        else{
            userSetSilence = 2;
        }
        userSetSilence *= 100;
        microphone.setSilenceLevel(0.5, userSetSilence); // 2 seconds of silence = Register silence with onActivity (works for itunes skip)
        microphone.addEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData);
        microphone.addEventListener(ActivityEvent.ACTIVITY, this.onMicActivity);
    }
}

// [Stop] Recording Function
protected function stopMicRecording():void
{
    myTimer.stop(); // Stop timer to get final audio clip duration

    consoleTA.text += "\nRecording stopped. (" + myTimer.currentCount + "s)";
    consoleTA.scrollToRange(int.MAX_VALUE, int.MAX_VALUE); 

    isRecording = false;
    if(!microphone) return;
    microphone.removeEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData);
}

private function gotMicData(micData:SampleDataEvent):void
{
    this.visualization.drawMicBar(microphone.activityLevel,0xFF0000);
    if(microphone.activityLevel <= 5) 
    {
        consoleTA.text += "\nNo audio detected"; //trace("no music playing");
        consoleTA.scrollToRange(int.MAX_VALUE, int.MAX_VALUE); 
    }
    // micData.data contains a ByteArray with our sample. 
    //Old: micRecording.writeBytes(micData.data);
    while(micData.data.bytesAvailable) { 
        var sample:Number = micData.data.readFloat(); 
        micRecording.writeFloat(sample); 
    }
}

protected function onMicActivity(event:ActivityEvent):void
{
    //trace("activating=" + event.activating + ", activityLevel=" + microphone.activityLevel);
    consoleTA.text += "\nactivating=" + event.activating + ", activityLevel=" + microphone.activityLevel;
    consoleTA.scrollToRange(int.MAX_VALUE, int.MAX_VALUE); 

    // Mic started recording...
    if(event.activating == true)
    {
        try{
            //fs.open(file, FileMode.WRITE);
            //fs.writ
        }catch(e:Error){
            trace(e.message);
        }
    }
    // Mic stopped recording...
    if(event.activating == false)
    {
        if(file)
        {
            i++;
            myTimer.stop();
            stopMicRecording();

            if(deleteCB.selected == true)
            {

                if(myTimer.currentCount < deleteNS.value)
                {
                    consoleTA.text += "\nAudio deleted. (Reason: Too short)";
                    consoleTA.scrollToRange(int.MAX_VALUE, int.MAX_VALUE); 
                }
                else
                {
                    writeWav(i);
                }
            }
            else
            {
                writeWav(i);
            }

            startMicRecording();
        }
    }

}

private function save():void
{
    consoleTA.text += "\nSaving..."; //trace("file saved!");
    consoleTA.scrollToRange(int.MAX_VALUE, int.MAX_VALUE); 

    file = new File( );
    file.browseForSave( "Save your wav" );
    file.addEventListener( Event.SELECT, writeWav );
}

public function writeWav(i:int):void
{
    var wavWriter:WAVWriter = new WAVWriter();

    // Set settings
    micRecording.position = 0;
    wavWriter.numOfChannels = 2;
    wavWriter.sampleBitRate = 16; //Audio sample bit rate: 8, 16, 24, 32
    wavWriter.samplingRate = 44100;   

    var file:File = File.desktopDirectory.resolvePath("SoundSlug Recordings/"+sessionTA.text+"_"+i+".wav");
    var stream:FileStream = new FileStream();
    //file = file.resolvePath("/SoundSlug Recordings/testFile.wav");
    stream.open( file, FileMode.WRITE );

    // convert ByteArray to WAV
    wavWriter.processSamples( stream, micRecording, 44100, 1 ); //change to 1?
    stream.close();

    consoleTA.text += "\nFile Saved: " + file.exists; //trace("saved: " + file.exists);
    consoleTA.scrollToRange(int.MAX_VALUE, int.MAX_VALUE);
}      

PS:我使用的是标准的 WavWriter AS 类:http://ghostcat.googlecode.com/svn-history/r424/trunk/ghostcatfp10/src/ghostcat/media/WAVWriter.as

最佳答案

是的,耳机到麦克风是问题。您仅通过耳机插孔发送一小部分数据。它已经过净化、重组、减少或节流,以使聆听体验不会中断(一个关键的体验因素)。记录下来会给你一堆泥巴。为了录制完美的音频,您必须使用音频数据本身,而不是依赖于(真诚地)朝着完美数字再现之外的其他目标努力的程序的输出。

不过,不知道为什么音频只有一半。

关于flash - 在 Adob​​e AIR/Actionscript 3.0 中录制 WAV - **问题**,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8766169/

相关文章:

android - 在全屏模式下如何将 Android 平板电脑浏览器 Flash 电影锁定为横向

java - 在Java中播放音频文件

android - APK 50meg+ 扩展包和 Flash

actionscript-3 - AS3 从外部 SWF 实例化类

actionscript-3 - Flash AS3 - URLLoader 无法加载来自不同域的二进制数据

actionscript-3 - AS3-我的声音播放器似乎运作良好,但声音不存在

android - 从Android上的 Assets 读取音频文件

audio - FFmpeg 库 : Muxing audio from external file

JavaScript 调用 AS3 错误

jquery - 当文本在 flash 或 jquery 谷歌地图上滚动时,固定 div 中的文本字体粗细会发生变化,为什么以及如何处理它?