Created
February 5, 2023 17:32
-
-
Save greed9/bc7ff358ab082d3c758e8926ac96ea7b to your computer and use it in GitHub Desktop.
Example of reading (and possibly modifying) a 16 or 32 bit WAV file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (c) 2015-2022, Martin Roth ([email protected]) | |
* | |
* Permission to use, copy, modify, and/or distribute this software for any | |
* purpose with or without fee is hereby granted, provided that the above | |
* copyright notice and this permission notice appear in all copies. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | |
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | |
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | |
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | |
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | |
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | |
* PERFORMANCE OF THIS SOFTWARE. | |
*/ | |
// Heavily based on and slightly modified from: https://github.com/mhroth/tinywav | |
#include "tinywav.h" | |
#include <assert.h> | |
#define NUM_CHANNELS 2 | |
#define SAMPLE_RATE 44100 | |
#define BLOCK_SIZE 512 | |
/* Filter coefficient value: A < 1 */ | |
#define A_VALUE ((float)(0.24)) | |
float y_history = 0.0; | |
// based on: https://www.dsp-weimich.com/digital-signal-processing/iir-first-order-digital-filter/ | |
float IIR_Filter_First_Order(float x_input) | |
{ | |
float y_round; | |
y_history = y_history + A_VALUE * (((float)x_input - y_history)); | |
y_round = y_history; | |
return y_round; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
char *outputPath = "./tiny_output.wav"; | |
if (argc < 2) | |
return -1; | |
const char *inputPath = argv[1]; | |
fprintf(stderr, "Starting\n"); | |
TinyWav twReader; | |
tinywav_open_read(&twReader, inputPath, TW_INLINE); | |
if (twReader.numChannels != NUM_CHANNELS || twReader.h.SampleRate != SAMPLE_RATE) | |
{ | |
fprintf(stderr, "Supplied test wav has wrong format - should be [%d]channels, fs=[%d]\n", NUM_CHANNELS, SAMPLE_RATE); | |
return -1; | |
} | |
TinyWav twWriter; | |
tinywav_open_write(&twWriter, NUM_CHANNELS, SAMPLE_RATE, TW_FLOAT32, TW_INLINE, outputPath); | |
int totalNumSamples = twReader.numFramesInHeader; | |
fprintf(stderr, "%d\n", twReader.numFramesInHeader); | |
int samplesProcessed = 0; | |
while (samplesProcessed < totalNumSamples) | |
{ | |
float buffer[NUM_CHANNELS * BLOCK_SIZE]; | |
int samplesRead = tinywav_read_f(&twReader, buffer, BLOCK_SIZE); | |
assert(samplesRead > 0 && " Could not read from file!"); | |
// printf( "%d\n", samplesRead) ; | |
// mod code | |
/* for (int j = 0 j < 2; J++) | |
{ | |
for (int i = 0; i < samplesRead; i++) | |
{ | |
// buffer[i] = IIR_Filter_First_Order( buffer[i]) ; | |
} | |
} | |
*/ | |
int samplesWritten = tinywav_write_f(&twWriter, buffer, samplesRead); | |
assert(samplesWritten > 0 && "Could not write to file!"); | |
samplesProcessed += samplesRead * NUM_CHANNELS; | |
} | |
tinywav_close_read(&twReader); | |
tinywav_close_write(&twWriter); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment