<commit_before>import pyaudio
import wave
from pyoperant.interfaces import base_
from pyoperant import InterfaceError

class PyAudioInterface(base_.BaseInterface):
    """Class which holds information about an audio device"""
    def __init__(self,device_name,*args,**kwargs):
        super(PyAudioInterface, self).__init__(*args,**kwargs)
        self.device_name = device_name
        self.open()

    def open(self):
        self.pa = pyaudio.PyAudio()
        for index in range(self.pa.get_device_count()):
            if self.device_name == self.pa.get_device_info_by_index(index)['name']:
                self.device_index = index
                break
            else:
                self.device_index = None
        if self.device_index == None:
            raise InterfaceError('could not find pyaudio device %s' % (self.device_name))

        self.device_info = self.pa.get_device_info_by_index(self.device_index)

    def close(self):
        self.stream.close()
        self._wf.close()
        self.pa.terminate()

    def _get_stream(self,start=False):
        """
        """
        def _callback(in_data, frame_count, time_info, status):
            data = self._wf.readframes(frame_count)
            return (data, pyaudio.paContinue)

        self.stream = self.pa.open(format=self.pa.get_format_from_width(self._wf.getsampwidth()),
                                   channels=self._wf.getnchannels(),
                                   rate=self._wf.getframerate(),
                                   output=True,
                                   output_device_index=self.device_index,
                                   start=start,
                                   stream_callback=_callback)

    def _queue_wav(self,wav_file,start=False):
        self._wf = wave.open(wav_file)
        self._get_stream(start=start)

    def _play_wav(self):
        self.stream.start_stream()

    def _stop_wav(self):
        self.stream.close()<commit_msg>fixed errors on close. added callback & validation<commit_after>import pyaudio
import wave
from pyoperant.interfaces import base_
from pyoperant import InterfaceError

class PyAudioInterface(base_.BaseInterface):
    """Class which holds information about an audio device

    assign a simple callback function that will execute on each frame
    presentation by writing interface.callback

    interface.callback() should return either True (to continue playback) or
    False (to terminate playback)

    Before assigning any callback function, please read the following:
    https://www.assembla.com/spaces/portaudio/wiki/Tips_Callbacks

    """
    def __init__(self,device_name='default',*args,**kwargs):
        super(PyAudioInterface, self).__init__(*args,**kwargs)
        self.device_name = device_name
        self.device_index = None
        self.stream = None
        self.wf = None
        self.callback = None
        self.open()

    def open(self):
        self.pa = pyaudio.PyAudio()
        for index in range(self.pa.get_device_count()):
            if self.device_name == self.pa.get_device_info_by_index(index)['name']:
                self.device_index = index
                break
            else:
                self.device_index = None
        if self.device_index == None:
            raise InterfaceError('could not find pyaudio device %s' % (self.device_name))

        self.device_info = self.pa.get_device_info_by_index(self.device_index)

    def close(self):
        try:
            self.stream.close()
        except AttributeError:
            self.stream = None
        try:    
            self.wf.close()
        except AttributeError:
            self.wf = None
        self.pa.terminate()

    def validate(self):
        if self.wf is not None:
            return True
        else:
            raise InterfaceError('there is something wrong with this wav file')

    def _get_stream(self,start=False):
        """
        """
        def _callback(in_data, frame_count, time_info, status):
            try:
                cont = self.callback()         
            except TypeError:
                cont = True

            if cont:
                data = self.wf.readframes(frame_count)
                return (data, pyaudio.paContinue)
            else:
                return (0, pyaudio.paComplete)

        self.stream = self.pa.open(format=self.pa.get_format_from_width(self.wf.getsampwidth()),
                                   channels=self.wf.getnchannels(),
                                   rate=self.wf.getframerate(),
                                   output=True,
                                   output_device_index=self.device_index,
                                   start=start,
                                   stream_callback=_callback)

    def _queue_wav(self,wav_file,start=False):
        self.wf = wave.open(wav_file)
        self.validate()
        self._get_stream(start=start)

    def _play_wav(self):
        self.stream.start_stream()

    def _stop_wav(self):
        try:
            self.stream.close()
        except AttributeError:
            self.stream = None
{'fraction_non_alphanumeric': 0.09014634146341463, 'fraction_numerical': 0.0001951219512195122, 'mean_word_length': 1.8781583380123525, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5607340', 'n_tokens_mistral': 1447, 'n_tokens_neox': 1363, 'n_words': 289}