Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bufferSizeMs parameter to recorder interface #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ public void run() {
Integer sampleRate ,
Integer numChannels ,
Integer bitRate ,
Integer bufferSizeMs ,
String path ,
t_AUDIO_SOURCE _audioSource ,
boolean toStream
Expand All @@ -278,7 +279,7 @@ public void run() {
}
try
{
recorder._startRecorder( numChannels, sampleRate, bitRate, codec, path, audioSource, this );
recorder._startRecorder( numChannels, sampleRate, bitRate, bufferSizeMs, codec, path, audioSource, this);
if (subsDurationMillis > 0)
setTimer(subsDurationMillis);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class FlautoRecorderEngine
FlautoRecorder session = null;
FileOutputStream outputStream = null;
final private Handler mainHandler = new Handler (Looper.getMainLooper ());
private ByteBuffer buffer = null;



Expand Down Expand Up @@ -136,33 +137,57 @@ int writeData(int bufferSize)
int r = 0;
while (isRecording ) {
//ShortBuffer shortBuffer = ShortBuffer.allocate(bufferSize/2);
ByteBuffer byteBuffer = ByteBuffer.allocate(bufferSize);
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(bufferSize);
try {
// gets the voice output from microphone to byte format
if ( Build.VERSION.SDK_INT >= 23 )
{
n = recorder.read(byteBuffer.array(), 0, bufferSize, AudioRecord.READ_NON_BLOCKING);
n = recorder.read(byteBuffer, bufferSize, AudioRecord.READ_NON_BLOCKING);

}
else
{
n = recorder.read(byteBuffer.array(), 0, bufferSize);
n = recorder.read(byteBuffer, bufferSize);
}
final int ln = n;//2 * n;
byteBuffer.position(byteBuffer.position() + n);

if (n > 0) {
totalBytes += n;
r += n;
if (outputStream != null) {
outputStream.write(byteBuffer.array(), 0, ln);
outputStream.write(byteBuffer.array(), 0, n);
} else {
mainHandler.post(new Runnable() {
@Override
public void run() {

session.recordingData(Arrays.copyOfRange(byteBuffer.array(), 0, ln));
if (n < buffer.remaining()) {
byteBuffer.flip();
buffer.put(byteBuffer);
} else {
int rem = buffer.remaining();
// Fill remaining internal buffer with data
{
byteBuffer.flip();
int oldLimit = byteBuffer.limit();
assert oldLimit == n;
byteBuffer.limit(rem);
buffer.put(byteBuffer);
assert byteBuffer.position() == rem;
byteBuffer.limit(oldLimit);
}
});
// Create copy of the internal buffer
buffer.flip();
byte[] bufferToSend = new byte[buffer.limit()];
buffer.get(bufferToSend);
// Clear the internal buffer and put remaining new bytes
buffer.clear();
buffer.put(byteBuffer);
assert buffer.position() == (n - rem);
// Send full buffer
mainHandler.post(new Runnable() {
@Override
public void run() {
session.recordingData(bufferToSend);
}
});
}
}
for (int i = 0; i < n / 2; ++i) {
short curSample = getShort(byteBuffer.array()[i * 2], byteBuffer.array()[i * 2 + 1]);
Expand Down Expand Up @@ -194,6 +219,7 @@ public void run() {
Integer numChannels,
Integer sampleRate,
Integer bitRate,
Integer bufferSizeMs,
t_CODEC theCodec,
String path,
int audioSource,
Expand All @@ -207,25 +233,27 @@ public void run() {
codec = theCodec;
int channelConfig = (numChannels == 1) ? AudioFormat.CHANNEL_IN_MONO : AudioFormat.CHANNEL_IN_STEREO;
int audioFormat = tabCodec[codec.ordinal()];
int bufferSize = AudioRecord.getMinBufferSize
int minBufferSize = AudioRecord.getMinBufferSize
(
sampleRate,
channelConfig,
channelConfig,
tabCodec[codec.ordinal()]
) * 2;
);
int requestedBufferSize = (bufferSizeMs * sampleRate / 1000) * 2; // Assume 16bit PCM
int bufferSize = Integer.max(minBufferSize, requestedBufferSize);


recorder = new AudioRecord( audioSource,
sampleRate,
channelConfig,
audioFormat,
bufferSize
);
recorder = new AudioRecord(audioSource,
sampleRate,
channelConfig,
audioFormat,
bufferSize
);

if (recorder.getState() == AudioRecord.STATE_INITIALIZED)
{
recorder.startRecording();
isRecording = true;
buffer = ByteBuffer.allocateDirect(bufferSize);
try {
writeAudioDataToFile(codec, sampleRate, path);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public interface FlautoRecorderInterface
Integer numChannels,
Integer sampleRate,
Integer bitRate,
Integer bufferSizeMs,
t_CODEC codec,
String path,
int audioSource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public boolean CheckPermissions()
Integer numChannels,
Integer sampleRate,
Integer bitRate,
Integer bufferSizeMs,
t_CODEC codec,
String path,
int audioSource,
Expand Down
3 changes: 2 additions & 1 deletion ios/Classes/FlautoRecorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
toPath: (NSString*)path
channels: (int)numChannels
sampleRate: (long)sampleRate
bitRate: (long)bitRate;
bitRate: (long)bitRate
bufferSizeMs: (int)bufferSizeMs;

- (void)stopRecorder;
- (void)setSubscriptionDuration: (long)millisec;
Expand Down
2 changes: 2 additions & 0 deletions ios/Classes/FlautoRecorder.mm
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,13 @@ - (bool)startRecorderCodec: (t_CODEC)codec
channels: (int)numChannels
sampleRate: (long)sampleRate
bitRate: (long)bitRate
bufferSizeMs: (int)bufferSizeMs
{
NSMutableDictionary* audioSettings = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithLong: sampleRate], AVSampleRateKey,
[NSNumber numberWithInt: formats[codec] ], AVFormatIDKey,
[NSNumber numberWithInt: numChannels ], AVNumberOfChannelsKey,
[NSNumber numberWithInt: bufferSizeMs ], @"bufferSizeMs",
nil];

// If bitrate is defined, we use it, otherwise use the OS default
Expand Down
6 changes: 4 additions & 2 deletions ios/Classes/FlautoRecorderEngine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@

AVAudioInputNode* inputNode = [engine inputNode];
AVAudioFormat* inputFormat = [inputNode outputFormatForBus: 0];
NSNumber* bufferSizeMs = audioSettings [@"bufferSizeMs"];
double samplePerMs = [inputFormat sampleRate] / 1000.0;
unsigned int bufferSize = (unsigned int)(samplePerMs * [bufferSizeMs doubleValue]);
double sRate = [inputFormat sampleRate];
// -AVAudioChannelCount channelCount = [inputFormat channelCount];
AVAudioChannelLayout* layout = [inputFormat channelLayout];
Expand All @@ -59,7 +62,6 @@

NSNumber* nbChannels = audioSettings [AVNumberOfChannelsKey];
NSNumber* sampleRate = audioSettings [AVSampleRateKey];
//sampleRate = [NSNumber numberWithInt: 44000];
AVAudioFormat* recordingFormat = [[AVAudioFormat alloc] initWithCommonFormat: AVAudioPCMFormatInt16 sampleRate: sampleRate.doubleValue channels: (unsigned int)(nbChannels.unsignedIntegerValue) interleaved: YES];
AVAudioConverter* converter = [[AVAudioConverter alloc]initFromFormat: inputFormat toFormat: recordingFormat];
NSFileManager* fileManager = [NSFileManager defaultManager];
Expand All @@ -76,7 +78,7 @@
}


[inputNode installTapOnBus: 0 bufferSize: 20480 format: inputFormat block:
[inputNode installTapOnBus: 0 bufferSize: bufferSize format: inputFormat block:
^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when)
{
inputStatus = AVAudioConverterInputStatus_HaveData ;
Expand Down