The tiny wav I/O module is the most simple wav file I/O module you've ever seen.

Dependents:   SimpleWaveRecorderPlayer USBMSD_SD_HelloWorld_FRDM-KL25Z Application-SimpleWaveRecorderPlayerGenerator MbedClock ... more

Committer:
shintamainjp
Date:
Sat Apr 14 02:13:30 2012 +0000
Revision:
0:c853ba46d0b9
Initial version of the tiny wav I/O module for mbed.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shintamainjp 0:c853ba46d0b9 1 /**
shintamainjp 0:c853ba46d0b9 2 * @file wavfile.h
shintamainjp 0:c853ba46d0b9 3 * @author Shinichiro Nakamura
shintamainjp 0:c853ba46d0b9 4 */
shintamainjp 0:c853ba46d0b9 5
shintamainjp 0:c853ba46d0b9 6 /*
shintamainjp 0:c853ba46d0b9 7 * ===============================================================
shintamainjp 0:c853ba46d0b9 8 * Tiny WAV I/O Module
shintamainjp 0:c853ba46d0b9 9 * Version 0.0.1
shintamainjp 0:c853ba46d0b9 10 * ===============================================================
shintamainjp 0:c853ba46d0b9 11 * Copyright (c) 2011-2012 Shinichiro Nakamura
shintamainjp 0:c853ba46d0b9 12 *
shintamainjp 0:c853ba46d0b9 13 * Permission is hereby granted, free of charge, to any person
shintamainjp 0:c853ba46d0b9 14 * obtaining a copy of this software and associated documentation
shintamainjp 0:c853ba46d0b9 15 * files (the "Software"), to deal in the Software without
shintamainjp 0:c853ba46d0b9 16 * restriction, including without limitation the rights to use,
shintamainjp 0:c853ba46d0b9 17 * copy, modify, merge, publish, distribute, sublicense, and/or
shintamainjp 0:c853ba46d0b9 18 * sell copies of the Software, and to permit persons to whom the
shintamainjp 0:c853ba46d0b9 19 * Software is furnished to do so, subject to the following
shintamainjp 0:c853ba46d0b9 20 * conditions:
shintamainjp 0:c853ba46d0b9 21 *
shintamainjp 0:c853ba46d0b9 22 * The above copyright notice and this permission notice shall be
shintamainjp 0:c853ba46d0b9 23 * included in all copies or substantial portions of the Software.
shintamainjp 0:c853ba46d0b9 24 *
shintamainjp 0:c853ba46d0b9 25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
shintamainjp 0:c853ba46d0b9 26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
shintamainjp 0:c853ba46d0b9 27 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
shintamainjp 0:c853ba46d0b9 28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
shintamainjp 0:c853ba46d0b9 29 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
shintamainjp 0:c853ba46d0b9 30 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
shintamainjp 0:c853ba46d0b9 31 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
shintamainjp 0:c853ba46d0b9 32 * OTHER DEALINGS IN THE SOFTWARE.
shintamainjp 0:c853ba46d0b9 33 * ===============================================================
shintamainjp 0:c853ba46d0b9 34 */
shintamainjp 0:c853ba46d0b9 35
shintamainjp 0:c853ba46d0b9 36 #ifndef WAVFILE_H
shintamainjp 0:c853ba46d0b9 37 #define WAVFILE_H
shintamainjp 0:c853ba46d0b9 38
shintamainjp 0:c853ba46d0b9 39 #include <stdio.h>
shintamainjp 0:c853ba46d0b9 40 #include <stdint.h>
shintamainjp 0:c853ba46d0b9 41
shintamainjp 0:c853ba46d0b9 42 #define WAVFILE_AUDIO_FORMAT_PCM (1)
shintamainjp 0:c853ba46d0b9 43 #define WAVFILE_AUDIO_FORMAT_EXTENSIBLE (65534)
shintamainjp 0:c853ba46d0b9 44 #define WAVFILE_MAXIMUM_CHANNELS (32)
shintamainjp 0:c853ba46d0b9 45
shintamainjp 0:c853ba46d0b9 46 #define WAVFILE_INFO_AUDIO_FORMAT(P) ((P)->audio_format)
shintamainjp 0:c853ba46d0b9 47 #define WAVFILE_INFO_NUM_CHANNELS(P) ((P)->num_channels)
shintamainjp 0:c853ba46d0b9 48 #define WAVFILE_INFO_SAMPLE_RATE(P) ((P)->sample_rate)
shintamainjp 0:c853ba46d0b9 49 #define WAVFILE_INFO_BYTE_RATE(P) ((P)->byte_rate)
shintamainjp 0:c853ba46d0b9 50 #define WAVFILE_INFO_BLOCK_ALIGN(P) ((P)->block_align)
shintamainjp 0:c853ba46d0b9 51 #define WAVFILE_INFO_BITS_PER_SAMPLE(P) ((P)->bits_per_sample)
shintamainjp 0:c853ba46d0b9 52
shintamainjp 0:c853ba46d0b9 53 #define WAVFILE_DATA_IS_END_OF_DATA(P) ((P)->num_channels == 0)
shintamainjp 0:c853ba46d0b9 54 #define WAVFILE_DATA_NUM_CHANNELS(P) ((P)->num_channels)
shintamainjp 0:c853ba46d0b9 55 #define WAVFILE_DATA_CHANNEL_DATA(P,CH) ((P)->channel_data[CH])
shintamainjp 0:c853ba46d0b9 56
shintamainjp 0:c853ba46d0b9 57 #define WAVFILE_DEBUG_ENABLED (0)
shintamainjp 0:c853ba46d0b9 58
shintamainjp 0:c853ba46d0b9 59 typedef struct WAVFILE WAVFILE;
shintamainjp 0:c853ba46d0b9 60
shintamainjp 0:c853ba46d0b9 61 typedef struct {
shintamainjp 0:c853ba46d0b9 62 uint16_t audio_format;
shintamainjp 0:c853ba46d0b9 63 uint16_t num_channels;
shintamainjp 0:c853ba46d0b9 64 uint32_t sample_rate;
shintamainjp 0:c853ba46d0b9 65 uint32_t byte_rate;
shintamainjp 0:c853ba46d0b9 66 uint16_t block_align;
shintamainjp 0:c853ba46d0b9 67 uint16_t bits_per_sample;
shintamainjp 0:c853ba46d0b9 68 } wavfile_info_t;
shintamainjp 0:c853ba46d0b9 69
shintamainjp 0:c853ba46d0b9 70 typedef struct {
shintamainjp 0:c853ba46d0b9 71 uint16_t num_channels;
shintamainjp 0:c853ba46d0b9 72 double channel_data[WAVFILE_MAXIMUM_CHANNELS];
shintamainjp 0:c853ba46d0b9 73 } wavfile_data_t;
shintamainjp 0:c853ba46d0b9 74
shintamainjp 0:c853ba46d0b9 75 /**
shintamainjp 0:c853ba46d0b9 76 */
shintamainjp 0:c853ba46d0b9 77 enum WavFileResult {
shintamainjp 0:c853ba46d0b9 78 WavFileResultOK,
shintamainjp 0:c853ba46d0b9 79 WavFileResultErrorInvalidFileName,
shintamainjp 0:c853ba46d0b9 80 WavFileResultErrorMemoryAllocation,
shintamainjp 0:c853ba46d0b9 81 WavFileResultErrorFileOpen,
shintamainjp 0:c853ba46d0b9 82 WavFileResultErrorFileWrite,
shintamainjp 0:c853ba46d0b9 83 WavFileResultErrorBrokenChunkId,
shintamainjp 0:c853ba46d0b9 84 WavFileResultErrorBrokenChunkSize,
shintamainjp 0:c853ba46d0b9 85 WavFileResultErrorBrokenChunkData,
shintamainjp 0:c853ba46d0b9 86 WavFileResultErrorBrokenFormatId,
shintamainjp 0:c853ba46d0b9 87 WavFileResultErrorInvalidFormatId,
shintamainjp 0:c853ba46d0b9 88 WavFileResultErrorBrokenAudioFormat,
shintamainjp 0:c853ba46d0b9 89 WavFileResultErrorInvalidAudioFormat,
shintamainjp 0:c853ba46d0b9 90 WavFileResultErrorInvalidNumChannels,
shintamainjp 0:c853ba46d0b9 91 WavFileResultErrorBrokenNumChannels,
shintamainjp 0:c853ba46d0b9 92 WavFileResultErrorBrokenSampleRate,
shintamainjp 0:c853ba46d0b9 93 WavFileResultErrorBrokenByteRate,
shintamainjp 0:c853ba46d0b9 94 WavFileResultErrorInvalidByteRate,
shintamainjp 0:c853ba46d0b9 95 WavFileResultErrorBrokenBlockAlign,
shintamainjp 0:c853ba46d0b9 96 WavFileResultErrorBrokenBitsPerSample,
shintamainjp 0:c853ba46d0b9 97 WavFileResultErrorUnsupportedBitsPerSample,
shintamainjp 0:c853ba46d0b9 98 WavFileResultErrorAlreadyInfoChecked,
shintamainjp 0:c853ba46d0b9 99 WavFileResultErrorAlreadyDataChecked,
shintamainjp 0:c853ba46d0b9 100 WavFileResultErrorNoDataChunk,
shintamainjp 0:c853ba46d0b9 101 WavFileResultErrorInvalidMode,
shintamainjp 0:c853ba46d0b9 102 WavFileResultErrorNeedInfoChecked,
shintamainjp 0:c853ba46d0b9 103 WavFileResultErrorNeedDataChecked,
shintamainjp 0:c853ba46d0b9 104 WavFileResultErrorInvalidHandler,
shintamainjp 0:c853ba46d0b9 105 };
shintamainjp 0:c853ba46d0b9 106
shintamainjp 0:c853ba46d0b9 107 /**
shintamainjp 0:c853ba46d0b9 108 */
shintamainjp 0:c853ba46d0b9 109 enum WavFileMode {
shintamainjp 0:c853ba46d0b9 110 WavFileModeRead,
shintamainjp 0:c853ba46d0b9 111 WavFileModeWrite,
shintamainjp 0:c853ba46d0b9 112 };
shintamainjp 0:c853ba46d0b9 113
shintamainjp 0:c853ba46d0b9 114 WAVFILE *wavfile_open(const char *filename, WavFileMode mode, WavFileResult *result);
shintamainjp 0:c853ba46d0b9 115 WavFileResult wavfile_read_info(WAVFILE *p, wavfile_info_t *info);
shintamainjp 0:c853ba46d0b9 116 WavFileResult wavfile_read_data(WAVFILE *p, wavfile_data_t *data);
shintamainjp 0:c853ba46d0b9 117 WavFileResult wavfile_write_info(WAVFILE *p, const wavfile_info_t *info);
shintamainjp 0:c853ba46d0b9 118 WavFileResult wavfile_write_data(WAVFILE *p, const wavfile_data_t *data);
shintamainjp 0:c853ba46d0b9 119 WavFileResult wavfile_close(WAVFILE *p);
shintamainjp 0:c853ba46d0b9 120 void wavfile_result_string(const WavFileResult result, char *buf, size_t siz);
shintamainjp 0:c853ba46d0b9 121
shintamainjp 0:c853ba46d0b9 122 #endif
shintamainjp 0:c853ba46d0b9 123