<commit_before>// Includes
#include <cstring>
#include "Sound.h"
#include "System.h"
#include "fmod/fmod.h"

// Globals
static SoundManager *gSoundManager = 0;
SoundManager *sound_manager() {return system()->sound_manager();}

// SoundSource
// ===========

void SoundSource::Play() {
  if (channel_ != 0)
    FMOD_Channel_SetPaused(channel_, false);
}

void SoundSource::Pause() {
  if (channel_ != 0)
    FMOD_Channel_SetPaused(channel_, true);
}

bool SoundSource::IsPaused() const {
  FMOD_BOOL is_paused = false;
  if (channel_ != 0)
    FMOD_Channel_GetPaused(channel_, &is_paused);
  return is_paused;
}

void SoundSource::Stop() {
  if (channel_ != 0)
    FMOD_Channel_Stop(channel_);
}

bool SoundSource::IsStopped() const {
  FMOD_BOOL is_playing = false;
  if (channel_ != 0)
    FMOD_Channel_IsPlaying(channel_, &is_playing);
  return !is_playing;
}

// SoundSample
// ===========

SoundSample *SoundSample::Load(InputStream in, bool store_compressed, float base_volume) {
  // Load the data into memory - we could eventually switch to using extra_info.fileoffset or
  // FMOD_OPENUSER mode instead but this is good for now.
  if (!in.IsValid() || !sound_manager()->IsInitialized())
    return 0;
  char *data;
  int length = in.ReadAllData((void**)&data);

  // Attempt to create the sound
  FMOD_SOUND *sound;
  FMOD_RESULT result;
  FMOD_CREATESOUNDEXINFO extra_info;
  memset(&extra_info, 0, sizeof(extra_info));
  extra_info.cbsize = sizeof(extra_info);
  extra_info.length = length;
  if (store_compressed) {
    result = FMOD_System_CreateSound(sound_manager()->GetSystem(),
      data, FMOD_CREATECOMPRESSEDSAMPLE | FMOD_OPENMEMORY | FMOD_SOFTWARE, &extra_info, &sound);
  } else {
    result = FMOD_System_CreateSound(sound_manager()->GetSystem(),
      data, FMOD_DEFAULT | FMOD_OPENMEMORY | FMOD_SOFTWARE, &extra_info, &sound);
  }
  
  // Store the result or return error
  delete[] data;
  if (result != FMOD_OK)
    return 0;
  return new SoundSample(sound, base_volume);
}

SoundSample::~SoundSample() {
  FMOD_Sound_Release(sound_);
}

SoundSource SoundSample::Play(bool looped, bool start_paused) const {
  FMOD_CHANNEL *channel;
  FMOD_Sound_SetLoopCount(sound_, looped? -1 : 0);
  int rv = FMOD_System_PlaySound(sound_manager()->GetSystem(), FMOD_CHANNEL_FREE, sound_, start_paused, &channel);
  FMOD_Channel_SetVolume(channel, base_volume_);
  return SoundSource(channel);
}

// SoundManager
// ============

void SoundManager::SetVolume(float volume) {<commit_msg>add supporrt for looping sound<commit_after>// Includes
#include <cstring>
#include "Sound.h"
#include "System.h"
#include "fmod/fmod.h"

// Globals
static SoundManager *gSoundManager = 0;
SoundManager *sound_manager() {return system()->sound_manager();}

// SoundSource
// ===========

void SoundSource::Play() {
  if (channel_ != 0)
    FMOD_Channel_SetPaused(channel_, false);
}

void SoundSource::Pause() {
  if (channel_ != 0)
    FMOD_Channel_SetPaused(channel_, true);
}

bool SoundSource::IsPaused() const {
  FMOD_BOOL is_paused = false;
  if (channel_ != 0)
    FMOD_Channel_GetPaused(channel_, &is_paused);
  return is_paused;
}

void SoundSource::Stop() {
  LOGF("stopped");
  if (channel_ != 0)
    FMOD_Channel_Stop(channel_);
}

bool SoundSource::IsStopped() const {
  FMOD_BOOL is_playing = false;
  if (channel_ != 0)
    FMOD_Channel_IsPlaying(channel_, &is_playing);
  return !is_playing;
}

// SoundSample
// ===========

SoundSample *SoundSample::Load(InputStream in, bool store_compressed, float base_volume) {
  // Load the data into memory - we could eventually switch to using extra_info.fileoffset or
  // FMOD_OPENUSER mode instead but this is good for now.
  if (!in.IsValid() || !sound_manager()->IsInitialized())
    return 0;
  char *data;
  int length = in.ReadAllData((void**)&data);

  // Attempt to create the sound
  FMOD_SOUND *sound;
  FMOD_RESULT result;
  FMOD_CREATESOUNDEXINFO extra_info;
  memset(&extra_info, 0, sizeof(extra_info));
  extra_info.cbsize = sizeof(extra_info);
  extra_info.length = length;
  if (store_compressed) {
    result = FMOD_System_CreateSound(sound_manager()->GetSystem(),
      data, FMOD_CREATECOMPRESSEDSAMPLE | FMOD_OPENMEMORY | FMOD_SOFTWARE | FMOD_LOOP_NORMAL, &extra_info, &sound);
  } else {
    result = FMOD_System_CreateSound(sound_manager()->GetSystem(),
      data, FMOD_DEFAULT | FMOD_OPENMEMORY | FMOD_SOFTWARE | FMOD_LOOP_NORMAL, &extra_info, &sound);
  }
  
  // Store the result or return error
  delete[] data;
  if (result != FMOD_OK)
    return 0;
  return new SoundSample(sound, base_volume);
}

SoundSample::~SoundSample() {
  FMOD_Sound_Release(sound_);
}

SoundSource SoundSample::Play(bool looped, bool start_paused) const {
  FMOD_CHANNEL *channel;
  FMOD_Sound_SetLoopCount(sound_, looped? -1 : 0);
  FMOD_System_PlaySound(sound_manager()->GetSystem(), FMOD_CHANNEL_FREE, sound_, start_paused, &channel);
  FMOD_Channel_SetVolume(channel, base_volume_);
  return SoundSource(channel);
}

// SoundManager
// ============

void SoundManager::SetVolume(float volume) {
{'fraction_non_alphanumeric': 0.1344139157936351, 'fraction_numerical': 0.004348685510970548, 'mean_word_length': 4.974025974025974, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1265656', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1722, 'n_words': 383}