Hi,
you're using wrong AVFormatIDKey value. The correct one for PCM is 1819304813 (your one is MPEG4AAC). Here's the working code for AWS Lex & Pythonista (I did install awscli & boto3 via pip via StaSh).
from objc_util import *
import boto3
import os
import sound
import console
import uuid
def record(file_name):
AVAudioSession = ObjCClass('AVAudioSession')
NSURL = ObjCClass('NSURL')
AVAudioRecorder = ObjCClass('AVAudioRecorder')
shared_session = AVAudioSession.sharedInstance()
category_set = shared_session.setCategory_error_(ns('AVAudioSessionCategoryPlayAndRecord'), None)
settings = {
ns('AVFormatIDKey'): ns(1819304813),
ns('AVSampleRateKey'):ns(16000.0),
ns('AVNumberOfChannelsKey'):ns(1),
ns('AVLinearPCMBitDepthKey'):ns(16),
ns('AVLinearPCMIsFloatKey'):ns(False),
ns('AVLinearPCMIsBigEndianKey'):ns(False)
}
output_path = os.path.abspath(file_name)
out_url = NSURL.fileURLWithPath_(ns(output_path))
recorder = AVAudioRecorder.alloc().initWithURL_settings_error_(out_url, settings, None)
if recorder is None:
console.alert('Failed to initialize recorder')
return None
started_recording = recorder.record()
if started_recording:
print('Recording started, press the "stop script" button to end recording...')
try:
while True:
pass
except KeyboardInterrupt:
print('Stopping...')
recorder.stop()
recorder.release()
print('Stopped recording.')
return output_path
def main():
console.clear()
path = record("{}.pcm".format(uuid.uuid4().hex))
if path is None:
print('Nothing recorded')
return
sound.play_effect(path)
recording = open(path, 'rb')
session = boto3.Session(profile_name='lex')
client = session.client('lex-runtime')
r = client.post_content(botName='BookTrip', botAlias='$LATEST', userId=uuid.uuid4().hex,
contentType='audio/l16; rate=16000; channels=1',
accept='text/plain; charset=utf-8',
inputStream=recording)
print(r)
os.remove(path)
if __name__ == '__main__':
main()
And here's the console output when I said book a car.
Recording started, press the "stop script" button to end recording...
Stopping...
Stopped recording.
{'slots': {'PickUpDate': None, 'DriverAge': None, 'ReturnDate': None, 'PickUpCity': None, 'CarType': None}, 'intentName': 'BookCar', 'slotToElicit': 'PickUpCity', 'dialogState': 'ElicitSlot', 'ResponseMetadata': {'HTTPStatusCode': 200, 'RetryAttempts': 0, 'HTTPHeaders': {'x-amzn-requestid': '33a2a3f2-6c5b-11e7-a3f3-d1ffbce58259', 'connection': 'keep-alive', 'x-amz-lex-slots': 'eyJQaWNrVXBEYXRlIjpudWxsLCJSZXR1cm5EYXRlIjpudWxsLCJEcml2ZXJBZ2UiOm51bGwsIkNhclR5cGUiOm51bGwsIlBpY2tVcENpdHkiOm51bGx9', 'date': 'Wed, 19 Jul 2017 08:21:02 GMT', 'x-amz-lex-input-transcript': 'book a car', 'content-length': '0', 'x-amz-lex-message': 'In what city do you need to rent a car?', 'content-type': 'text/plain;charset=utf-8', 'x-amz-lex-intent-name': 'BookCar', 'x-amz-lex-slot-to-elicit': 'PickUpCity', 'x-amz-lex-dialog-state': 'ElicitSlot'}, 'RequestId': '33a2a3f2-6c5b-11e7-a3f3-d1ffbce58259'}, 'contentType': 'text/plain;charset=utf-8', 'message': 'In what city do you need to rent a car?', 'inputTranscript': 'book a car', 'audioStream': <botocore.response.StreamingBody object at 0x108a4d4a8>}
Lex predefined BookTrip bot is used in this case.
HTH,
Zrzka