Skip to content

Commit d8079f5

Browse files
committed
1.0: working
1 parent 78a94d3 commit d8079f5

File tree

7 files changed

+305
-109
lines changed

7 files changed

+305
-109
lines changed

LICENSE

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
This is free and unencumbered software released into the public domain.
2+
3+
Anyone is free to copy, modify, publish, use, compile, sell, or
4+
distribute this software, either in source code form or as a compiled
5+
binary, for any purpose, commercial or non-commercial, and by any
6+
means.
7+
8+
In jurisdictions that recognize copyright laws, the author or authors
9+
of this software dedicate any and all copyright interest in the
10+
software to the public domain. We make this dedication for the benefit
11+
of the public at large and to the detriment of our heirs and
12+
successors. We intend this dedication to be an overt act of
13+
relinquishment in perpetuity of all present and future rights to this
14+
software under copyright law.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.
23+
24+
For more information, please refer to <https://unlicense.org>

Makefile

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
all:
2-
gcc main.c -o pcm2wav
1+
PROJECT = pcm2wav
2+
CC = gcc
3+
CFLAGS = -Wall
4+
RM = rm -f
5+
6+
PROG1 = $(PROJECT)
7+
PROG2 = wav2pcm
8+
PROGS = $(PROG1) $(PROG2)
9+
10+
all: $(PROGS)
11+
12+
$(PROG1): src/$(PROG1).c
13+
$(CC) $(CFLAGS) $^ -o $@
14+
15+
$(PROG2): src/$(PROG2).c
16+
$(CC) $(CFLAGS) $^ -o $@
17+
318
clean:
4-
rm pcm2wav
19+
$(RM) $(PROGS)

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# pcm2wav
2+
3+
Simple tools for added WAV-header to PCM-data.
4+
5+
## build
6+
7+
Type:
8+
9+
```shell
10+
$ make
11+
```
12+
## use
13+
14+
```shell
15+
./wav2pcm test.wav test.wav.pcm
16+
```
17+
18+
```shell
19+
./pcm2wav test.wav.pcm test.wav.pcm.wav 2 44100 16
20+
```

main.c

-105
This file was deleted.

src/pcm2wav.c

+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdint.h>
4+
5+
#if 1
6+
#define swap_u16
7+
#define swap_16
8+
#define swap_u32
9+
#define swap_32
10+
#define swap_u64
11+
#define swap_64
12+
#else
13+
#define swap_u16 swap_uint16
14+
#define swap_16 swap_int16
15+
#define swap_u32 swap_uint32
16+
#define swap_32 swap_int32
17+
#define swap_u64 swap_uint64
18+
#define swap_64 swap_int64
19+
#endif
20+
21+
////////////// Endian swaping functions //////////
22+
23+
//! Byte swap unsigned short
24+
uint16_t swap_uint16( uint16_t val )
25+
{
26+
return (val << 8) | (val >> 8 );
27+
}
28+
29+
//! Byte swap short
30+
int16_t swap_int16( int16_t val )
31+
{
32+
return (val << 8) | ((val >> 8) & 0xFF);
33+
}
34+
35+
//! Byte swap unsigned int
36+
uint32_t swap_uint32( uint32_t val )
37+
{
38+
val = ((val << 8) & 0xFF00FF00 ) | ((val >> 8) & 0xFF00FF );
39+
return (val << 16) | (val >> 16);
40+
}
41+
42+
//! Byte swap int
43+
int32_t swap_int32( int32_t val )
44+
{
45+
val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF );
46+
return (val << 16) | ((val >> 16) & 0xFFFF);
47+
}
48+
49+
//! Byte swap int64
50+
int64_t swap_int64( int64_t val )
51+
{
52+
val = ((val << 8) & 0xFF00FF00FF00FF00ULL ) | ((val >> 8) & 0x00FF00FF00FF00FFULL );
53+
val = ((val << 16) & 0xFFFF0000FFFF0000ULL ) | ((val >> 16) & 0x0000FFFF0000FFFFULL );
54+
return (val << 32) | ((val >> 32) & 0xFFFFFFFFULL);
55+
}
56+
57+
//! Byte swap uint64
58+
uint64_t swap_uint64( uint64_t val )
59+
{
60+
val = ((val << 8) & 0xFF00FF00FF00FF00ULL ) | ((val >> 8) & 0x00FF00FF00FF00FFULL );
61+
val = ((val << 16) & 0xFFFF0000FFFF0000ULL ) | ((val >> 16) & 0x0000FFFF0000FFFFULL );
62+
return (val << 32) | (val >> 32);
63+
}
64+
65+
////////////// Endian swaping wrapper functions //////////
66+
void fwrite64(int64_t value, FILE *f)
67+
{
68+
int64_t item = swap_64(value);
69+
fwrite(&item, sizeof(int64_t),1,f);
70+
}
71+
72+
void fwriteU64(uint64_t value, FILE *f)
73+
{
74+
uint64_t item = swap_u64(value);
75+
fwrite(&item, sizeof(uint64_t),1,f);
76+
}
77+
78+
79+
void fwrite32(int32_t value, FILE *f)
80+
{
81+
int32_t item = swap_32(value);
82+
fwrite(&item, sizeof(int32_t),1,f);
83+
}
84+
85+
void fwriteU32(uint32_t value, FILE *f)
86+
{
87+
uint32_t item = swap_u32(value);
88+
fwrite(&item, sizeof(uint32_t),1,f);
89+
}
90+
91+
void fwrite16(int16_t value, FILE *f)
92+
{
93+
int16_t item = swap_16(value);
94+
fwrite(&item, sizeof(int16_t),1,f);
95+
}
96+
97+
void fwriteU16(uint16_t value, FILE *f)
98+
{
99+
uint16_t item = swap_u16(value);
100+
fwrite(&item, sizeof(uint16_t),1,f);
101+
}
102+
103+
void fwrite8(int8_t *value, FILE *f)
104+
{
105+
fwrite(value, sizeof(int8_t), strlen(value),f);
106+
}
107+
108+
void fwriteU8(uint8_t *value, FILE *f)
109+
{
110+
fwrite(value, sizeof(uint8_t), strlen(value),f);
111+
}
112+
113+
void usage(char *command)
114+
{
115+
printf("usage:\n"
116+
"\t%s pcmfile wavfile channel samplerate bitspersample\n", command);
117+
}
118+
119+
int main(int argc, char *argv[])
120+
{
121+
FILE *pcmfile, *wavfile;
122+
unsigned long int pcmfile_size, chunk_size, total_size;
123+
int read_len;
124+
int chunkSize = 16;
125+
short audioFormat = 1;
126+
short NumChannels, BitsPerSample, BlockAlign;
127+
int SamplingRate, ByteRate;
128+
unsigned char buf[1024];
129+
130+
if (argc != 6)
131+
{
132+
usage(argv[0]);
133+
return 1;
134+
}
135+
136+
pcmfile = fopen(argv[1], "rb");
137+
if (pcmfile == NULL)
138+
{
139+
fprintf(stderr, "!Error: Can't open pcmfile.\n");
140+
return 1;
141+
}
142+
fseek(pcmfile, 0, SEEK_END);
143+
pcmfile_size = ftell(pcmfile);
144+
fseek(pcmfile, 0, SEEK_SET);
145+
total_size = 36 + pcmfile_size;
146+
147+
wavfile = fopen(argv[2], "wb");
148+
if (wavfile == NULL)
149+
{
150+
fprintf(stderr, "!Error: Can't create wavfile.\n");
151+
return 1;
152+
}
153+
NumChannels = atoi(argv[3]);
154+
SamplingRate = atoi(argv[4]);
155+
BitsPerSample = atoi(argv[5]);
156+
ByteRate = NumChannels * BitsPerSample * SamplingRate / 8;
157+
BlockAlign = NumChannels * BitsPerSample / 8;
158+
159+
fwrite8("RIFF", wavfile);
160+
fwrite32(total_size, wavfile);
161+
fwrite8("WAVE", wavfile);
162+
fwrite8("fmt ", wavfile);
163+
fwrite32(chunkSize, wavfile);
164+
fwrite16(audioFormat, wavfile);
165+
fwrite16(NumChannels, wavfile);
166+
fwrite32(SamplingRate, wavfile);
167+
fwrite32(ByteRate, wavfile);
168+
fwrite16(BlockAlign, wavfile);
169+
fwrite16(BitsPerSample, wavfile);
170+
fwrite8("data", wavfile);
171+
fwrite32(pcmfile_size, wavfile);
172+
fflush(wavfile);
173+
174+
while((read_len = fread(buf, 1, sizeof(buf), pcmfile)) != 0)
175+
{
176+
if(read_len != fwrite(buf, 1, read_len, wavfile))
177+
{
178+
fprintf(stderr, "!Error: Can't write wavfile.\n");
179+
return 1;
180+
}
181+
fflush(wavfile);
182+
}
183+
184+
fclose(pcmfile);
185+
fclose(wavfile);
186+
}

0 commit comments

Comments
 (0)