<commit_before>#include "player.hxx"
#include "stb_vorbis.h"
#include <iostream>

Player::Player(std::string const &filename) {
  // Load a vorbis file
  int channels, len, sampleRate;
  int16_t *buffer;
  len = stb_vorbis_decode_filename(filename.c_str(), &channels, &sampleRate, 
      &buffer);

  // Initialize an SDL audio device
  SDL_AudioSpec want, have;

  want.freq = sampleRate;
  want.format = AUDIO_S16LSB;
  want.channels = channels;
  want.samples = 4096;
  want.callback = playerCallback;
  want.userdata = (void*)this;

  audioDevice = SDL_OpenAudioDevice(NULL, 0, &want, &have,
      SDL_AUDIO_ALLOW_FORMAT_CHANGE);

  if (want.format != have.format) {
    std::cout << "Player failed to get required audio format!" << std::endl;
  }

  // Prep player class members to play from previously loaded audio buffer
  currentLen = len * sizeof(uint16_t) * channels;
  audioPos = audioData = (uint8_t*)buffer;
}

Player::~Player() {
  delete[] audioData;
}

void Player::start() {
  SDL_PauseAudioDevice(audioDevice, 0);
}

void Player::playerCallback(void *userData, uint8_t *stream, int len) {
  Player *player = (Player*)userData;

  // Don't play if empty
  if (player->currentLen == 0) {
    SDL_memset(stream, 0, len);
    return;
  }

  // Limit reading to buffer bounds
  len = (len > player->currentLen ? player->currentLen : len);

  // Copy audio buffer to player
  SDL_memcpy(stream, player->audioPos, len);

  // Move to next block
  player->audioPos += len;

  // Keep track of data left in the buffer
  player->currentLen -= len;
}<commit_msg>Improved player playback routine<commit_after>#include "player.hxx"
#include "stb_vorbis.h"
#include <iostream>

Player::Player(std::string const &filename) {
  // Load a vorbis file
  int channels, len, sampleRate;
  int16_t *buffer;
  len = stb_vorbis_decode_filename(filename.c_str(), &channels, &sampleRate, 
      &buffer);

  // Initialize an SDL audio device
  SDL_AudioSpec want, have;

  want.freq = sampleRate;
  want.format = AUDIO_S16LSB;
  want.channels = channels;
  want.samples = 4096;
  want.callback = playerCallback;
  want.userdata = (void*)this;

  audioDevice = SDL_OpenAudioDevice(NULL, 0, &want, &have,
      SDL_AUDIO_ALLOW_FORMAT_CHANGE);

  if (want.format != have.format) {
    std::cout << "Player failed to get required audio format!" << std::endl;
  }

  // Prep player class members to play from previously loaded audio buffer
  currentLen = len * sizeof(uint16_t) * channels;
  audioPos = audioData = (uint8_t*)buffer;
}

Player::~Player() {
  delete[] audioData;
}

void Player::start() {
  SDL_PauseAudioDevice(audioDevice, 0);
}

void Player::playerCallback(void *userData, uint8_t *stream, int len) {
  Player *player = (Player*)userData;

  // Prep with silence (copying doesn't always happen)
  SDL_memset(stream, 0, len);

  // Don't play if empty
  if (player->currentLen == 0) {
    return;
  }

  // Limit reading to buffer bounds
  len = (len > player->currentLen ? player->currentLen : len);

  // Copy audio buffer to player
  SDL_memcpy(stream, player->audioPos, len);

  // Move to next block
  player->audioPos += len;

  // Keep track of data left in the buffer
  player->currentLen -= len;
}
{'fraction_non_alphanumeric': 0.12675179071940207, 'fraction_numerical': 0.009965742759265027, 'mean_word_length': 3.874051593323217, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, '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': '1595117', 'n_tokens_mistral': 1106, 'n_tokens_neox': 1051, 'n_words': 294}