Skip to content

Commit

Permalink
Fix collecting both location and acoustic indicators flows simultaneo…
Browse files Browse the repository at this point in the history
…usly
  • Loading branch information
tonelli-m committed Dec 2, 2024
1 parent cfd7483 commit 729e298
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ class DefaultLiveAudioService(
override val isRunning: Boolean
get() = _isRunningFlow.value

override val audioSourceState: Flow<AudioSourceState> = audioSource.stateFlow
override val audioSourceStateFlow: Flow<AudioSourceState> = audioSource.stateFlow

override fun setupAudioSource(startWhenReady: Boolean) {
logger.debug("start when ready: $startWhenReady")
override fun setupAudioSource() {
// Create a job that will process incoming audio samples in a background thread
audioJob = coroutineScope.launch(Dispatchers.Default) {
audioSource.audioSamples
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ interface LiveAudioService {
* State of the underlying audio source.
* Can be used to reflect system interruptions or resumes to user interface
*/
val audioSourceState: Flow<AudioSourceState>
val audioSourceStateFlow: Flow<AudioSourceState>

/**
* Setup audio source for listening to incoming audio.
*
* @param startWhenReady If true, starts the audio source when it becomes ready.
*/
fun setupAudioSource(startWhenReady: Boolean = false)
fun setupAudioSource()

/**
* Destroy audio source.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.noiseplanet.noisecapture.services.measurement
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
Expand Down Expand Up @@ -36,7 +37,7 @@ class DefaultMeasurementRecordingService(

private val logger: Logger by inject { parametersOf(TAG) }

private val coroutineScope = CoroutineScope(Dispatchers.Default)
private val scope = CoroutineScope(Dispatchers.Default)
private var recordingJob: Job? = null

private val _isRecording = MutableStateFlow(value = false)
Expand Down Expand Up @@ -95,12 +96,18 @@ class DefaultMeasurementRecordingService(
recordingJob?.cancel()

// Start listening to the various data sources during the recording session
recordingJob = coroutineScope.launch {
userLocationService.getLiveLocation().collect { location ->
ongoingUserLocationHistory.add(location)
}
liveAudioService.getAcousticIndicatorsFlow().collect { acousticIndicators ->
ongoingAcousticIndicators.add(acousticIndicators)
recordingJob = scope.launch {
coroutineScope {
launch {
userLocationService.getLiveLocation().collect { location ->
ongoingUserLocationHistory.add(location)
}
}
launch {
liveAudioService.getAcousticIndicatorsFlow().collect { indicators ->
ongoingAcousticIndicators.add(indicators)
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MeasurementScreenViewModel(

init {
viewModelScope.launch {
liveAudioService.audioSourceState.collect { state ->
liveAudioService.audioSourceStateFlow.collect { state ->
if (state == AudioSourceState.READY) {
// Start recording audio whenever audio source is done initializing
startRecordingAudio()
Expand Down

0 comments on commit 729e298

Please sign in to comment.