diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2017-07-24 11:11:16 +0200 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2017-07-24 11:11:16 +0200 |
commit | 864cebd1d753a603de9a24d26a830bad87e2a42f (patch) | |
tree | b2f838c8b4e42285cd30ae697b80bdd8feb40aff | |
parent | 0dbcb9a977fd9b91d86c79657d1007c77feaffd9 (diff) | |
download | ODR-AudioEnc-864cebd1d753a603de9a24d26a830bad87e2a42f.tar.gz ODR-AudioEnc-864cebd1d753a603de9a24d26a830bad87e2a42f.tar.bz2 ODR-AudioEnc-864cebd1d753a603de9a24d26a830bad87e2a42f.zip |
Add wav file writer
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | src/wavfile.cpp (renamed from src/wavfile.c) | 83 | ||||
-rw-r--r-- | src/wavfile.h | 20 |
3 files changed, 94 insertions, 11 deletions
diff --git a/Makefile.am b/Makefile.am index 493dbeb..eb5cda6 100644 --- a/Makefile.am +++ b/Makefile.am @@ -94,7 +94,7 @@ odr_audioenc_SOURCES = src/odr-audioenc.cpp \ src/zmq.hpp \ src/utils.c \ src/utils.h \ - src/wavfile.c \ + src/wavfile.cpp \ src/common.h \ $(FEC_SOURCES) diff --git a/src/wavfile.c b/src/wavfile.cpp index 03f9c94..dbc1e13 100644 --- a/src/wavfile.c +++ b/src/wavfile.cpp @@ -1,5 +1,6 @@ /* ------------------------------------------------------------------ * Copyright (C) 2009 Martin Storsjo + * Copyright (C) 2017 Matthias P. Braendli * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,10 +18,11 @@ */ #include "wavfile.h" -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <stdint.h> +#include <cstdio> +#include <cstdlib> +#include <cstring> +#include <cstdint> +#include <stdexcept> #define TAG(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d)) @@ -191,3 +193,76 @@ int wav_read_data(void* obj, unsigned char* data, unsigned int length) { return n; } +//============== WAV writer functions + +struct wavfile_header { + char riff_tag[4]; + int riff_length; + char wave_tag[4]; + char fmt_tag[4]; + int fmt_length; + short audio_format; + short num_channels; + int sample_rate; + int byte_rate; + short block_align; + short bits_per_sample; + char data_tag[4]; + int data_length; +}; + +WavWriter::WavWriter(const char *filename, int rate) +{ + struct wavfile_header header; + + int samples_per_second = rate; + int bits_per_sample = 16; + + strncpy(header.riff_tag,"RIFF",4); + strncpy(header.wave_tag,"WAVE",4); + strncpy(header.fmt_tag,"fmt ",4); + strncpy(header.data_tag,"data",4); + + header.riff_length = 0; + header.fmt_length = 16; + header.audio_format = 1; + header.num_channels = 2; + header.sample_rate = samples_per_second; + header.byte_rate = samples_per_second*(bits_per_sample/8); + header.block_align = bits_per_sample/8; + header.bits_per_sample = bits_per_sample; + header.data_length = 0; + + m_fd = fopen(filename, "w+"); + if (not m_fd) { + throw std::runtime_error("Could not open wav file"); + } + + fwrite(&header,sizeof(header),1,m_fd); + + fflush(m_fd); +} + +WavWriter::~WavWriter() +{ + // The wav file header contains the full file size, we must + // write this at the end + + int file_length = ftell(m_fd); + + int data_length = file_length - sizeof(struct wavfile_header); + fseek(m_fd,sizeof(struct wavfile_header) - sizeof(int),SEEK_SET); + fwrite(&data_length,sizeof(data_length),1,m_fd); + + int riff_length = file_length - 8; + fseek(m_fd,4,SEEK_SET); + fwrite(&riff_length,sizeof(riff_length),1,m_fd); + + fclose(m_fd); +} + +void WavWriter::write_data(short data[], int length) +{ + fwrite(data,sizeof(short),length,m_fd); +} + diff --git a/src/wavfile.h b/src/wavfile.h index e121b33..92ea56f 100644 --- a/src/wavfile.h +++ b/src/wavfile.h @@ -1,5 +1,6 @@ /* ------------------------------------------------------------------ * Copyright (C) 2009 Martin Storsjo + * Copyright (C) 2017 Matthias P. Braendli * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,9 +19,7 @@ #pragma once -#ifdef __cplusplus -extern "C" { -#endif +#include <cstdio> void* wav_read_open(const char *filename); void wav_read_close(void* obj); @@ -28,7 +27,16 @@ void wav_read_close(void* obj); int wav_get_header(void* obj, int* format, int* channels, int* sample_rate, int* bits_per_sample, unsigned int* data_length); int wav_read_data(void* obj, unsigned char* data, unsigned int length); -#ifdef __cplusplus -} -#endif +class WavWriter { + public: + WavWriter(const char *filename, int rate); + ~WavWriter(); + WavWriter(const WavWriter& other) = delete; + WavWriter& operator=(const WavWriter& other) = delete; + + void write_data(short *data, int length); + + private: + FILE *m_fd = nullptr; +}; |