Skip to content

Commit

Permalink
1.0: working
Browse files Browse the repository at this point in the history
  • Loading branch information
zvezdochiot committed Feb 12, 2023
1 parent 78a94d3 commit d8079f5
Show file tree
Hide file tree
Showing 7 changed files with 305 additions and 109 deletions.
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <https://unlicense.org>
21 changes: 18 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
all:
gcc main.c -o pcm2wav
PROJECT = pcm2wav
CC = gcc
CFLAGS = -Wall
RM = rm -f

PROG1 = $(PROJECT)
PROG2 = wav2pcm
PROGS = $(PROG1) $(PROG2)

all: $(PROGS)

$(PROG1): src/$(PROG1).c
$(CC) $(CFLAGS) $^ -o $@

$(PROG2): src/$(PROG2).c
$(CC) $(CFLAGS) $^ -o $@

clean:
rm pcm2wav
$(RM) $(PROGS)
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# pcm2wav

Simple tools for added WAV-header to PCM-data.

## build

Type:

```shell
$ make
```
## use

```shell
./wav2pcm test.wav test.wav.pcm
```

```shell
./pcm2wav test.wav.pcm test.wav.pcm.wav 2 44100 16
```
105 changes: 0 additions & 105 deletions main.c

This file was deleted.

186 changes: 186 additions & 0 deletions src/pcm2wav.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
#include <stdio.h>
#include <string.h>
#include <stdint.h>

#if 1
#define swap_u16
#define swap_16
#define swap_u32
#define swap_32
#define swap_u64
#define swap_64
#else
#define swap_u16 swap_uint16
#define swap_16 swap_int16
#define swap_u32 swap_uint32
#define swap_32 swap_int32
#define swap_u64 swap_uint64
#define swap_64 swap_int64
#endif

////////////// Endian swaping functions //////////

//! Byte swap unsigned short
uint16_t swap_uint16( uint16_t val )
{
return (val << 8) | (val >> 8 );
}

//! Byte swap short
int16_t swap_int16( int16_t val )
{
return (val << 8) | ((val >> 8) & 0xFF);
}

//! Byte swap unsigned int
uint32_t swap_uint32( uint32_t val )
{
val = ((val << 8) & 0xFF00FF00 ) | ((val >> 8) & 0xFF00FF );
return (val << 16) | (val >> 16);
}

//! Byte swap int
int32_t swap_int32( int32_t val )
{
val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF );
return (val << 16) | ((val >> 16) & 0xFFFF);
}

//! Byte swap int64
int64_t swap_int64( int64_t val )
{
val = ((val << 8) & 0xFF00FF00FF00FF00ULL ) | ((val >> 8) & 0x00FF00FF00FF00FFULL );
val = ((val << 16) & 0xFFFF0000FFFF0000ULL ) | ((val >> 16) & 0x0000FFFF0000FFFFULL );
return (val << 32) | ((val >> 32) & 0xFFFFFFFFULL);
}

//! Byte swap uint64
uint64_t swap_uint64( uint64_t val )
{
val = ((val << 8) & 0xFF00FF00FF00FF00ULL ) | ((val >> 8) & 0x00FF00FF00FF00FFULL );
val = ((val << 16) & 0xFFFF0000FFFF0000ULL ) | ((val >> 16) & 0x0000FFFF0000FFFFULL );
return (val << 32) | (val >> 32);
}

////////////// Endian swaping wrapper functions //////////
void fwrite64(int64_t value, FILE *f)
{
int64_t item = swap_64(value);
fwrite(&item, sizeof(int64_t),1,f);
}

void fwriteU64(uint64_t value, FILE *f)
{
uint64_t item = swap_u64(value);
fwrite(&item, sizeof(uint64_t),1,f);
}


void fwrite32(int32_t value, FILE *f)
{
int32_t item = swap_32(value);
fwrite(&item, sizeof(int32_t),1,f);
}

void fwriteU32(uint32_t value, FILE *f)
{
uint32_t item = swap_u32(value);
fwrite(&item, sizeof(uint32_t),1,f);
}

void fwrite16(int16_t value, FILE *f)
{
int16_t item = swap_16(value);
fwrite(&item, sizeof(int16_t),1,f);
}

void fwriteU16(uint16_t value, FILE *f)
{
uint16_t item = swap_u16(value);
fwrite(&item, sizeof(uint16_t),1,f);
}

void fwrite8(int8_t *value, FILE *f)
{
fwrite(value, sizeof(int8_t), strlen(value),f);
}

void fwriteU8(uint8_t *value, FILE *f)
{
fwrite(value, sizeof(uint8_t), strlen(value),f);
}

void usage(char *command)
{
printf("usage:\n"
"\t%s pcmfile wavfile channel samplerate bitspersample\n", command);
}

int main(int argc, char *argv[])
{
FILE *pcmfile, *wavfile;
unsigned long int pcmfile_size, chunk_size, total_size;
int read_len;
int chunkSize = 16;
short audioFormat = 1;
short NumChannels, BitsPerSample, BlockAlign;
int SamplingRate, ByteRate;
unsigned char buf[1024];

if (argc != 6)
{
usage(argv[0]);
return 1;
}

pcmfile = fopen(argv[1], "rb");
if (pcmfile == NULL)
{
fprintf(stderr, "!Error: Can't open pcmfile.\n");
return 1;
}
fseek(pcmfile, 0, SEEK_END);
pcmfile_size = ftell(pcmfile);
fseek(pcmfile, 0, SEEK_SET);
total_size = 36 + pcmfile_size;

wavfile = fopen(argv[2], "wb");
if (wavfile == NULL)
{
fprintf(stderr, "!Error: Can't create wavfile.\n");
return 1;
}
NumChannels = atoi(argv[3]);
SamplingRate = atoi(argv[4]);
BitsPerSample = atoi(argv[5]);
ByteRate = NumChannels * BitsPerSample * SamplingRate / 8;
BlockAlign = NumChannels * BitsPerSample / 8;

fwrite8("RIFF", wavfile);
fwrite32(total_size, wavfile);
fwrite8("WAVE", wavfile);
fwrite8("fmt ", wavfile);
fwrite32(chunkSize, wavfile);
fwrite16(audioFormat, wavfile);
fwrite16(NumChannels, wavfile);
fwrite32(SamplingRate, wavfile);
fwrite32(ByteRate, wavfile);
fwrite16(BlockAlign, wavfile);
fwrite16(BitsPerSample, wavfile);
fwrite8("data", wavfile);
fwrite32(pcmfile_size, wavfile);
fflush(wavfile);

while((read_len = fread(buf, 1, sizeof(buf), pcmfile)) != 0)
{
if(read_len != fwrite(buf, 1, read_len, wavfile))
{
fprintf(stderr, "!Error: Can't write wavfile.\n");
return 1;
}
fflush(wavfile);
}

fclose(pcmfile);
fclose(wavfile);
}
Loading

0 comments on commit d8079f5

Please sign in to comment.