<commit_before>/**
 * Copyright 2011 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.java;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;

import playn.core.Asserts;
import playn.core.PlayN;
import playn.core.ResourceCallback;
import playn.core.Sound;

class JavaSound implements Sound {

  private final String name;
  private Clip clip;
  private boolean looping;

  private List<ResourceCallback<? super Sound>> callbacks;

  JavaSound(String name, final InputStream inputStream) {
    this.name = name;

    JavaAssets.doResourceAction(new Runnable() {
      public void run () {
        init(inputStream);
        if (callbacks != null) {
          for (ResourceCallback<? super Sound> callback : callbacks) {
            callback.done(JavaSound.this);
          }
          callbacks = null;
        }
      }
    });
  }

  private void init(InputStream inputStream) {
    try {
      clip = AudioSystem.getClip();
    } catch (LineUnavailableException e) {
      PlayN.log().warn("Unable to create clip for " + name);
      return; // give up
    } catch (IllegalArgumentException e) {
      // OpenJDK on Linux may throw java.lang.IllegalArgumentException: No line matching interface
      // Clip supporting format PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame,
      // big-endian is supported.
      PlayN.log().info("Failed to load sound " + name + " due to " + e.toString());
      return; // give up
    }

    AudioInputStream ais;
    try {
      ais = AudioSystem.getAudioInputStream(inputStream);
      if (name.endsWith(".mp3")) {
        AudioFormat baseFormat = ais.getFormat();
        AudioFormat decodedFormat = new AudioFormat(
          AudioFormat.Encoding.PCM_SIGNED,
          baseFormat.getSampleRate(),
          16, // we have to force sample size to 16
          baseFormat.getChannels(),
          baseFormat.getChannels()*2,
          baseFormat.getSampleRate(),
          false // big endian
          );
        ais = AudioSystem.getAudioInputStream(decodedFormat, ais);
      }
    } catch (Exception e) {
      PlayN.log().warn("Failed to create audio stream for " + name, e);
      return; // give up
    }

    try {
      clip.open(ais);
    } catch (Exception e) {
      PlayN.log().warn("Failed to open sound " + name, e);
      return; // give up
    }
  }

  @Override
  public boolean play() {
    if (clip == null) {
      return false;
    }
    if (looping) {
      clip.loop(Clip.LOOP_CONTINUOUSLY);
    } else {
      clip.setFramePosition(0);
      clip.start();
    }
    return true;
  }

  @Override
  public void stop() {
    if (clip == null) {
      return;
    }
    clip.stop();
    clip.flush();
  }

  @Override
  public void setLooping(boolean looping) {
    this.looping = looping;
  }

  @Override
  public void setVolume(float volume) {
    Asserts.checkArgument(0f <= volume && volume <= 1f, "Must ensure 0f <= volume <= 1f");
    // TODO implement
  }

  @Override
  public boolean isPlaying() {
    return (clip != null) && clip.isActive();
  }

  @Override
  public void addCallback(ResourceCallback<? super Sound> callback) {
    if (clip != null) {
      callback.done(this);
    } else {
      if (callbacks == null) {
        callbacks = new ArrayList<ResourceCallback<? super Sound>>();
      }
      callbacks.add(callback);
    }
  }
}
<commit_msg>We have to reset the frame position when looping as well.<commit_after>/**
 * Copyright 2011 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.java;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;

import playn.core.Asserts;
import playn.core.PlayN;
import playn.core.ResourceCallback;
import playn.core.Sound;

class JavaSound implements Sound {

  private final String name;
  private Clip clip;
  private boolean looping;

  private List<ResourceCallback<? super Sound>> callbacks;

  JavaSound(String name, final InputStream inputStream) {
    this.name = name;

    JavaAssets.doResourceAction(new Runnable() {
      public void run () {
        init(inputStream);
        if (callbacks != null) {
          for (ResourceCallback<? super Sound> callback : callbacks) {
            callback.done(JavaSound.this);
          }
          callbacks = null;
        }
      }
    });
  }

  private void init(InputStream inputStream) {
    try {
      clip = AudioSystem.getClip();
    } catch (LineUnavailableException e) {
      PlayN.log().warn("Unable to create clip for " + name);
      return; // give up
    } catch (IllegalArgumentException e) {
      // OpenJDK on Linux may throw java.lang.IllegalArgumentException: No line matching interface
      // Clip supporting format PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame,
      // big-endian is supported.
      PlayN.log().info("Failed to load sound " + name + " due to " + e.toString());
      return; // give up
    }

    AudioInputStream ais;
    try {
      ais = AudioSystem.getAudioInputStream(inputStream);
      if (name.endsWith(".mp3")) {
        AudioFormat baseFormat = ais.getFormat();
        AudioFormat decodedFormat = new AudioFormat(
          AudioFormat.Encoding.PCM_SIGNED,
          baseFormat.getSampleRate(),
          16, // we have to force sample size to 16
          baseFormat.getChannels(),
          baseFormat.getChannels()*2,
          baseFormat.getSampleRate(),
          false // big endian
          );
        ais = AudioSystem.getAudioInputStream(decodedFormat, ais);
      }
    } catch (Exception e) {
      PlayN.log().warn("Failed to create audio stream for " + name, e);
      return; // give up
    }

    try {
      clip.open(ais);
    } catch (Exception e) {
      PlayN.log().warn("Failed to open sound " + name, e);
      return; // give up
    }
  }

  @Override
  public boolean play() {
    if (clip == null) {
      return false;
    }
    clip.setFramePosition(0);
    if (looping) {
      clip.loop(Clip.LOOP_CONTINUOUSLY);
    } else {
      clip.start();
    }
    return true;
  }

  @Override
  public void stop() {
    if (clip == null) {
      return;
    }
    clip.stop();
    clip.flush();
  }

  @Override
  public void setLooping(boolean looping) {
    this.looping = looping;
  }

  @Override
  public void setVolume(float volume) {
    Asserts.checkArgument(0f <= volume && volume <= 1f, "Must ensure 0f <= volume <= 1f");
    // TODO implement
  }

  @Override
  public boolean isPlaying() {
    return (clip != null) && clip.isActive();
  }

  @Override
  public void addCallback(ResourceCallback<? super Sound> callback) {
    if (clip != null) {
      callback.done(this);
    } else {
      if (callbacks == null) {
        callbacks = new ArrayList<ResourceCallback<? super Sound>>();
      }
      callbacks.add(callback);
    }
  }
}

{'fraction_non_alphanumeric': 0.10796139927623644, 'fraction_numerical': 0.005307599517490953, 'mean_word_length': 2.691451469278718, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4719673', 'n_tokens_mistral': 2438, 'n_tokens_neox': 2365, 'n_words': 716}