tarina

git clone https://git.tarina.org/tarina
Log | Files | Refs | README | LICENSE

convert.c (2596B)


      1 /*
      2  * Copyright (C) 2013-2015 Intel Corporation
      3  *
      4  * This program is free software; you can redistribute it and/or modify
      5  * it under the terms of the GNU General Public License as published by
      6  * the Free Software Foundation; either version 2 of the License, or
      7  * (at your option) any later version.
      8  *
      9  * This program is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12  * GNU General Public License for more details.
     13  *
     14  */
     15 
     16 #include <stdio.h>
     17 #include <stddef.h>
     18 #include <stdlib.h>
     19 #include <stdint.h>
     20 
     21 void convert_uint8_to_float(void *buf, float *val, int samples)
     22 {
     23 	int i;
     24 
     25 	for (i = 0; i < samples; i++)
     26 		val[i] = ((uint8_t *) buf)[i];
     27 }
     28 
     29 void convert_int16_to_float(void *buf, float *val, int samples)
     30 {
     31 	int i;
     32 
     33 	for (i = 0; i < samples; i++)
     34 		val[i] = ((int16_t *) buf)[i];
     35 }
     36 
     37 void convert_int24_to_float(void *buf, float *val, int samples)
     38 {
     39 	int i;
     40 	int32_t tmp;
     41 
     42 	for (i = 0; i < samples; i++) {
     43 		tmp = ((uint8_t *) buf)[i * 3 + 2] << 24;
     44 		tmp |= ((uint8_t *) buf)[i * 3 + 1] << 16;
     45 		tmp |= ((uint8_t *) buf)[i * 3] << 8;
     46 		tmp >>= 8;
     47 		val[i] = tmp;
     48 	}
     49 }
     50 
     51 void convert_int32_to_float(void *buf, float *val, int samples)
     52 {
     53 	int i;
     54 
     55 	for (i = 0; i < samples; i++)
     56 		val[i] = ((int32_t *) buf)[i];
     57 }
     58 
     59 void convert_float_to_uint8(float *val, void *buf, int samples, int channels)
     60 {
     61 	int i, c, idx;
     62 
     63 	for (i = 0; i < samples; i++) {
     64 		for (c = 0; c < channels; c++) {
     65 			idx = i * channels + c;
     66 			((uint8_t *) buf)[idx] = (uint8_t) val[idx];
     67 		}
     68 	}
     69 }
     70 
     71 void convert_float_to_int16(float *val, void *buf, int samples, int channels)
     72 {
     73 	int i, c, idx;
     74 
     75 	for (i = 0; i < samples; i++) {
     76 		for (c = 0; c < channels; c++) {
     77 			idx = i * channels + c;
     78 			((int16_t *) buf)[idx] = (int16_t) val[idx];
     79 		}
     80 	}
     81 }
     82 
     83 void convert_float_to_int24(float *val, void *buf, int samples, int channels)
     84 {
     85 	int i, c, idx_f, idx_i;
     86 	int32_t val_f_i;
     87 
     88 	for (i = 0; i < samples; i++) {
     89 		for (c = 0; c < channels; c++) {
     90 			idx_f = i * channels + c;
     91 			idx_i = 3 * idx_f;
     92 			val_f_i = (int32_t) val[idx_f];
     93 			((int8_t *) buf)[idx_i + 0] =
     94 				(int8_t) (val_f_i & 0xff);
     95 			((int8_t *) buf)[idx_i + 1] =
     96 				(int8_t) ((val_f_i >> 8) & 0xff);
     97 			((int8_t *) buf)[idx_i + 2] =
     98 				(int8_t) ((val_f_i >> 16) & 0xff);
     99 		}
    100 	}
    101 }
    102 
    103 void convert_float_to_int32(float *val, void *buf, int samples, int channels)
    104 {
    105 	int i, c, idx;
    106 
    107 	for (i = 0; i < samples; i++) {
    108 		for (c = 0; c < channels; c++) {
    109 			idx = i * channels + c;
    110 			((int32_t *) buf)[idx] = (int32_t) val[idx];
    111 		}
    112 	}
    113 }