<commit_before>/**
 * Copyright 2012 The PlayN Authors
 *
 * 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 playn.ios;

import cli.System.Threading.ThreadPool;
import cli.System.Threading.WaitCallback;

import cli.MonoTouch.AVFoundation.AVAudioPlayer;
import cli.MonoTouch.Foundation.NSError;
import cli.MonoTouch.Foundation.NSUrl;

import cli.OpenTK.Audio.AudioContext;
import cli.OpenTK.Audio.OpenAL.AL;
import cli.OpenTK.Audio.OpenAL.ALGetSourcei;
import cli.OpenTK.Audio.OpenAL.ALSourceState;
import cli.OpenTK.Audio.OpenAL.ALSourceb;
import cli.OpenTK.Audio.OpenAL.ALSourcef;
import cli.OpenTK.Audio.OpenAL.ALSourcei;

import playn.core.AudioImpl;
import playn.core.Sound;

public class IOSAudio extends AudioImpl {

  @SuppressWarnings("unused") // retain a reference to keep OpenTK Audio kosher
  private final AudioContext actx;
  private final int[] result = new int[1]; // used for AL.GetSource

  private final int[] sources;
  private final IOSSoundOAL[] active;
  private final int[] started;

  public IOSAudio(IOSPlatform platform, int numSources) {
    super(platform);
    actx = new AudioContext();
    // obtain our desired number of sources
    sources = new int[numSources];
    AL.GenSources(sources.length, sources);
    active = new IOSSoundOAL[sources.length];
    started = new int[sources.length];
  }

  public Sound createSound(String path, boolean isMusic) {
    // if the file is meant to be music, or if it's not uncompressed CAFF, we need to use
    // AVAudioPlayer; if it's uncompressed CAFF, we can use OpenAL
    platform.log().info("Creating sound " + path);
    return (isMusic || !path.endsWith(".caf")) ?
      createAVAP(NSUrl.FromFilename(path)) : createOAL(path);
  }

  Sound createAVAP(NSUrl url) {
    final IOSSoundAVAP sound = new IOSSoundAVAP();
    ThreadPool.QueueUserWorkItem(new WaitCallback(new WaitCallback.Method() {
      public void Invoke(Object arg) {
        NSUrl url = (NSUrl) arg;
        NSError[] error = new NSError[1];
        AVAudioPlayer player = AVAudioPlayer.FromUrl(url, error);
        if (error[0] == null) {<commit_msg>Clear and restore our OpenAL audio session on interrupt.<commit_after>/**
 * Copyright 2012 The PlayN Authors
 *
 * 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 playn.ios;

import cli.System.EventArgs;
import cli.System.EventHandler;
import cli.System.Threading.ThreadPool;
import cli.System.Threading.WaitCallback;

import cli.MonoTouch.AVFoundation.AVAudioPlayer;
import cli.MonoTouch.AudioToolbox.AudioSession;
import cli.MonoTouch.Foundation.NSError;
import cli.MonoTouch.Foundation.NSUrl;

import cli.OpenTK.Audio.AudioContext;
import cli.OpenTK.Audio.OpenAL.AL;
import cli.OpenTK.Audio.OpenAL.ALGetSourcei;
import cli.OpenTK.Audio.OpenAL.ALSourceState;
import cli.OpenTK.Audio.OpenAL.ALSourceb;
import cli.OpenTK.Audio.OpenAL.ALSourcef;
import cli.OpenTK.Audio.OpenAL.ALSourcei;
import cli.OpenTK.Audio.OpenAL.Alc;
import cli.OpenTK.ContextHandle;

import playn.core.AudioImpl;
import playn.core.Sound;

public class IOSAudio extends AudioImpl {

  private final AudioContext actx;
  private final int[] result = new int[1]; // used for AL.GetSource

  private final int[] sources;
  private final IOSSoundOAL[] active;
  private final int[] started;

  public IOSAudio(IOSPlatform platform, int numSources) {
    super(platform);
    actx = new AudioContext();

    // obtain our desired number of sources
    sources = new int[numSources];
    AL.GenSources(sources.length, sources);
    active = new IOSSoundOAL[sources.length];
    started = new int[sources.length];

    // clear and restore our OAL context on audio session interruption
    AudioSession.add_Interrupted(new EventHandler(new EventHandler.Method() {
      public void Invoke(Object sender, EventArgs event) {
        Alc.MakeContextCurrent(ContextHandle.Zero);
      }
    }));
    AudioSession.add_Resumed(new EventHandler(new EventHandler.Method() {
      public void Invoke(Object sender, EventArgs event) {
        actx.MakeCurrent(); // calls Alc.MakeContextCurrent under the hood
      }
    }));
  }

  public Sound createSound(String path, boolean isMusic) {
    // if the file is meant to be music, or if it's not uncompressed CAFF, we need to use
    // AVAudioPlayer; if it's uncompressed CAFF, we can use OpenAL
    platform.log().info("Creating sound " + path);
    return (isMusic || !path.endsWith(".caf")) ?
      createAVAP(NSUrl.FromFilename(path)) : createOAL(path);
  }

  Sound createAVAP(NSUrl url) {
    final IOSSoundAVAP sound = new IOSSoundAVAP();
    ThreadPool.QueueUserWorkItem(new WaitCallback(new WaitCallback.Method() {
      public void Invoke(Object arg) {
        NSUrl url = (NSUrl) arg;
        NSError[] error = new NSError[1];
        AVAudioPlayer player = AVAudioPlayer.FromUrl(url, error);
        if (error[0] == null) {
{'fraction_non_alphanumeric': 0.09650468883205456, 'fraction_numerical': 0.0037510656436487637, 'mean_word_length': 3.9965928449744466, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3844147', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1745, 'n_words': 544}