package
{
????
import
com.adobe.audio.format.WAVWriter;
????
import
flash.display.Sprite;
????
import
flash.display.StageScaleMode;
????
import
flash.events.ErrorEvent;
????
import
flash.events.Event;
????
import
flash.events.KeyboardEvent;
????
import
flash.events.ProgressEvent;
????
import
flash.events.SampleDataEvent;
????
import
flash.media.Microphone;
????
import
flash.media.Sound;
????
import
flash.text.TextField;
????
import
flash.utils.ByteArray;
????
import
flash.utils.getTimer;
????
import
fr.kikko.lab.ShineMP3Encoder;
????
?
????
?????
?????
?????
????
public
class
Main
extends
Sprite
????
{
????????
private
var
_mic:Microphone;
????????
private
var
_voice:ByteArray;
????????
private
var
_mp3Encoder:ShineMP3Encoder;
????????
?
????????
private
var
_state:
String
;
????????
private
var
_timer:
int
;
????????
?
????????
?
????????
public
function
Main():
void
????????
{
????????????
if
(stage) init();
????????????
else
addEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
????????
}
????????
?
????????
private
function
onAddedToStage(e:Event):
void
????????
{
????????????
removeEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
????????????
init();
????????
}
????????
?
????????
private
function
init():
void
????????
{
????????????
stage.scaleMode = StageScaleMode.NO_SCALE;
????????????
stage.stageFocusRect =
false
;
????????????
?
????????????
if
(!setupMicrophone())
return
;
????????????
?
????????????
stage.addEventListener(KeyboardEvent.KEY_UP,commandHandler);
????????????
?
????????????
_state =
'pre-recording'
;
????????????
?
????????????
log(
'press <key>: <r>ecord'
);
????????
}
????????
?
????????
private
function
setupMicrophone():
Boolean
????????
{
????????????
_mic = Microphone.getMicrophone();
????????????
if
(!_mic) { log(
'no michrophone!'
);
return
false
; }
????????????
?
????????????
_mic.rate =
44
;
????????????
_mic.setSilenceLevel(
0
,
1000
);
????????????
_mic.setLoopBack(
true
);
????????????
_mic.setUseEchoSuppression(
true
);
????????????
return
true
;
????????
}
????????
?
????????
private
function
commandHandler(e:KeyboardEvent):
void
????????
{
????????????
switch
(
String
.fromCharCode(e.charCode))
????????????
{
????????????????
case
'r'
:
????????????????????
startRecording();
????????????????????
break
;
????????????????????
?
????????????????
case
'e'
:
????????????????????
stopAndEncodeRecording();
????????????????????
break
;
????????????????????
?
????????????????
case
's'
:
????????????????????
saveMP3();
????????????????????
break
;
????????????
}
????????
}
????????
?
????????
private
function
saveMP3():
void
????????
{
????????????
if
(_state !=
'encoded'
)
return
;
????????????
?
????????????
_mp3Encoder.saveAs();
????????????
_state =
'pre-recording'
;
????????
}
????????
?
????????
private
function
startRecording():
void
????????
{
????????????
if
(_state !=
'pre-recording'
&& _state !=
'encoded'
)
return
;
????????????
?
????????????
log(
'press <key>: stop and <e>ncode recording'
);
????????????
?
????????????
_state =
'pre-encoding'
;
????????????
_voice =
new
ByteArray();
????????????
_mic.addEventListener(SampleDataEvent.SAMPLE_DATA,onRecord);
????????
}
????????
?
????????
private
function
stopAndEncodeRecording():
void
????????
{
????????????
if
(_state !=
'pre-encoding'
)
return
;
????????????
?
????????????
_state =
'encoding'
;
????????????
_mic.removeEventListener(SampleDataEvent.SAMPLE_DATA,onRecord);
????????????
?
????????????
_voice.position =
0
;
????????????
?
????????????
log(
'encode start...synchronous convert to a WAV first'
);
????????????
?
????????????
convertToMP3();
????????
}
????????
?
????????
private
function
convertToMP3():
void
????????
{
????????????
var
wavWrite:WAVWriter =
new
WAVWriter();
????????????
wavWrite.numOfChannels =
1
;
????????????
wavWrite.sampleBitRate =
16
;
????????????
wavWrite.samplingRate =
44100
;
????????????
?
????????????
var
wav:ByteArray =
new
ByteArray();
????????????
?
????????????
_timer = getTimer();
????????????
wavWrite.processSamples(wav,_voice,
44100
,
1
);
????????????
log(
'convert to a WAV used: '
+ (getTimer() - _timer) +
'ms'
);
????????????
?
????????????
wav.position =
0
;
????????????
log(
'WAV size:'
+ wav.bytesAvailable +
' bytes'
);
????????????
log(
'Asynchronous convert to MP3 now'
);
????????????
?
????????????
_timer = getTimer();
????????????
_mp3Encoder =
new
ShineMP3Encoder( wav );
????????????
_mp3Encoder.addEventListener(Event.COMPLETE,onEncoded);
????????????
_mp3Encoder.addEventListener(ProgressEvent.PROGRESS,onEncoding);
????????????
_mp3Encoder.addEventListener(ErrorEvent.ERROR,onEncodeError);
????????????
_mp3Encoder.start();
????????
}
????????
?
????????
private
function
onEncoded(e:Event):
void
????????
{
????????????
_state =
'encoded'
;
????????????
log(
'encode MP3 complete used: '
+ (getTimer() - _timer) +
'ms'
);
????????????
_mp3Encoder.mp3Data.position =
0
;
????????????
log(
'MP3 size:'
+ _mp3Encoder.mp3Data.bytesAvailable +
' bytes'
);
????????????
log(
'press <key>: <s>ave to MP3 or <r>ecord again'
);
????????
}
????????
?
????????
private
function
onEncoding(e:ProgressEvent):
void
????????
{
????????????
log(
'encoding MP3... '
+
Number
(e.bytesLoaded / e.bytesTotal *
100
).toFixed(
2
) +
'%'
,
true
);
????????
}
????????
?
????????
private
function
onEncodeError(e:ErrorEvent):
void
????????
{
????????????
log(
'encode MP3 error '
+ e.text);
????????
}
????????
?
????????
private
function
onRecord(e:SampleDataEvent):
void
????????
{
????????????
_voice.writeBytes( e.data );
????????????
?
????????????
var
str:
String
=
''
;
????????????
for
(
var
i:
int
= _mic.activityLevel; i--;)
????????????
{
????????????????
str +=
'*'
;
????????????
}
????????????
log(
'recoding '
+ str,
true
);
????????
}
????????
?
????????
private
function
log(msg:
String
,updateInPlace:
Boolean
=
false
):
void
????????
{
????????????
var
txt:TextField = getChildByName(
'logTxt'
)
as
TextField;
????????????
if
(txt)
????????????
{
????????????????
if
(updateInPlace)
????????????????
{
????????????????????
if
(txt.text.substr( -
1
,
1
) ==
'r'
)
????????????????????
{
????????????????????????
txt.text = txt.text.substr(
0
,txt.text.lastIndexOf(
'r'
,txt.text.length -
2
) );
????????????????????
}
????????????????????
txt.appendText(
'n'
+ msg +
'n'
);
????????????????
}
????????????????
else
????????????????
{
????????????????????
txt.appendText((txt.text.substr(-
1
,
1
) ==
'r'
?
''
:
'n'
) + msg);
????????????????
}
????????????????
txt.scrollV = txt.maxScrollV;
????????????
}
????????????
else
????????????
{
????????????????
txt =
new
TextField();
????????????????
txt.name =
'logTxt'
;
????????????????
txt.multiline =
true
;
????????????????
txt.border =
true
;
????????????????
txt.borderColor =
0x666666
;
????????????????
txt.width = stage.stageWidth -
20
;
????????????????
txt.height = stage.stageHeight -
20
;
????????????????
txt.x = txt.y =
10
;
????????????????
txt.text = msg;
????????????????
addChild(txt);
????????????
}
????????
}
????
}
????
?
}