Ejemplo n.º 1
0
int
OpusDecoder_init(decoders_OpusDecoder *self,
                 PyObject *args, PyObject *kwds)
{
    char *filename;
    int error;

    self->opus_file = NULL;
    self->audiotools_pcm = NULL;
    self->closed = 0;

    if (!PyArg_ParseTuple(args, "s", &filename))
        return -1;

    if ((self->opus_file = op_open_file(filename, &error)) == NULL) {
        PyErr_SetString(PyExc_ValueError, "error opening Opus file");
        return -1;
    }

    self->channel_count = op_channel_count(self->opus_file, -1);

    if ((self->audiotools_pcm = open_audiotools_pcm()) == NULL)
        return -1;

    return 0;
}
Ejemplo n.º 2
0
int
ReplayGainReader_init(replaygain_ReplayGainReader *self,
                      PyObject *args, PyObject *kwds) {
    self->pcmreader = NULL;
    self->channels = array_ia_new();
    self->white_noise = NULL;
    self->audiotools_pcm = NULL;

    double replaygain;
    double peak;

    if (!PyArg_ParseTuple(args, "O&dd",
                          pcmreader_converter, &(self->pcmreader),
                          &(replaygain),
                          &(peak)))
        return -1;

    if ((self->white_noise = open_dither()) == NULL)
        return -1;

    if ((self->audiotools_pcm = open_audiotools_pcm()) == NULL)
        return -1;

    self->multiplier = powl(10.0l, replaygain / 20.0l);
    if (self->multiplier > 1.0l)
        self->multiplier = 1.0l / peak;

    return 0;
}
Ejemplo n.º 3
0
int
ALACDecoder_init(decoders_ALACDecoder *self,
                 PyObject *args, PyObject *kwds)
{
    char *filename;
    static char *kwlist[] = {"filename", NULL};
    unsigned i;

    self->filename = NULL;
    self->file = NULL;
    self->bitstream = NULL;
    self->audiotools_pcm = NULL;

    self->frameset_channels = array_ia_new();
    self->frame_channels = array_ia_new();
    self->uncompressed_LSBs = array_i_new();
    self->residuals = array_i_new();

    for (i = 0; i < MAX_CHANNELS; i++) {
        self->subframe_headers[i].qlp_coeff = array_i_new();
    }

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &filename))
        return -1;

    /*open the alac file as a BitstreamReader*/
    if ((self->file = fopen(filename, "rb")) == NULL) {
        PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
        return -1;
    } else {
        self->bitstream = br_open(self->file, BS_BIG_ENDIAN);
    }
    self->filename = strdup(filename);

    self->bitstream->mark(self->bitstream);

    if (parse_decoding_parameters(self)) {
        self->bitstream->unmark(self->bitstream);
        return -1;
    } else {
        self->bitstream->rewind(self->bitstream);
    }

    /*seek to the 'mdat' atom, which contains the ALAC stream*/
    if (seek_mdat(self->bitstream) == ERROR) {
        self->bitstream->unmark(self->bitstream);
        PyErr_SetString(PyExc_ValueError,
                        "Unable to locate 'mdat' atom in stream");
        return -1;
    } else {
        self->bitstream->unmark(self->bitstream);
    }

    /*setup a framelist generator function*/
    if ((self->audiotools_pcm = open_audiotools_pcm()) == NULL)
        return -1;

    return 0;
}
static int
CDDAReader_init(cdio_CDDAReader *self, PyObject *args, PyObject *kwds)
{
    char *device = NULL;
    struct stat buf;

    self->is_cd_image = 0;
    self->is_logging = 0;
    self->dealloc = NULL;
    self->closed = 0;
    self->audiotools_pcm = NULL;
    cddareader_reset_log(&(self->log));

    if (!PyArg_ParseTuple(args, "s|i", &device, &(self->is_logging)))
        return -1;

    if ((self->audiotools_pcm = open_audiotools_pcm()) == NULL)
        return -1;

    /*identify whether drive is physical or a CD image*/
    if (stat(device, &buf)) {
        PyErr_SetFromErrno(PyExc_IOError);
        return -1;
    }
    if (S_ISREG(buf.st_mode)) {
        if (cdio_is_cuefile(device) ||
            cdio_is_binfile(device) ||
            cdio_is_tocfile(device) ||
            cdio_is_nrg(device)) {
            /*open CD image and set function pointers*/
            self->is_cd_image = 1;
            self->is_logging = 0;
            return CDDAReader_init_image(self, device);
        } else {
            /*unsupported file*/
            PyErr_SetString(PyExc_ValueError, "unsupported CD image type");
            return -1;
        }
    } else if (S_ISBLK(buf.st_mode)) {
        if (cdio_is_device(device, DRIVER_LINUX)) {
            /*open CD device and set function pointers*/
            self->is_cd_image = 0;
            return CDDAReader_init_device(self, device);
        } else {
            /*unsupported block device*/
            PyErr_SetString(PyExc_ValueError, "unsupported block device");
            return -1;
        }
    } else {
        /*unsupported file type*/
        PyErr_SetString(PyExc_ValueError, "unsupported file type");
        return -1;
    }
}
Ejemplo n.º 5
0
int
VorbisDecoder_init(decoders_VorbisDecoder *self, PyObject *args, PyObject *kwds) {
    char* filename;
    vorbis_info* info;

    self->open_ok = 0;
    self->channel_count = 0;
    self->rate = 0;
    self->closed = 0;
    self->audiotools_pcm = NULL;

    if (!PyArg_ParseTuple(args, "s", &filename))
        return -1;

    /*open file using reference Ogg Vorbis decoder*/
    switch (ov_fopen(filename, &(self->vorbisfile))) {
    case 0:
    default:
        self->open_ok = 1;
        break;
    case OV_EREAD:
        PyErr_SetString(PyExc_ValueError, "I/O error");
        return -1;
    case OV_ENOTVORBIS:
        PyErr_SetString(PyExc_ValueError, "not a Vorbis file");
        return -1;
    case OV_EVERSION:
        PyErr_SetString(PyExc_ValueError, "Vorbis version mismatch");
        return -1;
    case OV_EBADHEADER:
        PyErr_SetString(PyExc_ValueError, "invalid Vorbis bitstream header");
        return -1;
    case OV_EFAULT:
        PyErr_SetString(PyExc_ValueError, "internal logic fault");
        return -1;
    }

    /*pull stream metadata from decoder*/
    if ((info = ov_info(&(self->vorbisfile), -1)) != NULL) {
        self->channel_count = info->channels;
        self->rate = info->rate;
    } else {
        PyErr_SetString(PyExc_ValueError, "unable to get Vorbis info");
        return -1;
    }

    /*open FrameList creator*/
    if ((self->audiotools_pcm = open_audiotools_pcm()) == NULL)
        return -1;

    return 0;
}
Ejemplo n.º 6
0
int
Sine_Stereo_init(decoders_Sine_Stereo* self, PyObject *args, PyObject *kwds) {
    double f1;
    double f2;

    if ((self->audiotools_pcm = open_audiotools_pcm()) == NULL)
        return -1;

    if (!PyArg_ParseTuple(args, "iiiddddd",
                          &(self->bits_per_sample),
                          &(self->total_pcm_frames),
                          &(self->sample_rate),
                          &f1, &(self->a1),
                          &f2, &(self->a2),
                          &(self->fmult)))
        return -1;

    switch (self->bits_per_sample) {
    case 8:
        self->full_scale = 0x7F;
        break;
    case 16:
        self->full_scale = 0x7FFF;
        break;
    case 24:
        self->full_scale = 0x7FFFFF;
        break;
    default:
        PyErr_SetString(PyExc_ValueError, "bits per sample must be 8, 16, 24");
        return -1;
    }

    if (self->total_pcm_frames < 0) {
        PyErr_SetString(PyExc_ValueError, "total_pcm_frames must be >= 0");
        return -1;
    }

    if (self->sample_rate < 1) {
        PyErr_SetString(PyExc_ValueError, "sample_rate must be > 0");
        return -1;
    }

    self->remaining_pcm_frames = self->total_pcm_frames;
    self->delta1 = 2 * M_PI / (self->sample_rate / f1);
    self->delta2 = 2 * M_PI / (self->sample_rate / f2);
    self->theta1 = self->theta2 = 0.0l;

    self->closed = 0;

    return 0;
}
Ejemplo n.º 7
0
int
MP3Decoder_init(decoders_MP3Decoder *self,
                PyObject *args, PyObject *kwds)
{
    char *filename;
    int error;

    self->handle = NULL;

    self->channels = 0;
    self->rate = 0;
    self->encoding = 0;
    self->closed = 0;

    self->audiotools_pcm = NULL;

    if (!PyArg_ParseTuple(args, "s", &filename))
        return -1;

    if ((self->handle = mpg123_new(NULL, &error)) == NULL) {
        PyErr_SetString(PyExc_ValueError, "error initializing decoder");
        return -1;
    }

    if ((error = mpg123_open(self->handle, filename)) != MPG123_OK) {
        PyErr_SetString(PyExc_ValueError, "error opening file");
        return -1;
    }

    if ((error = mpg123_getformat(self->handle,
                                  &(self->rate),
                                  &(self->channels),
                                  &(self->encoding))) != MPG123_OK) {
        PyErr_SetString(PyExc_ValueError, "error getting file format");
        return -1;
    }

    if ((self->audiotools_pcm = open_audiotools_pcm()) == NULL)
        return -1;

    return 0;
}
Ejemplo n.º 8
0
int
Sine_Simple_init(decoders_Sine_Simple* self, PyObject *args, PyObject *kwds) {
    if ((self->audiotools_pcm = open_audiotools_pcm()) == NULL)
        return -1;

    if (!PyArg_ParseTuple(args, "iiiii",
                          &(self->total_pcm_frames),
                          &(self->bits_per_sample),
                          &(self->sample_rate),
                          &(self->max_value),
                          &(self->count)))
        return -1;

    switch (self->bits_per_sample) {
    case 8:
    case 16:
    case 24:
        break;
    default:
        PyErr_SetString(PyExc_ValueError, "bits per sample must be 8, 16, 24");
        return -1;
    }

    if (self->total_pcm_frames < 0) {
        PyErr_SetString(PyExc_ValueError, "total_pcm_frames must be >= 0");
        return -1;
    }

    if (self->sample_rate < 1) {
        PyErr_SetString(PyExc_ValueError, "sample_rate must be > 0");
        return -1;
    }


    self->remaining_pcm_frames = self->total_pcm_frames;
    self->i = 0;

    self->closed = 0;

    return 0;
}
static int
TrackReader_init(dvda_TrackReader *self, PyObject *args, PyObject *kwds)
{
    dvda_Track* track;

    self->closed = 0;
    self->reader = NULL;
    self->audiotools_pcm = NULL;

    if (!PyArg_ParseTuple(args, "O!", &dvda_TrackType, &track))
        return -1;

    if ((self->reader = dvda_open_track_reader(track->track)) == NULL) {
        PyErr_SetString(PyExc_IOError, "unable to open track reader");
        return -1;
    }

    if ((self->audiotools_pcm = open_audiotools_pcm()) == NULL) {
        return -1;
    }

    return 0;
}
Ejemplo n.º 10
0
int
ALSAAudio_init(output_ALSAAudio *self, PyObject *args, PyObject *kwds)
{
    PyObject *audiotools_pcm = NULL;
    char *device;
    int sample_rate = 44100;
    int channels = 2;
    int bits_per_sample = 16;
    int error;
    snd_pcm_format_t output_format = SND_PCM_FORMAT_S16_LE;

    self->framelist_type = NULL;
    self->output = NULL;
    self->mixer = NULL;
    self->mixer_elem = NULL;
    self->buffer_size = 0;

    /*get FrameList type for comparison during .play() operation*/
    if ((audiotools_pcm = open_audiotools_pcm()) != NULL) {
        self->framelist_type = PyObject_GetAttrString(audiotools_pcm,
                                                      "FrameList");
        Py_DECREF(audiotools_pcm);
        if (self->framelist_type == NULL) {
            /*unable to get audiotools.pcm.FrameList type*/
            return -1;
        }
    } else {
        /*unable to open audiotools.pcm module*/
        return -1;
    }

    if (!PyArg_ParseTuple(args, "siii",
                          &device,
                          &sample_rate,
                          &channels,
                          &bits_per_sample))
        return -1;

    /*sanity check output parameters*/
    if (sample_rate > 0) {
        self->sample_rate = sample_rate;
    } else {
        PyErr_SetString(
            PyExc_ValueError, "sample rate must be a postive value");
        return -1;
    }

    if (channels > 0) {
        self->channels = channels;
    } else {
        PyErr_SetString(
            PyExc_ValueError, "channels must be a positive value");
        return -1;
    }

    switch (bits_per_sample) {
    case 8:
        self->bits_per_sample = bits_per_sample;
        self->buffer.int8 = NULL;
        self->play = play_8_bps;
        output_format = SND_PCM_FORMAT_S8;
        break;
    case 16:
        self->bits_per_sample = bits_per_sample;
        self->buffer.int16 = NULL;
        self->play = play_16_bps;
        output_format = SND_PCM_FORMAT_S16;
        break;
    case 24:
        self->bits_per_sample = bits_per_sample;
        self->buffer.int32 = NULL;
        self->play = play_24_bps;
        output_format = SND_PCM_FORMAT_S32;
        //output_format = SND_PCM_FORMAT_FLOAT;
        break;
    default:
        PyErr_SetString(
            PyExc_ValueError, "bits-per-sample must be 8, 16 or 24");
        return -1;
    }

    if ((error = snd_pcm_open(&self->output,
                              device,
                              SND_PCM_STREAM_PLAYBACK,
                              0)) < 0) {
        PyErr_SetString(PyExc_IOError, "unable to open ALSA output handle");
        return -1;
    }

    if ((error = snd_pcm_set_params(self->output,
                                    output_format,
                                    SND_PCM_ACCESS_RW_INTERLEAVED,
                                    channels,
                                    sample_rate,
                                    1,
                                    500000)) < 0) {
        PyErr_SetString(PyExc_IOError, "unable to set ALSA stream parameters");
        return -1;
    }

    if ((error = snd_mixer_open(&self->mixer, 0)) < 0) {
        /*unable to open ALSA mixer*/
        self->mixer = NULL;
        return 0;
    } else if ((error = snd_mixer_attach(self->mixer, device)) < 0) {
        /*unable to attach mixer to card*/
        snd_mixer_close(self->mixer);
        self->mixer = NULL;
        return 0;
    } else if ((error = snd_mixer_selem_register(self->mixer,
                                                 NULL,
                                                 NULL)) < 0) {
        /*unable to register mixer*/
        snd_mixer_close(self->mixer);
        self->mixer = NULL;
        return 0;
    } else if ((error = snd_mixer_load(self->mixer)) < 0) {
        /*unable to load mixer*/
        snd_mixer_close(self->mixer);
        self->mixer = NULL;
        return 0;
    }

    /*walk through mixer elements to find Master or PCM*/
    self->mixer_elem = find_playback_mixer_element(self->mixer, "Master");
    if (self->mixer_elem == NULL) {
        /*this may be NULL if no Master or PCM found*/
        self->mixer_elem = find_playback_mixer_element(self->mixer, "PCM");
    }
    if (self->mixer_elem != NULL) {
        snd_mixer_selem_get_playback_volume_range(self->mixer_elem,
                                                  &self->volume_min,
                                                  &self->volume_max);
    }

    return 0;
}
Ejemplo n.º 11
0
int
FlacDecoder_init(decoders_FlacDecoder *self,
                 PyObject *args, PyObject *kwds)
{
    char* filename;
    int stream_offset = 0;

    self->filename = NULL;
    self->file = NULL;
    self->bitstream = NULL;

    self->seektable = array_o_new((ARRAY_COPY_FUNC)seekpoint_copy,
                                  free,
                                  NULL);

    self->subframe_data = array_ia_new();
    self->residuals = array_i_new();
    self->qlp_coeffs = array_i_new();
    self->framelist_data = array_i_new();
    self->audiotools_pcm = NULL;
    self->remaining_samples = 0;

    if (!PyArg_ParseTuple(args, "si|i",
                          &filename,
                          &(self->channel_mask),
                          &stream_offset))
        return -1;

    if (self->channel_mask < 0) {
        PyErr_SetString(PyExc_ValueError, "channel_mask must be >= 0");
        return -1;
    }
    if (stream_offset < 0) {
        PyErr_SetString(PyExc_ValueError, "stream offset must be >= 0");
        return -1;
    }

    /*open the flac file*/
    self->file = fopen(filename, "rb");
    if (self->file == NULL) {
        PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
        return -1;
    } else {
        self->bitstream = br_open(self->file, BS_BIG_ENDIAN);
    }

    /*skip the given number of bytes, if any*/
    if (stream_offset != 0)
        fseek(self->file, stream_offset, SEEK_SET);

    self->filename = strdup(filename);

    /*read the STREAMINFO block, SEEKTABLE block
      and setup the total number of samples to read*/
    if (flacdec_read_metadata(self->bitstream,
                              &(self->streaminfo),
                              self->seektable)) {
        self->streaminfo.channels = 0;
        return -1;
    }

    self->remaining_samples = self->streaminfo.total_samples;

    /*initialize the output MD5 sum*/
    audiotools__MD5Init(&(self->md5));
    self->perform_validation = 1;
    self->stream_finalized = 0;

    /*setup a framelist generator function*/
    if ((self->audiotools_pcm = open_audiotools_pcm()) == NULL)
        return -1;

    /*place mark at beginning of stream in case seeking is needed*/
    self->bitstream->mark(self->bitstream);

    /*mark stream as not closed and ready for reading*/
    self->closed = 0;

    return 0;
}
Ejemplo n.º 12
0
int
FlacDecoder_init(decoders_FlacDecoder *self,
                 PyObject *args, PyObject *kwds)
{
    self->file = NULL;
    self->bitstream = NULL;

    self->seektable = a_obj_new((ARRAY_COPY_FUNC)seekpoint_copy,
                                free,
                                NULL);

    self->subframe_data = aa_int_new();
    self->residuals = a_int_new();
    self->qlp_coeffs = a_int_new();
    self->framelist_data = a_int_new();
    self->audiotools_pcm = NULL;
    self->remaining_samples = 0;

    if (!PyArg_ParseTuple(args, "O", &self->file)) {
        return -1;
    } else {
        Py_INCREF(self->file);
    }

    /*open BitstreamReader from FLAC file stream
      based on whether it's a low-level file object*/
    if (PyFile_Check(self->file)) {
        /*open bitstream through file object*/
        self->bitstream = br_open(PyFile_AsFile(self->file), BS_BIG_ENDIAN);
    } else {
        /*treat file as Python-implemented file-like object*/
        self->bitstream = br_open_external(
            self->file,
            BS_BIG_ENDIAN,
            4096,
            (ext_read_f)br_read_python,
            (ext_close_f)bs_close_python,
            (ext_free_f)bs_free_python_nodecref);
    }

    /*read the STREAMINFO block, SEEKTABLE block
      and setup the total number of samples to read*/
    if (flacdec_read_metadata(self->bitstream,
                              &(self->streaminfo),
                              self->seektable,
                              &(self->channel_mask))) {
        self->streaminfo.channels = 0;
        return -1;
    }

    if (PyFile_Check(self->file)) {
        /*place mark at beginning of stream but after metadata
          in case seeking is needed*/
        self->bitstream->mark(self->bitstream);
    }

    self->remaining_samples = self->streaminfo.total_samples;

    /*initialize the output MD5 sum*/
    audiotools__MD5Init(&(self->md5));
    self->perform_validation = 1;
    self->stream_finalized = 0;

    /*setup a framelist generator function*/
    if ((self->audiotools_pcm = open_audiotools_pcm()) == NULL)
        return -1;

    /*mark stream as not closed and ready for reading*/
    self->closed = 0;

    return 0;
}
Ejemplo n.º 13
0
int
ALACDecoder_init(decoders_ALACDecoder *self,
                 PyObject *args, PyObject *kwds)
{
    PyObject *file;
    unsigned atom_size;
    char atom_name[4];
    int got_decoding_parameters = 0;
    int got_seektable = 0;

    self->bitstream = NULL;
    self->mdat_start = NULL;
    self->total_pcm_frames = 0;
    self->read_pcm_frames = 0;
    self->seektable = NULL;
    self->closed = 0;
    self->audiotools_pcm = NULL;

    /*setup some dummy parameters*/
    self->params.block_size = 4096;
    self->params.history_multiplier = 40;
    self->params.initial_history = 10;
    self->params.maximum_K = 14;

    if (!PyArg_ParseTuple(args, "O", &file)) {
        return -1;
    } else {
        Py_INCREF(file);
    }

    self->bitstream = br_open_external(file,
                                       BS_BIG_ENDIAN,
                                       4096,
                                       br_read_python,
                                       bs_setpos_python,
                                       bs_getpos_python,
                                       bs_free_pos_python,
                                       bs_fseek_python,
                                       bs_close_python,
                                       bs_free_python_decref);

    /*walk through atoms*/
    while (read_atom_header(self->bitstream, &atom_size, atom_name)) {
        if (!memcmp(atom_name, "mdat", 4)) {
            /*get mdat atom's starting position*/
            if (self->mdat_start) {
                PyErr_SetString(PyExc_ValueError,
                                "multiple mdat atoms found in stream");
                return -1;
            } else {
                self->mdat_start = self->bitstream->getpos(self->bitstream);
                self->bitstream->seek(self->bitstream,
                                      atom_size - 8,
                                      BS_SEEK_CUR);
            }
        } else if (!memcmp(atom_name, "moov", 4)) {
            /*find and parse metadata from moov atom*/

            struct qt_atom *moov_atom;

            if (!setjmp(*br_try(self->bitstream))) {
                moov_atom = qt_atom_parse_by_name(self->bitstream,
                                                  atom_size,
                                                  atom_name);

                br_etry(self->bitstream);
            } else {
                br_etry(self->bitstream);
                PyErr_SetString(PyExc_IOError, "I/O error parsing moov atom");
                return -1;
            }

            if (!got_decoding_parameters &&
                get_decoding_parameters(self, moov_atom)) {

                /*this is an arbitrary block size limit
                  to keep from blowing up the stack

                  Apple's reference encoder uses 4096 exclusively
                  but the file format allows sizes up to 32 bits(!)
                  which would break the whole world if anybody used
                  them all for a single ALAC frame.

                  so I'll just assume such files are malicious
                  and reject them outright*/
                if (self->params.block_size > 65535) {
                    PyErr_SetString(PyExc_ValueError, "block size too large");
                    return -1;
                }

                got_decoding_parameters = 1;
            }

            if (!got_seektable && get_seektable(self, moov_atom)) {
                got_seektable = 1;
            }

            moov_atom->free(moov_atom);
        } else {
            /*skip remaining atoms*/

            if (atom_size >= 8) {
                self->bitstream->seek(self->bitstream,
                                      atom_size - 8,
                                      BS_SEEK_CUR);
            }
        }
    }

    if (!got_decoding_parameters) {
        PyErr_SetString(PyExc_ValueError, "no decoding parameters");
        return -1;
    }

    /*seek to start of mdat atom*/
    if (self->mdat_start) {
        self->bitstream->setpos(self->bitstream, self->mdat_start);
    } else {
        PyErr_SetString(PyExc_ValueError, "no mdat atom found in stream");
        return -1;
    }

    /*open FrameList generator*/
    if ((self->audiotools_pcm = open_audiotools_pcm()) == NULL) {
        return -1;
    }

    return 0;
}
Ejemplo n.º 14
0
int
OggFlacDecoder_init(decoders_OggFlacDecoder *self,
                    PyObject *args, PyObject *kwds) {
    char* filename;
    ogg_status result;
    uint16_t header_packets;

    self->ogg_stream = NULL;
    self->ogg_file = NULL;
    self->subframe_data = aa_int_new();
    self->residuals = a_int_new();
    self->qlp_coeffs = a_int_new();
    self->framelist_data = a_int_new();
    self->audiotools_pcm = NULL;
    self->packet = br_substream_new(BS_BIG_ENDIAN);
    self->stream_finalized = 0;

    if (!PyArg_ParseTuple(args, "si", &filename, &(self->channel_mask)))
        return -1;

    if (self->channel_mask < 0) {
        PyErr_SetString(PyExc_ValueError, "channel_mask must be >= 0");
        return -1;
    }

    self->ogg_file = fopen(filename, "rb");
    if (self->ogg_file == NULL) {
        PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
        return -1;
    } else {
        self->ogg_stream = oggreader_open(self->ogg_file);
    }

    /*the first packet should be the FLAC's STREAMINFO*/
    if ((result = oggreader_next_packet(self->ogg_stream,
                                        self->packet)) == OGG_OK) {
        if (!oggflac_read_streaminfo(self->packet,
                                     &(self->streaminfo),
                                     &header_packets))
            return -1;
    } else {
        PyErr_SetString(ogg_exception(result), ogg_strerror(result));
        return -1;
    }

    /*skip subsequent header packets*/
    for (; header_packets > 0; header_packets--) {
        if ((result = oggreader_next_packet(self->ogg_stream,
                                            self->packet)) != OGG_OK) {
            PyErr_SetString(ogg_exception(result), ogg_strerror(result));
            return -1;
        }
    }

    /*initialize the output MD5 sum*/
    audiotools__MD5Init(&(self->md5));

    /*add callback for CRC16 calculation*/
    br_add_callback(self->packet, (bs_callback_f)flac_crc16, &(self->crc16));

    /*setup a framelist generator function*/
    if ((self->audiotools_pcm = open_audiotools_pcm()) == NULL)
        return -1;

    /*mark stream as not closed and ready for reading*/
    self->closed = 0;

    return 0;
}
Ejemplo n.º 15
0
int
DVDA_Title_init(decoders_DVDA_Title *self, PyObject *args, PyObject *kwds) {
    static char *kwlist[] = {"audio_ts",
                             "titleset",
                             "start_sector",
                             "end_sector",

                             "cdrom",
                             NULL};
    char* audio_ts;
    unsigned titleset;
    unsigned start_sector;
    unsigned end_sector;
#else
int
    DVDA_Title_init(decoders_DVDA_Title *self,
                    char* audio_ts,
                    unsigned titleset,
                    unsigned start_sector,
                    unsigned end_sector)
{
#endif
    char* cdrom = NULL;

    self->sector_reader = NULL;
    self->packet_reader = NULL;

    self->packet_data = buf_new();

    self->frames = buf_new();

    self->pcm_frames_remaining = 0;

    self->bits_per_sample = 0;
    self->sample_rate = 0;
    self->channel_count = 0;
    self->channel_mask = 0;

    self->mlp_decoder = open_mlp_decoder(self->frames);

    self->codec_framelist = aa_int_new();
    self->output_framelist = aa_int_new();

#ifndef STANDALONE
    if ((self->audiotools_pcm = open_audiotools_pcm()) == NULL)
        return -1;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "sIII|s",
                                     kwlist,
                                     &audio_ts,
                                     &titleset,
                                     &start_sector,
                                     &end_sector,
                                     &cdrom))
        return -1;
#endif

    /*setup a sector reader according to AUDIO_TS and cdrom device*/
    if ((self->sector_reader = open_sector_reader(audio_ts,
                                                  titleset,
                                                  cdrom)) == NULL) {
#ifndef STANDALONE
        PyErr_SetFromErrnoWithFilename(PyExc_IOError, audio_ts);
#endif
        return -1;
    }

    /*setup a packet reader according to start and end sector
      this packet reader will be shared by all returned DVDA_Tracks*/
    self->packet_reader = open_packet_reader(self->sector_reader,
                                             start_sector,
                                             end_sector);

    return 0;
}
Ejemplo n.º 16
0
int
SameSample_init(decoders_SameSample* self, PyObject *args, PyObject *kwds)
{
    static char *kwlist[] = {"sample",
                             "total_pcm_frames",
                             "sample_rate",
                             "channels",
                             "channel_mask",
                             "bits_per_sample",
                             NULL};

    self->closed = 0;
    if ((self->audiotools_pcm = open_audiotools_pcm()) == NULL)
        return -1;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "iiiiii", kwlist,
                                     &(self->sample),
                                     &(self->total_pcm_frames),
                                     &(self->sample_rate),
                                     &(self->channels),
                                     &(self->channel_mask),
                                     &(self->bits_per_sample)))
        return -1;

    /*sanity-check input parameters*/
    if (self->total_pcm_frames < 0) {
        PyErr_SetString(PyExc_ValueError, "invalid number of total_pcm_frames");
        return -1;
    }
    if (self->sample_rate <= 0) {
        PyErr_SetString(PyExc_ValueError, "invalid sample_rate");
        return -1;
    }
    if (self->channels <= 0) {
        PyErr_SetString(PyExc_ValueError, "invalid channels");
    }
    switch (self->bits_per_sample) {
    case 8:
        if ((-128 <= self->sample) && (self->sample <= 127)) {
            break;
        } else {
            PyErr_SetString(PyExc_ValueError, "invalid sample value");
            return -1;
        }
    case 16:
        if ((-32768 <= self->sample) && (self->sample <= 32767)) {
            break;
        } else {
            PyErr_SetString(PyExc_ValueError, "invalid sample value");
            return -1;
        }
    case 24:
        if ((-8388608 <= self->sample) && (self->sample <= 8388607)) {
            break;
        } else {
            PyErr_SetString(PyExc_ValueError, "invalid sample value");
            return -1;
        }
    default:
        PyErr_SetString(PyExc_ValueError, "invalid bits_per_sample");
        return -1;
    }

    self->remaining_pcm_frames = self->total_pcm_frames;

    return 0;
}