<commit_before>/*
 * CPSound.j
 * AppKit
 *
 * Created by <NAME>
 * Copyright 2010, <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

@import <Foundation/CPObject.j>

CPSoundLoadStateEmpty       = 0;
CPSoundLoadStateLoading     = 1;
CPSoundLoadStateCanBePlayed = 1;

CPSoundPlayBackStatePlay    = 0;
CPSoundPlayBackStateStop    = 1;
CPSoundPlayBackStatePause   = 2;


/*! TNSound is a Cappuccino for binding for HTML5 audio element
    respecting the NSSound protocol

    CPSound delegate:
        - sound:didFinishPlaying: called when sound has finished to played

*/
@implementation CPSound : CPObject
{
    CPString            _name       @accessors(property=name);
    id                  _delegate   @accessors(property=delegate);

    HTMLAudioElement    _audioTag;
    int                 _loadStatus;
    int                 _playBackStatus;
}

#pragma mark -
#pragma mark Initialization

- (id)init
{
    if (self = [super init])
    {
        _loadStatus = CPSoundLoadStateEmpty;
        _loops = NO;
        _audioTag = new Audio();
        _audioTag.preload = YES;
        _audioTag.addEventListener("canplay", function(){
            [self _soundDidload];
        })
        _audioTag.addEventListener("ended", function(){
            [self _soundDidEnd];
        })

    }

    return self;
}

/*! init the sound with contents of file located at url represetend by CPString.
    @param aFile CPString containing the path of the sound
    @param byRef ignored. keeped for Cocoa compat
*/
- (id)initWithContentsOfFile:(CPString)aFile byReference:(BOOL)byRef
{
    if (self = [self init])
    {
        _loadStatus = CPSoundLoadStateLoading;
        _audioTag.src = aFile;
    }

    return self;
}

/*! init the sound with contents of file located at CURL.
    @param aFile CPURL containing the URL of the sound
    @param byRef ignored. keeped for Cocoa compat
*/
- (id)initWithContentsOfURL:(CPURL)anURL byReference:(BOOL)byRef
{
    return [self initWithContentsOfFile:[anURL absoluteString] byReference:NO];
}

/*! init the sound with contents given CPData.
    @param aFile CPData containing the sound
    @param byRef ignored. keeped for Cocoa compat
*/
- (id)initWithData:(CPData)someData
{
    if (self = [self init])
    {
        _loadStatus = CPSoundLoadStateLoading;
        _audioTag.src = [someData rawString];
    }

    return self;
}


#pragma mark -
#pragma mark Events listener

/*! @ignore
*/
- (void)_soundDidload
{
    _loadStatus = CPSoundLoadStateCanBePlayed;
}

/*! @ignore
*/
- (void)_soundDidEnd
{
    if (_delegate && [_delegate respondsToSelector:@selector(sound:didFinishPlaying:)])
        [_delegate sound:self didFinishPlaying:YES];
}


#pragma mark -
#pragma mark Media controls

/*! play the sound
*/
- (void)play
{
    if ((_loadStatus !== CPSoundLoadStateCanBePlayed)
        || (_playBackStatus === CPSoundPlayBackStatePlay))
        return;

    _audioTag.play();
    _playBackStatus = CPSoundPlayBackStatePlay;
}

/*! stop the sound
*/
- (void)stop
{
    if ((_loadStatus !== CPSoundLoadStateCanBePlayed)
        || (_playBackStatus === CPSoundPlayBackStateStop))
        return;

    _audioTag.pause();
    _audioTag.currentTime = 0.0;
    _playBackStatus = CPSoundPlayBackStateStop;
}

/*! pause the sound
*/
- (void)pause
{
    if ((_loadStatus !== CPSoundLoadStateCanBePlayed)
        || (_playBackStatus === CPSoundPlayBackStatePause))
        return;

    _audioTag.pause();
    _playBackStatus = CPSoundPlayBackStatePause;
}

/*! resume paused sound
*/
- (void)resume<commit_msg>some small glitch fix<commit_after>/*
 * CPSound.j
 * AppKit
 *
 * Created by <NAME>
 * Copyright 2010, <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

@import <Foundation/CPObject.j>

CPSoundLoadStateEmpty       = 0;
CPSoundLoadStateLoading     = 1;
CPSoundLoadStateCanBePlayed = 1;

CPSoundPlayBackStatePlay    = 0;
CPSoundPlayBackStateStop    = 1;
CPSoundPlayBackStatePause   = 2;


/*! CPSound is a Cappuccino for binding for HTML5 audio element
    respecting the NSSound protocol

    CPSound delegate:
        - sound:didFinishPlaying: called when sound has finished to played

*/
@implementation CPSound : CPObject
{
    CPString            _name       @accessors(property=name);
    id                  _delegate   @accessors(property=delegate);

    HTMLAudioElement    _audioTag;
    int                 _loadStatus;
    int                 _playBackStatus;
}

#pragma mark -
#pragma mark Initialization

- (id)init
{
    if (self = [super init])
    {
        _loadStatus = CPSoundLoadStateEmpty;
        _loops = NO;
        _audioTag = new Audio();
        _audioTag.preload = YES;
        _audioTag.addEventListener("canplay", function(){
            [self _soundDidload];
        })
        _audioTag.addEventListener("ended", function(){
            [self _soundDidEnd];
        })

    }

    return self;
}

/*! init the sound with contents of file located at url represetend by CPString.
    @param aFile CPString containing the path of the sound
    @param byRef ignored. keeped for Cocoa compat
*/
- (id)initWithContentsOfFile:(CPString)aFile byReference:(BOOL)byRef
{
    if (self = [self init])
    {
        _loadStatus = CPSoundLoadStateLoading;
        _audioTag.src = aFile;
    }

    return self;
}

/*! init the sound with contents of file located at CURL.
    @param aFile CPURL containing the URL of the sound
    @param byRef ignored. keeped for Cocoa compat
*/
- (id)initWithContentsOfURL:(CPURL)anURL byReference:(BOOL)byRef
{
    return [self initWithContentsOfFile:[anURL absoluteString] byReference:NO];
}

/*! init the sound with contents given CPData.
    @param aFile CPData containing the sound
    @param byRef ignored. keeped for Cocoa compat
*/
- (id)initWithData:(CPData)someData
{
    if (self = [self init])
    {
        _loadStatus = CPSoundLoadStateLoading;
        _audioTag.src = [someData rawString];
    }

    return self;
}


#pragma mark -
#pragma mark Events listener

/*! @ignore
*/
- (void)_soundDidload
{
    _loadStatus = CPSoundLoadStateCanBePlayed;
}

/*! @ignore
*/
- (void)_soundDidEnd
{
    if (![self loops])
        [self stop];
    if (_delegate && [_delegate respondsToSelector:@selector(sound:didFinishPlaying:)])
        [_delegate sound:self didFinishPlaying:YES];
}


#pragma mark -
#pragma mark Media controls

/*! play the sound
*/
- (void)play
{
    if ((_loadStatus !== CPSoundLoadStateCanBePlayed)
        || (_playBackStatus === CPSoundPlayBackStatePlay))
        return;

    _audioTag.play();
    _playBackStatus = CPSoundPlayBackStatePlay;
}

/*! stop the sound
*/
- (void)stop
{
    if ((_loadStatus !== CPSoundLoadStateCanBePlayed)
        || (_playBackStatus === CPSoundPlayBackStateStop))
        return;

    _audioTag.pause();
    _audioTag.currentTime = 0.0;
    _playBackStatus = CPSoundPlayBackStateStop;
    
    if (_delegate && [_delegate respondsToSelector:@selector(sound:didFinishPlaying:)])
        [_delegate sound:self didFinishPlaying:YES];
    
}

/*! pause the sound
*/
- (void)pause
{
    if ((_loadStatus !== CPSoundLoadStateCanBePlayed)
        || (_playBackStatus === CPSoundPlayBackStatePause))
        return;

    _audioTag.pause();
    _playBackStatus = CPSoundPlayBackStatePause;
}

/*! resume paused sound
*/
- (void)resume
{'fraction_non_alphanumeric': 0.09335789110164613, 'fraction_numerical': 0.005985956026246115, 'mean_word_length': 3.100047192071732, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '7438382', 'n_tokens_mistral': 2669, 'n_tokens_neox': 2557, 'n_words': 780}