<commit_before>/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.camera.integration.avsync.model

import android.content.Context
import android.media.AudioAttributes
import android.media.AudioFormat
import android.media.AudioManager
import android.media.AudioTrack
import androidx.annotation.VisibleForTesting
import androidx.camera.core.Logger
import androidx.core.util.Preconditions.checkArgument
import androidx.core.util.Preconditions.checkArgumentNonnegative
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlin.math.pow
import kotlin.math.sin

private const val TAG = "AudioGenerator"
private const val DEFAULT_SAMPLE_RATE: Int = 44100
private const val SAMPLE_WIDTH: Int = 2
private const val MAGNITUDE = 0.5
private const val ENCODING: Int = AudioFormat.ENCODING_PCM_16BIT
private const val CHANNEL = AudioFormat.CHANNEL_OUT_MONO

class AudioGenerator(private val isEnabled: Boolean = true) {

    @VisibleForTesting
    var audioTrack: AudioTrack? = null

    fun start() {
        if (isEnabled) {
            startInternal()
        } else {
            Logger.i(TAG, "Will not start audio generation, since AudioGenerator is disabled.")
        }
    }

    private fun startInternal() {
        Logger.i(TAG, "start audio generation")
        audioTrack!!.play()
    }

    fun stop() {
        if (isEnabled) {
            stopInternal()
        } else {
            Logger.i(TAG, "Will not stop audio generation, since AudioGenerator is disabled.")
        }
    }

    private fun stopInternal() {
        Logger.i(TAG, "stop audio generation")
        Logger.i(TAG, "playState before stopped: ${audioTrack!!.playState}")
        Logger.i(TAG, "playbackHeadPosition before stopped: ${audioTrack!!.playbackHeadPosition}")
        audioTrack!!.stop()
    }

    suspend fun initial(
        context: Context,
        frequency: Int,
        beepLengthInSec: Double,
    ): Boolean {
        checkArgumentNonnegative(frequency, "The input frequency should not be negative.")
        checkArgument(beepLengthInSec >= 0, "The beep length should not be negative.")

        return if (isEnabled) {<commit_msg>Reload static data after stopping audio track<commit_after>/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.camera.integration.avsync.model

import android.content.Context
import android.media.AudioAttributes
import android.media.AudioFormat
import android.media.AudioManager
import android.media.AudioTrack
import androidx.annotation.VisibleForTesting
import androidx.camera.core.Logger
import androidx.core.util.Preconditions.checkArgument
import androidx.core.util.Preconditions.checkArgumentNonnegative
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlin.math.pow
import kotlin.math.sin

private const val TAG = "AudioGenerator"
private const val DEFAULT_SAMPLE_RATE: Int = 44100
private const val SAMPLE_WIDTH: Int = 2
private const val MAGNITUDE = 0.5
private const val ENCODING: Int = AudioFormat.ENCODING_PCM_16BIT
private const val CHANNEL = AudioFormat.CHANNEL_OUT_MONO

class AudioGenerator(private val isEnabled: Boolean = true) {

    @VisibleForTesting
    var audioTrack: AudioTrack? = null

    fun start() {
        if (isEnabled) {
            startInternal()
        } else {
            Logger.i(TAG, "Will not start audio generation, since AudioGenerator is disabled.")
        }
    }

    private fun startInternal() {
        Logger.i(TAG, "start audio generation")
        audioTrack!!.play()
    }

    fun stop() {
        if (isEnabled) {
            stopInternal()
        } else {
            Logger.i(TAG, "Will not stop audio generation, since AudioGenerator is disabled.")
        }
    }

    private fun stopInternal() {
        Logger.i(TAG, "stop audio generation")
        Logger.i(TAG, "playState before stopped: ${audioTrack!!.playState}")
        Logger.i(TAG, "playbackHeadPosition before stopped: ${audioTrack!!.playbackHeadPosition}")
        audioTrack!!.stop()
        audioTrack!!.reloadStaticData()
    }

    suspend fun initial(
        context: Context,
        frequency: Int,
        beepLengthInSec: Double,
    ): Boolean {
        checkArgumentNonnegative(frequency, "The input frequency should not be negative.")
        checkArgument(beepLengthInSec >= 0, "The beep length should not be negative.")

        return if (isEnabled) {
{'fraction_non_alphanumeric': 0.08195244956772334, 'fraction_numerical': 0.006844380403458214, 'mean_word_length': 3.3897233201581027, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5941856', 'n_tokens_mistral': 1506, 'n_tokens_neox': 1459, 'n_words': 499}