<commit_before>import alsaaudio
import threading
from array import array

class Player(object):
    """Play sound using alsa.

    Public attributes:
       * start
       * end
       * position

    """
    def __init__(self, sound):
        self.set_sound(sound)
        self._playing = False
        self._periodsize = 128
        self._pcm = alsaaudio.PCM(type=alsaaudio.PCM_PLAYBACK,
                                  mode=alsaaudio.PCM_NORMAL,
                                  card='default')
        self._pcm.setrate(44100)
        self._pcm.setchannels(2)
        self._pcm.setformat(alsaaudio.PCM_FORMAT_FLOAT64_LE)
        self._pcm.setperiodsize(self._periodsize)
        self._lock = threading.Lock()

    def set_sound(self, sound):
        self._sound = sound
        self.start = 0
        self.position = 0
        self.end = len(sound._data)


    def play(self):

        # Reentrancy: only one thread is allowed to play at the same
        # time. An attempt to play while a thread is already playing
        # will return immediatly.
        if not self._lock.acquire(False):
            return

        # FIXME: self.stop() might have been called before!
        self._playing = True

        # FIXME: remove or make an if statement
        data = array('d', self._sound._data)

        position = self.start
        while self._playing:
            if position > self.end:
                self._playing = False
                position = 0
            else:
                start = position
                end = position + self._periodsize
                buf = data[start:end]
                # converting mono to stereo
                a = []
                for frame in buf:
                    a.append(frame)
                    a.append(frame)
                buf = array('d', a)
                self._pcm.write(buf)
                position = end

        self.position = position
        self._playing = False # useless ?
        self._lock.release()

    def thread_play(self):
        self._playing = True<commit_msg>No conversion before playing in Player.<commit_after>import alsaaudio
import threading
from array import array

class Player(object):
    """Play sound using alsa.

    Public attributes:
       * start
       * end
       * position

    """
    def __init__(self, sound):
        self.set_sound(sound)
        self._playing = False
        self._periodsize = 128
        self._pcm = alsaaudio.PCM(type=alsaaudio.PCM_PLAYBACK,
                                  mode=alsaaudio.PCM_NORMAL,
                                  card='default')
        self._pcm.setrate(44100)
        self._pcm.setchannels(2)
        self._pcm.setformat(alsaaudio.PCM_FORMAT_FLOAT64_LE)
        self._pcm.setperiodsize(self._periodsize)
        self._lock = threading.Lock()

    def set_sound(self, sound):
        self._sound = sound
        self.start = 0
        self.position = 0
        self.end = len(sound._data)


    def play(self):

        # Reentrancy: only one thread is allowed to play at the same
        # time. An attempt to play while a thread is already playing
        # will return immediatly.
        if not self._lock.acquire(False):
            return

        # FIXME: self.stop() might have been called before!
        self._playing = True

        position = self.start
        while self._playing:
            if position > self.end:
                self._playing = False
                position = 0
            else:
                start = position
                end = position + self._periodsize
                buf = self._sound._data[start:end]
                # converting mono to stereo
                a = []
                for frame in buf:
                    a.append(frame)
                    a.append(frame)
                buf = array('d', a)
                self._pcm.write(buf)
                position = end

        self.position = position
        self._playing = False # useless ?
        self._lock.release()

    def thread_play(self):
        self._playing = True
{'fraction_non_alphanumeric': 0.08117443868739206, 'fraction_numerical': 0.0069084628670120895, 'mean_word_length': 1.4932349323493235, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3647575', 'n_tokens_mistral': 1126, 'n_tokens_neox': 1037, 'n_words': 282}