Casting uint8_t* to int16_t8 in C

30 views Asked by At

I am attempting to adapt an example from here which reads PDM microphone data and prints it to a computer using serial USB. I want to bypass the filtering stage in the example so I can write my own filter while maintaining the other functions.

My current plan is to pass the incoming PDM data as the output of the filtering stage. However the input is given as uint8_t* in and the output is int16_t* out. What would be the easiest way of casting the in as out ? I have copied the whole function below.

int pdm_microphone_read(int16_t* buffer, size_t samples) {
    int filter_stride = (pdm_mic.filter.Fs / 1000);
    samples = (samples / filter_stride) * filter_stride;

    if (samples > pdm_mic.config.sample_buffer_size) {
        samples = pdm_mic.config.sample_buffer_size;
    }

    if (pdm_mic.raw_buffer_write_index == pdm_mic.raw_buffer_read_index) {
        return 0;
    }

    uint8_t* in = pdm_mic.raw_buffer[pdm_mic.raw_buffer_read_index];
    int16_t* out = buffer; //need to change this line
    
    pdm_mic.raw_buffer_read_index++;

    for (int i = 0; i < samples; i += filter_stride) {
#if PDM_DECIMATION == 64
        Open_PDM_Filter_64(in, out, pdm_mic.filter_volume, &pdm_mic.filter);
#elif PDM_DECIMATION == 128
        Open_PDM_Filter_128(in, out, pdm_mic.filter_volume, &pdm_mic.filter);
#else
        #error "Unsupported PDM_DECIMATION value!"
#endif

        in += filter_stride * (PDM_DECIMATION / 8);
        out += filter_stride;
    }

    return samples;
}
1

There are 1 answers

0
Xeno On

If I understand correctly, you need to copy the list list entry by entry. I would use a for loop:

for (int p1 = 0, p2 = 0; p1 < IN_SIZE; p1++, p2++) {
    out[p1] = (int16_t)in[p2];
}