Example #1
0
static PyObject*
ALACDecoder_read(decoders_ALACDecoder* self, PyObject *args)
{
    pcm_FrameList *framelist;
    status_t status;
    unsigned pcm_frames_read;

    if (self->closed) {
        PyErr_SetString(PyExc_ValueError, "cannot read closed stream");
        return NULL;
    }

    if (self->read_pcm_frames >= self->total_pcm_frames) {
        return empty_FrameList(self->audiotools_pcm,
                               self->channels,
                               self->bits_per_sample);
    }

    /*build FrameList based on alac decoding parameters*/
    framelist = new_FrameList(self->audiotools_pcm,
                              self->channels,
                              self->bits_per_sample,
                              self->params.block_size);

    /*decode ALAC frameset to FrameList*/
    if (!setjmp(*br_try(self->bitstream))) {
        status = decode_frameset(self,
                                 &pcm_frames_read,
                                 framelist->samples);
        br_etry(self->bitstream);
    } else {
        br_etry(self->bitstream);
        Py_DECREF((PyObject*)framelist);
        PyErr_SetString(PyExc_IOError, "I/O error reading stream");
        return NULL;
    }

    if (status != OK) {
        Py_DECREF((PyObject*)framelist);
        PyErr_SetString(alac_exception(status), alac_strerror(status));
        return NULL;
    }

    /*constrain FrameList to actual amount of PCM frames read
      which may be less than block size at the end of stream*/
    framelist->frames = pcm_frames_read;
    framelist->samples_length = pcm_frames_read * self->channels;

    /*reorder FrameList to .wav order*/
    reorder_channels(pcm_frames_read,
                     self->channels,
                     framelist->samples);

    self->read_pcm_frames += pcm_frames_read;

    /*return populated FrameList*/
    return (PyObject*)framelist;
}
static PyObject*
CDDAReader_read(cdio_CDDAReader* self, PyObject *args)
{
    int pcm_frames;
    unsigned sectors_to_read;
    pcm_FrameList *framelist;
    int sectors_read;
    PyThreadState *thread_state = NULL;

    if (!PyArg_ParseTuple(args, "i", &pcm_frames)) {
        return NULL;
    }

    if (self->closed) {
        PyErr_SetString(PyExc_ValueError, "cannot read closed stream");
        return NULL;
    }

    sectors_to_read = MAX(pcm_frames, 0) / (44100 / 75);
    if (sectors_to_read < 1) {
        sectors_to_read = 1;
    }

    framelist = new_FrameList(self->audiotools_pcm,
                              2,
                              16,
                              sectors_to_read * (44100 / 75));

    /*if logging is in progress, only let a single thread
      into this function at once so that the global callback
      can be set and used atomically

      since the callback function doesn't take any state
      we're forced to stick it in a global variable*/
    if (!self->is_logging) {
        thread_state = PyEval_SaveThread();
    }
    sectors_read = self->read(self, sectors_to_read, framelist->samples);
    if (!self->is_logging) {
        PyEval_RestoreThread(thread_state);
    }

    if (sectors_read >= 0) {
        /*reduce length of framelist if fewer samples are read*/
        framelist->frames = sectors_read * (44100 / 75);
        return (PyObject*)framelist;
    } else {
        Py_DECREF((PyObject*)framelist);
        PyErr_SetString(PyExc_IOError, "I/O error reading stream");
        return NULL;
    }
}
Example #3
0
static PyObject*
Sine_Stereo_read(decoders_Sine_Stereo* self, PyObject* args) {
    pcm_FrameList *framelist;
    int requested_frames;
    int frames_to_read;
    int i;
    double d;
    int ia;
    int *samples;

    if (self->closed) {
        PyErr_SetString(PyExc_ValueError, "cannot read closed stream");
        return NULL;
    }

    if (!PyArg_ParseTuple(args, "i", &requested_frames))
        return NULL;

    frames_to_read = MIN(MAX(requested_frames, 1), self->remaining_pcm_frames);

    framelist = new_FrameList(self->audiotools_pcm,
                              2,
                              self->bits_per_sample,
                              frames_to_read);
    samples = framelist->samples;

    for (i = 0; i < frames_to_read; i++) {
        d = ((self->a1 * sin(self->theta1)) +
             (self->a2 * sin(self->theta2))) * (double)(self->full_scale);
        ia = (int)(d + 0.5);
        put_sample(samples, 0, 2, i, ia);
        d = -((self->a1 * sin(self->theta1 * self->fmult)) +
              (self->a2 * sin(self->theta2 * self->fmult))) *
            (double)(self->full_scale);
        ia = (int)(d + 0.5);
        put_sample(samples, 1, 2, i, ia);
        self->theta1 += self->delta1;
        self->theta2 += self->delta2;
    }

    self->remaining_pcm_frames -= frames_to_read;

    return (PyObject*)framelist;
}
static PyObject*
MP3Decoder_read(decoders_MP3Decoder* self, PyObject *args)
{
    pcm_FrameList *framelist;
    int *samples;
    static int16_t buffer[BUFFER_SIZE];
    size_t buffer_size;
    size_t i;

    if (self->closed) {
        PyErr_SetString(PyExc_ValueError, "stream is closed");
        return NULL;
    }

    /*perform mpg123_read() to output buffer*/
    switch (mpg123_read(self->handle,
                        (unsigned char*)buffer,
                        BUFFER_SIZE,
                        &buffer_size)) {
    case MPG123_DONE:
        /*return empty framelist*/
        return empty_FrameList(self->audiotools_pcm,
                               self->channels,
                               16);
    case MPG123_OK:
        framelist = new_FrameList(self->audiotools_pcm,
                                  self->channels,
                                  BITS_PER_SAMPLE,
                                  (unsigned)(buffer_size / 2 / self->channels));

        samples = framelist->samples;

        for (i = 0; i < (buffer_size / 2); i++) {
            samples[i] = buffer[i];
        }

        return (PyObject*)framelist;
    default:
        /*raise exception*/
        PyErr_SetString(PyExc_ValueError, "error decoding MP3 frame");
        return NULL;
    }
}
Example #5
0
static PyObject*
Sine_Simple_read(decoders_Sine_Simple* self, PyObject* args) {
    pcm_FrameList *framelist;
    int requested_frames;
    int frames_to_read;
    int i;
    double d;
    int ia;
    int *samples;

    if (self->closed) {
        PyErr_SetString(PyExc_ValueError, "cannot read closed stream");
        return NULL;
    }

    if (!PyArg_ParseTuple(args, "i", &requested_frames))
        return NULL;

    frames_to_read = MIN(MAX(requested_frames, 1), self->remaining_pcm_frames);

    framelist = new_FrameList(self->audiotools_pcm,
                              1,
                              self->bits_per_sample,
                              frames_to_read);

    samples = framelist->samples;

    for (i = 0; i < frames_to_read; i++) {
        d = (double)(self->max_value) *
            sin(((M_PI * 2) *
                 (double)(self->i % self->count)) /
                (double)(self->count));
        ia = (int)(round(d));
        *samples = ia;
        samples++;
        self->i += 1;
    }

    self->remaining_pcm_frames -= frames_to_read;

    return (PyObject*)framelist;
}
static PyObject*
TrackReader_read(dvda_TrackReader *self, PyObject *args)
{
    int pcm_frames;
    unsigned requested_pcm_frames;
    unsigned received_pcm_frames;
    pcm_FrameList *framelist;
    const unsigned channel_count = dvda_channel_count(self->reader);
    const unsigned bits_per_sample = dvda_bits_per_sample(self->reader);

    /*if closed, raise ValueError*/
    if (self->closed) {
        PyErr_SetString(PyExc_ValueError, "unable to read closed stream");
        return NULL;
    }

    if (!PyArg_ParseTuple(args, "i", &pcm_frames)) {
        return NULL;
    }

    /*restrict requested number of PCM frames to a reasonable value*/
    requested_pcm_frames = MIN(MAX(pcm_frames, 1), 1 << 20);

    /*grab empty FrameList and make a buffer for it*/
    if ((framelist = new_FrameList(self->audiotools_pcm,
                                   channel_count,
                                   bits_per_sample,
                                   requested_pcm_frames)) == NULL) {
        return NULL;
    }

    /*perform read to FrameList's buffer*/
    received_pcm_frames = dvda_read(self->reader,
                                    requested_pcm_frames,
                                    framelist->samples);

    /*fill in remaining FrameList parameters*/
    framelist->frames = received_pcm_frames;

    /*return FrameList*/
    return (PyObject*)framelist;
}
Example #7
0
static PyObject*
SameSample_read(decoders_SameSample* self, PyObject* args)
{
    int pcm_frames;
    unsigned total_samples;
    pcm_FrameList *framelist;
    const int sample = self->sample;
    int *samples;

    if (self->closed) {
        PyErr_SetString(PyExc_ValueError, "unable to read closed stream");
        return NULL;
    }

    if (!PyArg_ParseTuple(args, "i", &pcm_frames))
        return NULL;

    pcm_frames = MIN(MAX(pcm_frames, 1), self->remaining_pcm_frames);

    framelist = new_FrameList(self->audiotools_pcm,
                              self->channels,
                              self->bits_per_sample,
                              pcm_frames);

    samples = framelist->samples;

    for (total_samples = pcm_frames * self->channels;
         total_samples;
         total_samples--) {
         *samples = sample;
         samples++;
    }

    self->remaining_pcm_frames -= pcm_frames;

    return (PyObject*)framelist;
}
Example #8
0
static PyObject*
OpusDecoder_read(decoders_OpusDecoder* self, PyObject *args)
{
    static opus_int16 pcm[BUF_SIZE];
    int pcm_frames_read;

    if (self->closed) {
        PyErr_SetString(PyExc_ValueError, "stream is closed");
        return NULL;
    }

    if ((pcm_frames_read = op_read(self->opus_file,
                                   pcm,
                                   BUF_SIZE,
                                   NULL)) >= 0) {
        const int channel_count = op_head(self->opus_file, -1)->channel_count;
        int i;
        pcm_FrameList *framelist = new_FrameList(self->audiotools_pcm,
                                                 channel_count,
                                                 BITS_PER_SAMPLE,
                                                 pcm_frames_read);
        int *samples = framelist->samples;

        for (i = 0; i < pcm_frames_read * channel_count; i++) {
            samples[i] = pcm[i];
        }

        /*reorder channels to .wav order if necessary*/
        switch (self->channel_count) {
        case 1:
        case 2:
        default:
            /*no change*/
            break;
        case 3:
            /*fL fC fR -> fL fR fC*/
            swap_channel_data(samples, 1, 2,
                              self->channel_count,
                              (unsigned)pcm_frames_read);
            break;
        case 4:
            /*fL fR bL bR -> fL fR bL bR*/
            /*no change*/
            break;
        case 5:
            /*fL fC fR bL bR -> fL fR fC bL bR*/
            swap_channel_data(samples, 1, 2,
                              self->channel_count,
                              (unsigned)pcm_frames_read);
            break;
        case 6:
            /*fL fC fR bL bR LFE -> fL fR fC bL bR LFE*/
            swap_channel_data(samples, 1, 2,
                              self->channel_count,
                              (unsigned)pcm_frames_read);

            /*fL fR fC bL bR LFE -> fL fR fC LFE bR bL*/
            swap_channel_data(samples, 3, 5,
                              self->channel_count,
                              (unsigned)pcm_frames_read);

            /*fL fR fC LFE bR bL -> fL fR fC LFE bL bR*/
            swap_channel_data(samples, 4, 5,
                              self->channel_count,
                              (unsigned)pcm_frames_read);
            break;
        case 7:
            /*fL fC fR sL sR bC LFE -> fL fR fC sL sR bC LFE*/
            swap_channel_data(samples, 1, 2,
                              self->channel_count,
                              (unsigned)pcm_frames_read);

            /*fL fR fC sL sR bC LFE -> fL fR fC LFE sR bC sL*/
            swap_channel_data(samples, 3, 6,
                              self->channel_count,
                              (unsigned)pcm_frames_read);

            /*fL fR fC LFE sR bC sL -> fL fR fC LFE bC sR sL*/
            swap_channel_data(samples, 4, 5,
                              self->channel_count,
                              (unsigned)pcm_frames_read);

            /*fL fR fC LFE bC sR sL -> fL fR fC LFE bC sL sR*/
            swap_channel_data(samples, 5, 6,
                              self->channel_count,
                              (unsigned)pcm_frames_read);
            break;
        case 8:
            /*fL fC fR sL sR bL bR LFE -> fL fR fC sL sR bL bR LFE*/
            swap_channel_data(samples, 1, 2,
                              self->channel_count,
                              (unsigned)pcm_frames_read);

            /*fL fR fC sL sR bL bR LFE -> fL fR fC LFE sR bL bR sL*/
            swap_channel_data(samples, 3, 6,
                              self->channel_count,
                              (unsigned)pcm_frames_read);

            /*fL fR fC LFE sR bL bR sL -> fL fR fC LFE bL sR bR sL*/
            swap_channel_data(samples, 4, 5,
                              self->channel_count,
                              (unsigned)pcm_frames_read);

            /*fL fR fC LFE bL sR bR sL -> fL fR fC LFE bL bR sR sL*/
            swap_channel_data(samples, 5, 6,
                              self->channel_count,
                              (unsigned)pcm_frames_read);

            /*fL fR fC LFE bL bR sR sL -> fL fR fC LFE bL bR sL sR*/
            swap_channel_data(samples, 6, 7,
                              self->channel_count,
                              (unsigned)pcm_frames_read);
            break;
        }

        return (PyObject*)framelist;
    } else {
        /*some sort of read error occurred*/
        PyErr_SetString(PyExc_ValueError, "error reading from file");
        return NULL;
    }
}
static PyObject*
VorbisDecoder_read(decoders_VorbisDecoder *self, PyObject *args) {
    int current_bitstream;
    long samples_read;
    float **pcm_channels;

    if (self->closed) {
        PyErr_SetString(PyExc_ValueError, "stream is closed");
        return NULL;
    }

    samples_read = ov_read_float(&(self->vorbisfile),
                                 &pcm_channels,
                                 4096,
                                 &current_bitstream);

    if (samples_read >= 0) {
        /*convert floating point samples to integer-based ones*/
        pcm_FrameList *framelist;
        int *samples;
        int c;

        if (samples_read == 0) {
            if (self->vorbisfile.os.e_o_s == 0) {
                /*EOF encountered without EOF being marked in stream*/
                PyErr_SetString(PyExc_IOError,
                                "I/O error reading from Ogg stream");
                return NULL;
            } else {
                return empty_FrameList(self->audiotools_pcm,
                                       self->channel_count,
                                       BITS_PER_SAMPLE);
            }
        }

        framelist = new_FrameList(self->audiotools_pcm,
                                  self->channel_count,
                                  BITS_PER_SAMPLE,
                                  (unsigned)samples_read);

        samples = framelist->samples;

        for (c = 0; c < self->channel_count; c++) {
            int channel[samples_read];

            float_to_int_converter(BITS_PER_SAMPLE)(
                (unsigned)samples_read,
                pcm_channels[c],
                channel);

            put_channel_data(samples,
                             c,
                             self->channel_count,
                             (unsigned)samples_read,
                             channel);
        }

        /*reorder channels to .wav order if necessary*/
        switch (self->channel_count) {
        case 1:
        case 2:
        default:
            /*no change*/
            break;
        case 3:
            /*fL fC fR -> fL fR fC*/
            swap_channel_data(samples, 1, 2,
                              self->channel_count, (unsigned)samples_read);
            break;
        case 4:
            /*fL fR bL bR -> fL fR bL bR*/
            /*no change*/
            break;
        case 5:
            /*fL fC fR bL bR -> fL fR fC bL bR*/
            swap_channel_data(samples, 1, 2,
                              self->channel_count, (unsigned)samples_read);
            break;
        case 6:
            /*fL fC fR bL bR LFE -> fL fR fC bL bR LFE*/
            swap_channel_data(samples, 1, 2,
                              self->channel_count, (unsigned)samples_read);

            /*fL fR fC bL bR LFE -> fL fR fC LFE bR bL*/
            swap_channel_data(samples, 3, 5,
                              self->channel_count, (unsigned)samples_read);

            /*fL fR fC LFE bR bL -> fL fR fC LFE bL bR*/
            swap_channel_data(samples, 4, 5,
                              self->channel_count, (unsigned)samples_read);
            break;
        case 7:
            /*fL fC fR sL sR bC LFE -> fL fR fC sL sR bC LFE*/
            swap_channel_data(samples, 1, 2,
                              self->channel_count, (unsigned)samples_read);

            /*fL fR fC sL sR bC LFE -> fL fR fC LFE sR bC sL*/
            swap_channel_data(samples, 3, 6,
                              self->channel_count, (unsigned)samples_read);

            /*fL fR fC LFE sR bC sL -> fL fR fC LFE bC sR sL*/
            swap_channel_data(samples, 4, 5,
                              self->channel_count, (unsigned)samples_read);

            /*fL fR fC LFE bC sR sL -> fL fR fC LFE bC sL sR*/
            swap_channel_data(samples, 5, 6,
                              self->channel_count, (unsigned)samples_read);
            break;
        case 8:
            /*fL fC fR sL sR bL bR LFE -> fL fR fC sL sR bL bR LFE*/
            swap_channel_data(samples, 1, 2,
                              self->channel_count, (unsigned)samples_read);

            /*fL fR fC sL sR bL bR LFE -> fL fR fC LFE sR bL bR sL*/
            swap_channel_data(samples, 3, 6,
                              self->channel_count, (unsigned)samples_read);

            /*fL fR fC LFE sR bL bR sL -> fL fR fC LFE bL sR bR sL*/
            swap_channel_data(samples, 4, 5,
                              self->channel_count, (unsigned)samples_read);

            /*fL fR fC LFE bL sR bR sL -> fL fR fC LFE bL bR sR sL*/
            swap_channel_data(samples, 5, 6,
                              self->channel_count, (unsigned)samples_read);

            /*fL fR fC LFE bL bR sR sL -> fL fR fC LFE bL bR sL sR*/
            swap_channel_data(samples, 6, 7,
                              self->channel_count, (unsigned)samples_read);
            break;
        }

        return (PyObject*)framelist;
    } else {
        switch (samples_read) {
        case OV_HOLE:
            PyErr_SetString(PyExc_ValueError, "data interruption detected");
            return NULL;
        case OV_EBADLINK:
            PyErr_SetString(PyExc_ValueError, "invalid stream section");
            return NULL;
        case OV_EINVAL:
            PyErr_SetString(PyExc_ValueError, "initial file headers corrupt");
            return NULL;
        default:
            PyErr_SetString(PyExc_ValueError, "unspecified error");
            return NULL;
        }
    }
}