Panda3D
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes
MovieAudioCursor Class Reference

A MovieAudio is actually any source that provides a sequence of audio samples. More...

#include "movieAudioCursor.h"

Inheritance diagram for MovieAudioCursor:
TypedWritableReferenceCount TypedWritable ReferenceCount TypedObject MemoryBase MemoryBase UserDataAudioCursor

List of all members.

Public Member Functions

 MovieAudioCursor (MovieAudio *src)
 This constructor returns a null audio stream --- a stream of total silence, at 8000 samples per second.
bool aborted () const
 If aborted is true, it means that the "ready" samples are not being replenished.
int audio_channels () const
 Returns the number of audio channels (ie, two for stereo, one for mono).
int audio_rate () const
 Returns the audio sample rate.
bool can_seek () const
 Returns true if the movie can seek.
bool can_seek_fast () const
 Returns true if seek operations are constant time.
virtual TypeHandle force_init_type ()
virtual TypeHandle get_type () const
double length () const
 Returns the length of the movie.
 PT (MovieAudio) get_source() const
void read_samples (int n, Datagram *dg)
 Read audio samples from the stream into a Datagram.
string read_samples (int n)
 Read audio samples from the stream and returns them as a string.
virtual void read_samples (int n, PN_int16 *data)
 Read audio samples from the stream.
virtual int ready () const
 Returns the number of audio samples that are ready to read.
virtual void seek (double offset)
 Skips to the specified offset within the file.
void skip_samples (int n)
 Skip audio samples from the stream.
double tell () const
 Returns the current offset within the file.

Static Public Member Functions

static TypeHandle get_class_type ()
static void init_type ()

Protected Member Functions

 PT (MovieAudio) _source

Protected Attributes

bool _aborted
int _audio_channels
int _audio_rate
bool _can_seek
bool _can_seek_fast
double _last_seek
double _length
PN_int64 _samples_read

Detailed Description

A MovieAudio is actually any source that provides a sequence of audio samples.

That could include an AVI file, a microphone, or an internet TV station. A MovieAudioCursor is a handle that lets you read data sequentially from a MovieAudio.

Thread safety: each individual MovieAudioCursor must be owned and accessed by a single thread. It is OK for two different threads to open the same file at the same time, as long as they use separate MovieAudioCursor objects.

Definition at line 38 of file movieAudioCursor.h.


Constructor & Destructor Documentation

This constructor returns a null audio stream --- a stream of total silence, at 8000 samples per second.

To get more interesting audio, you need to construct a subclass of this class.

Definition at line 28 of file movieAudioCursor.cxx.


Member Function Documentation

bool MovieAudioCursor::aborted ( ) const [inline]

If aborted is true, it means that the "ready" samples are not being replenished.

See the method "ready" for an explanation.

Definition at line 108 of file movieAudioCursor.I.

int MovieAudioCursor::audio_channels ( ) const [inline]

Returns the number of audio channels (ie, two for stereo, one for mono).

Definition at line 42 of file movieAudioCursor.I.

int MovieAudioCursor::audio_rate ( ) const [inline]

Returns the audio sample rate.

Definition at line 31 of file movieAudioCursor.I.

bool MovieAudioCursor::can_seek ( ) const [inline]

Returns true if the movie can seek.

If this is true, seeking is still not guaranteed to be fast: for some movies, seeking is implemented by rewinding to the beginning and then fast-forwarding to the desired location. Even if the movie cannot seek, the seek method can still advance to an arbitrary location by reading samples and discarding them. However, to move backward, can_seek must return true.

Definition at line 86 of file movieAudioCursor.I.

bool MovieAudioCursor::can_seek_fast ( ) const [inline]

Returns true if seek operations are constant time.

Definition at line 96 of file movieAudioCursor.I.

double MovieAudioCursor::length ( ) const [inline]

Returns the length of the movie.

Attempting to read audio samples beyond the specified length will produce silent samples.

Some kinds of Movie, such as internet TV station, might not have a predictable length. In that case, the length will be set to a very large number: 1.0E10.

Some AVI files have incorrect length values encoded into them - they may be a second or two long or short. When playing such an AVI using the Movie class, you may see a slightly truncated video, or a slightly elongated video (padded with black frames). There are utilities out there to fix the length values in AVI files.

An audio consumer needs to check the length, the ready status, and the aborted flag.

Definition at line 69 of file movieAudioCursor.I.

void MovieAudioCursor::read_samples ( int  n,
Datagram dg 
)

Read audio samples from the stream into a Datagram.

N is the number of samples you wish to read. Multiple-channel audio will be interleaved.

This is not particularly efficient, but it may be a convenient way to manipulate samples in python.

Definition at line 85 of file movieAudioCursor.cxx.

References Datagram::add_int16().

Referenced by read_samples(), and skip_samples().

string MovieAudioCursor::read_samples ( int  n)

Read audio samples from the stream and returns them as a string.

The samples are stored little-endian in the string. N is the number of samples you wish to read. Multiple-channel audio will be interleaved.

This is not particularly efficient, but it may be a convenient way to manipulate samples in python.

Definition at line 111 of file movieAudioCursor.cxx.

References read_samples().

void MovieAudioCursor::read_samples ( int  n,
PN_int16 *  data 
) [virtual]

Read audio samples from the stream.

N is the number of samples you wish to read. Your buffer must be equal in size to N * channels. Multiple-channel audio will be interleaved.

Reimplemented in UserDataAudioCursor.

Definition at line 58 of file movieAudioCursor.cxx.

int MovieAudioCursor::ready ( ) const [virtual]

Returns the number of audio samples that are ready to read.

This is primarily relevant for sources like microphones which produce samples at a fixed rate. If you try to read more samples than are ready, the result will be silent samples.

Some audio streams do not have a limit on how fast they can produce samples. Such streams will always return 0x40000000 as the ready-count. This may well exceed the length of the audio stream. You therefore need to check length separately.

If the aborted flag is set, that means the ready count is no longer being replenished. For example, a MovieAudioCursor might be reading from an internet radio station, and it might buffer data to avoid underruns. If it loses connection to the radio station, it will set the aborted flag to indicate that the buffer is no longer being replenished. But it is still ok to read the samples that are in the buffer, at least until they run out. Once those are gone, there will be no more.

An audio consumer needs to check the length, the ready status, and the aborted flag.

Reimplemented in UserDataAudioCursor.

Definition at line 188 of file movieAudioCursor.cxx.

void MovieAudioCursor::seek ( double  offset) [virtual]

Skips to the specified offset within the file.

If the movie reports that it cannot seek, then this method can still advance by reading samples and discarding them. However, to move backward, can_seek must be true.

If the movie reports that it can_seek, it doesn't mean that it can do so quickly. It may have to rewind the movie and then fast forward to the desired location. Only if can_seek_fast returns true can seek operations be done in constant time.

Seeking may not be precise, because AVI files often have inaccurate indices. After seeking, tell will indicate that the cursor is at the target location. However, in truth, the data you read may come from a slightly offset location.

Reimplemented in UserDataAudioCursor.

Definition at line 153 of file movieAudioCursor.cxx.

void MovieAudioCursor::skip_samples ( int  n) [inline]

Skip audio samples from the stream.

This is mostly for debugging purposes.

Definition at line 129 of file movieAudioCursor.I.

References read_samples().

double MovieAudioCursor::tell ( ) const [inline]

Returns the current offset within the file.

Definition at line 118 of file movieAudioCursor.I.


The documentation for this class was generated from the following files:
 All Classes Functions Variables Enumerations