//**********************************************************************************************************************
//
//	BXAudio.h
//
//	Author:		Peter Baumgartner (peter@baumgartner.com)
//	Copyright:	©2010 by IMAGINE GbR. All rights reserved.
//	Abstract:	Provides audio playback through QTKit (64bit compatible)...
//
//**********************************************************************************************************************


#if defined(MAC_OS_X_VERSION_10_5) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5


//----------------------------------------------------------------------------------------------------------------------


#pragma mark HEADERS

#import <QTKit/QTKit.h>


//----------------------------------------------------------------------------------------------------------------------


#pragma mark CONSTANTS

extern const double kBXAudioDefaultVolumeGamma;


//----------------------------------------------------------------------------------------------------------------------


#pragma mark

@interface BXAudio : NSObject
{
	id _delegate;
	
	QTMovie* _qtMovie;
	
	QTTime _inPoint;
	QTTime _outPoint;
	QTTime _duration;
	BOOL _loops;
	double _videoFrameRate;
	double _updateUserInterfaceInterval;
	
	// Playback state...
	
	BOOL _playing;
	BOOL _paused;
	BOOL _stopped;
	BOOL _running;
	
	// Volume control...
	
	float _volume;
	double _referenceVolume;
	double _volumeGamma;
	NSArray* _volumeEnvelope;
	
	double _envelopeStartTime;
	double _envelopeStartVolume;
	double _envelopeEndTime;
	double _envelopeEndVolume;
	
	double _fadeoutStartTime;
	double _fadeoutStartVolume;
	double _fadeoutEndTime;
	double _fadeoutEndVolume;

	BOOL _fadeoutStop;

	// Audiomarkers...
	
	NSArray* _markers;
	NSUInteger _nextMarkerIndex;
	double _nextMarkerTime;
	NSUInteger _lastMarkerIndex;
	double _lastMarkerTime;
	double _lastMarkerDelay;
	
	BOOL _isProtected;
	BOOL _isAuthorized;
}


// Creating...
 
+ (BXAudio*) audioWithURL:(NSURL*)inURL audioDevice:(NSString*)inDeviceUID error:(NSError**)outError;
- (id) initWithURL:(NSURL*)inURL audioDevice:(NSString*)inDeviceUID error:(NSError**)outError;

+ (BXAudio*) audioWithFile:(NSString*)inPath audioDevice:(NSString*)inDeviceUID error:(NSError**)outError;
- (id) initWithFile:(NSString*)inPath audioDevice:(NSString*)inDeviceUID error:(NSError**)outError;

- (QTMovieLoadState) loadState;
- (void) waitUntilLoaded;

// Properties...

@property (assign) id delegate;
@property (retain) QTMovie* qtMovie;

@property (readonly) double durationInSeconds;
@property (assign) double currentTimeInSeconds;
@property (assign) double inPointInSeconds;
@property (assign) double outPointInSeconds;
@property (nonatomic, assign) QTTime inPoint;
@property (nonatomic, assign) QTTime outPoint;
@property (nonatomic, assign) BOOL loops;
@property (retain) NSArray* markers;						// Objects must implement the BXAudioMarker protocol

@property (assign) double videoFrameRate;
@property (assign) double updateUserInterfaceInterval;

// Playback control...

- (void) play;
- (void) pause;
- (void) stop;

@property (readonly) BOOL isPlaying;
@property (readonly) BOOL isPaused;
@property (readonly) BOOL isStopped;

// Volume control...

@property (assign) float volume;
@property (assign) double referenceVolume;					// Volume envelopes are scaled with this value
@property (assign) double volumeGamma;						// Set this value to achieve a desirable fading response
@property (retain) NSArray* volumeEnvelope;					// Objects must implement the BXQTVolumeEnvelopePoint protocol

- (void) fadeoutWithDuration:(double)inDuration andStop:(BOOL)inStop;

// Last audiomarker that fired...

@property (readonly) NSUInteger lastMarkerIndex;
@property (readonly) double lastMarkerTime;
@property (readonly) double lastMarkerDelay;

// is audio protected or authorized

@property (readonly) BOOL isProtected;
@property (readonly) BOOL isAuthorized;

@end


//----------------------------------------------------------------------------------------------------------------------


// Audiomarkers can be any object that conform to the following protocol, i.e. they must implement   
// accessors for the time and name properties...

#pragma mark

@protocol BXAudioMarker
@property double time;
@property (retain) NSString* name;
@end


// A volumeEnvelope is an array of objects that conform to the following protocol, i.e. they must  
// implement accessors for the time and volume properties...

#pragma mark

@protocol BXVolumeEnvelopePoint
@property double time;
@property double volume;
@end


// The delegate may implement the following optional methods...

#pragma mark

@protocol BXAudioDelegate

@optional

- (void) didChangeLoadState:(QTMovieLoadState)inLoadState forAudio:(BXAudio*)inAudio;

- (void) willStartPlayingAudio:(BXAudio*)inAudio;
- (void) didStartPlayingAudio:(BXAudio*)inAudio;

- (void) didFireMarkerForAudio:(BXAudio*)inAudio withIndex:(NSUInteger)inIndex;
- (void) updateUserInterfaceForAudio:(BXAudio*)inAudio;
- (void) didLoopAudio:(BXAudio*)inAudio;

- (void) willStopPlayingAudio:(BXAudio*)inAudio;
- (void) didStopPlayingAudio:(BXAudio*)inAudio atEnd:(BOOL)inAtEnd;

@end


//----------------------------------------------------------------------------------------------------------------------

#endif
