posix/wav_util: use fopen/fread for wav_reader

This commit is contained in:
Matthias Ringwald 2022-05-09 13:49:35 +02:00
parent 1ff97ac18c
commit 86d6811aca
2 changed files with 18 additions and 34 deletions

View File

@ -37,19 +37,17 @@
#define BTSTACK_FILE__ "wav_util.c"
#include <stdint.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include "wav_util.h"
#include "btstack_util.h"
#include "btstack_debug.h"
/* wav reader state */
static int wav_reader_fd;
static FILE * wav_reader_file;
static int wav_reader_bytes_per_sample = 2;
static struct {
uint8_t num_channels;
@ -78,20 +76,6 @@ static void little_endian_fstore_32(FILE *wav_file, uint32_t value){
fwrite(&buf, 1, 4, wav_file);
}
static ssize_t __read(int fd, void *buf, size_t count){
ssize_t len, pos = 0;
while (count > 0) {
len = read(fd, (int8_t * )buf + pos, count);
if (len <= 0)
return pos;
count -= len;
pos += len;
}
return pos;
}
static void write_wav_header(FILE * wav_file, int total_num_samples, int num_channels, int sample_rate){
unsigned int write_with_bytes_per_sample = 2;
/* write RIFF header */
@ -196,15 +180,14 @@ int wav_writer_write_int16(int num_samples, int16_t * data){
int wav_reader_open(const char * filepath){
memset (&wav_reader_state, 0, sizeof(wav_reader_state));
wav_reader_fd = open(filepath, O_RDONLY);
if (!wav_reader_fd) {
wav_reader_file = fopen(filepath, "rb");
if (wav_reader_file == NULL) {
log_error("Can't open file %s", filepath);
return 1;
}
uint8_t buf[40];
__read(wav_reader_fd, buf, sizeof(buf));
fread(buf, 1, sizeof(buf), wav_reader_file);
wav_reader_state.num_channels = little_endian_read_16(buf, 22);
if ((wav_reader_state.num_channels < 1) || (wav_reader_state.num_channels > 2)) {
@ -232,24 +215,28 @@ uint32_t wav_reader_get_sampling_rate(void){
}
int wav_reader_close(void){
close(wav_reader_fd);
if (wav_reader_file != NULL){
fclose(wav_reader_file);
}
return 0;
}
// Wav data: 8bit is uint8_t; 16bit is int16
int wav_reader_read_int8(int num_samples, int8_t * data){
if (!wav_reader_fd) return 1;
if (wav_reader_file == NULL) {
return 1;
}
int i;
int bytes_read = 0;
for (i=0; i<num_samples; i++){
if (wav_reader_bytes_per_sample == 2){
uint8_t buf[2];
bytes_read +=__read(wav_reader_fd, &buf, 2);
data[i] = buf[1];
bytes_read += fread(buf, 1, sizeof(buf), wav_reader_file);;
data[i] = buf[1];
} else {
uint8_t buf[1];
bytes_read +=__read(wav_reader_fd, &buf, 1);
bytes_read += fread(buf, 1, sizeof(buf), wav_reader_file);;
data[i] = buf[0] + 128;
}
}
@ -261,12 +248,14 @@ int wav_reader_read_int8(int num_samples, int8_t * data){
}
int wav_reader_read_int16(int num_samples, int16_t * data){
if (!wav_reader_fd) return 1;
if (wav_reader_file == NULL) {
return 1;
}
int i;
int bytes_read = 0;
for (i=0; i<num_samples; i++){
uint8_t buf[2];
bytes_read +=__read(wav_reader_fd, &buf, 2);
bytes_read += fread(buf, 1, sizeof(buf), wav_reader_file);;
data[i] = little_endian_read_16(buf, 0);
}
if (bytes_read == num_samples * wav_reader_bytes_per_sample) {

View File

@ -39,11 +39,6 @@
#define WAV_UIL_H
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#if defined __cplusplus
extern "C" {