1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
#include <stdio.h> #include <stdlib.h> #include <string.h>
#include "pcm2alaw.h"
typedef struct WAV_HEADER { char ChunkID[4]; unsigned int ChunkSize; char Format[4];
char SubChunk1ID[4]; unsigned int SubChunk1Size; unsigned short int AudioFormat; unsigned short int NumChannels; unsigned int SampleRate; unsigned int ByteRate; unsigned short int BlockAlign; unsigned short int BitsPerSample;
char SubChunk2ID[4]; unsigned int SubChunk2Size; } WAV_HEADER;
typedef struct ALAW_HEADER { char ChunkID[4]; unsigned int ChunkSize; char Format[4];
char SubChunk1ID[4]; unsigned int SubChunk1Size; unsigned short int AudioFormat; unsigned short int NumChannels; unsigned int SampleRate; unsigned int ByteRate; unsigned short int BlockAlign; unsigned short int BitsPerSample;
char SubChunk3ID[4]; unsigned int SubChunk3Size; unsigned int SampleLength;
char SubChunk2ID[4]; unsigned int SubChunk2Size; } ALAW_HEADER;
int main(int argc, char *argv[]) { FILE *fpin = NULL; FILE *fout = NULL; WAV_HEADER wav; ALAW_HEADER alaw; char tmp; short int pcm_val;
fpin = fopen("./M1F1-int16-AFsp.wav", "rb"); fread(&wav, sizeof(struct WAV_HEADER), 1, fpin); fout = fopen("./8bitalaw.wav", "w+b");
printf("ChunkID=%c%c%c%c\n", wav.ChunkID[0], wav.ChunkID[1], wav.ChunkID[2], wav.ChunkID[3]); printf("ChunkSize=%d\n", wav.ChunkSize); printf("Format=%c%c%c%c\n", wav.Format[0], wav.Format[1], wav.Format[2], wav.Format[3]); printf("SubChunk1ID=%c%c%c%c\n", wav.SubChunk1ID[0], wav.SubChunk1ID[1], wav.SubChunk1ID[2], wav.SubChunk1ID[3]); printf("SubChunk1Size=%d\n", wav.SubChunk1Size); printf("AudioFormat=%d\n", wav.AudioFormat); printf("NumChannels=%d\n", wav.NumChannels); printf("SampleRate=%d\n", wav.SampleRate); printf("ByteRate=%d\n", wav.ByteRate); printf("BlockAlign=%d\n", wav.BlockAlign); printf("BitsPerSample=%d\n", wav.BitsPerSample); printf("SubChunk2ID=%c%c%c%c\n", wav.SubChunk2ID[0], wav.SubChunk2ID[1], wav.SubChunk2ID[2], wav.SubChunk2ID[3]); printf("SubChunk2Size=%d\n", wav.SubChunk2Size);
strcpy(alaw.ChunkID, wav.ChunkID); alaw.ChunkSize = (wav.ChunkSize - 36) / 2 + 48; strcpy(alaw.Format, wav.Format); strcpy(alaw.SubChunk1ID, wav.SubChunk1ID); alaw.SubChunk1Size = wav.SubChunk1Size; alaw.AudioFormat = wav.AudioFormat; alaw.NumChannels = wav.NumChannels; alaw.SampleRate = wav.SampleRate; alaw.ByteRate = wav.ByteRate; alaw.BlockAlign = wav.BlockAlign; alaw.BitsPerSample = wav.BitsPerSample;
strcpy(alaw.SubChunk3ID, "fact"); alaw.SubChunk3Size = 4; alaw.SampleLength = (wav.ChunkSize - 36) / 2;
strcpy(alaw.SubChunk2ID, wav.SubChunk2ID); alaw.SubChunk2Size = (wav.ChunkSize - 36) / 2;
fwrite(&alaw, sizeof(ALAW_HEADER), 1, fout); while (1 == fread(&pcm_val, sizeof(short int), 1, fpin)) { tmp = linear2alaw(pcm_val); fputc(tmp, fout); }
fclose(fpin); fclose(fout); return 0; }
|