Skip to content

Commit

Permalink
[iOS, macOS] Fix AvAudioSession Defaults (#1513)
Browse files Browse the repository at this point in the history
* Add Support for `AVAudioSession`

* Update SpeechToTextViewModel.cs

* Remove macOS AVAudioSession logic, Add `AVAudioSession.SetSupportsMultichannelContent(true, out _)`

* Add `void InitializeAvAudioSession()`
  • Loading branch information
brminnick authored Nov 13, 2023
1 parent 339f7e3 commit e72da77
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,24 @@ async Task SetLocales(CancellationToken token)
[RelayCommand]
async Task Play(CancellationToken cancellationToken)
{
await textToSpeech.SpeakAsync(RecognitionText ?? "Welcome to .NET MAUI Community Toolkit!", new()
var timeoutCancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(5));

try
{
await textToSpeech.SpeakAsync(RecognitionText ?? "Welcome to .NET MAUI Community Toolkit!", new()
{
Locale = CurrentLocale,
Pitch = 1,
Volume = 1
}, cancellationToken).WaitAsync(timeoutCancellationTokenSource.Token);
}
catch (TaskCanceledException)
{
Locale = CurrentLocale,
Pitch = 2,
Volume = 1
}, cancellationToken);
await Toast.Make("Playback automatically stopped after 5 seconds").Show(cancellationToken);
#if IOS
await Toast.Make("If you did not hear playback, test again on a physical iOS device").Show(cancellationToken);
#endif
}
}

[RelayCommand(IncludeCancelCommand = true, CanExecute = nameof(CanListenExecute))]
Expand All @@ -89,24 +101,24 @@ async Task Listen(CancellationToken cancellationToken)
RecognitionText = beginSpeakingPrompt;

var recognitionResult = await speechToText.ListenAsync(
CultureInfo.GetCultureInfo(CurrentLocale?.Language ?? defaultLanguage),
new Progress<string>(partialText =>
{
if (RecognitionText is beginSpeakingPrompt)
{
RecognitionText = string.Empty;
}
CultureInfo.GetCultureInfo(CurrentLocale?.Language ?? defaultLanguage),
new Progress<string>(partialText =>
{
if (RecognitionText is beginSpeakingPrompt)
{
RecognitionText = string.Empty;
}
RecognitionText += partialText + " ";
}), cancellationToken);
RecognitionText += partialText + " ";
}), cancellationToken);

if (recognitionResult.IsSuccessful)
{
RecognitionText = recognitionResult.Text;
}
else
{
await Toast.Make(recognitionResult.Exception?.Message ?? "Unable to recognize speech").Show(cancellationToken);
await Toast.Make(recognitionResult.Exception?.Message ?? "Unable to recognize speech").Show(CancellationToken.None);
}

if (RecognitionText is beginSpeakingPrompt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,24 @@ static Task<bool> IsSpeechPermissionAuthorized(CancellationToken cancellationTok
return Task.FromResult(SFSpeechRecognizer.AuthorizationStatus is SFSpeechRecognizerAuthorizationStatus.Authorized);
}

static void InitializeAvAudioSession(out AVAudioSession sharedAvAudioSession)
{
sharedAvAudioSession = AVAudioSession.SharedInstance();
if (UIDevice.CurrentDevice.CheckSystemVersion(15, 0))
{
sharedAvAudioSession.SetSupportsMultichannelContent(true, out _);
}

sharedAvAudioSession.SetCategory(AVAudioSessionCategory.PlayAndRecord, AVAudioSessionCategoryOptions.DefaultToSpeaker);
}

async Task<string> InternalListenAsync(CultureInfo culture, IProgress<string>? recognitionResult, CancellationToken cancellationToken)
{
recognitionProgress = recognitionResult;

await InternalStartListeningAsync(culture, cancellationToken);

return await getRecognitionTaskCompletionSource.Task.WaitAsync((cancellationToken));
return await getRecognitionTaskCompletionSource.Task.WaitAsync(cancellationToken);
}

void StopRecording()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@ Task InternalStartListeningAsync(CultureInfo culture, CancellationToken cancella
throw new ArgumentException("Speech recognizer is not available");
}

if (UIDevice.CurrentDevice.CheckSystemVersion(15, 0))
{
AVAudioSession.SharedInstance().SetSupportsMultichannelContent(true, out _);
}

audioEngine = new AVAudioEngine();
liveSpeechRequest = new SFSpeechAudioBufferRecognitionRequest();

InitializeAvAudioSession(out _);

var node = audioEngine.InputNode;
var recordingFormat = node.GetBusOutputFormat(0);
Expand All @@ -40,8 +37,9 @@ Task InternalStartListeningAsync(CultureInfo culture, CancellationToken cancella
throw new ArgumentException("Error starting audio engine - " + error.LocalizedDescription);
}

var currentIndex = 0;
cancellationToken.ThrowIfCancellationRequested();

var currentIndex = 0;
recognitionTask = speechRecognizer.GetRecognitionTask(liveSpeechRequest, (result, err) =>
{
if (err is not null)
Expand Down Expand Up @@ -76,8 +74,6 @@ Task InternalStartListeningAsync(CultureInfo culture, CancellationToken cancella
}
});

cancellationToken.ThrowIfCancellationRequested();

return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,8 @@ Task InternalStartListeningAsync(CultureInfo culture, CancellationToken cancella

audioEngine = new AVAudioEngine();
liveSpeechRequest = new SFSpeechAudioBufferRecognitionRequest();
var audioSession = AVAudioSession.SharedInstance();

if (UIDevice.CurrentDevice.CheckSystemVersion(15, 0))
{
audioSession.SetSupportsMultichannelContent(true, out _);
}

audioSession.SetCategory(AVAudioSessionCategory.Record, AVAudioSessionCategoryOptions.DefaultToSpeaker);

InitializeAvAudioSession(out var audioSession);

var mode = audioSession.AvailableModes.Contains(AVAudioSession.ModeMeasurement)
? AVAudioSession.ModeMeasurement
Expand Down Expand Up @@ -58,9 +52,10 @@ Task InternalStartListeningAsync(CultureInfo culture, CancellationToken cancella
{
throw new Exception(error.LocalizedDescription);
}

cancellationToken.ThrowIfCancellationRequested();

var currentIndex = 0;

recognitionTask = speechRecognizer.GetRecognitionTask(liveSpeechRequest, (result, err) =>
{
if (err is not null)
Expand Down Expand Up @@ -95,8 +90,6 @@ Task InternalStartListeningAsync(CultureInfo culture, CancellationToken cancella
}
});

cancellationToken.ThrowIfCancellationRequested();

return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public async Task<SpeechToTextResult> ListenAsync(CultureInfo culture, IProgress
{
try
{
cancellationToken.ThrowIfCancellationRequested();
var isPermissionGranted = await IsSpeechPermissionAuthorized(cancellationToken).ConfigureAwait(false);
if (!isPermissionGranted)
{
Expand Down

0 comments on commit e72da77

Please sign in to comment.