Skip to content

Instantly share code, notes, and snippets.

@tetsu-koba
Created October 4, 2015 06:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tetsu-koba/b0c36b35e2a4ef47b375 to your computer and use it in GitHub Desktop.
Save tetsu-koba/b0c36b35e2a4ef47b375 to your computer and use it in GitHub Desktop.
Various operation for PCM data
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int mix(char *inbuf1, char *inbuf2, char *outbuf, int len, float vol1, float vol2)
{
int16_t *inp1, *inp2;
int16_t *outp;
int cnt = len / sizeof(int16_t);
inp1 = (int16_t*)inbuf1;
inp2 = (int16_t*)inbuf2;
outp = (int16_t*)outbuf;
while (cnt-- > 0) {
*outp++ = *inp1++ * vol1 + *inp2++ * vol2;
}
return (char*)outp - outbuf;
}
#define BUFSIZE 4096
int main(int argc, char** argv)
{
FILE *fin1, *fin2;
FILE *fout;
char inbuf1[BUFSIZE], inbuf2[BUFSIZE], outbuf[BUFSIZE];
int read_len1, read_len2, write_len;
char *infile1 = argv[1];
char *infile2 = argv[3];
char *outfile = argv[5];
float vol1, vol2;
if (argc != 6) {
fprintf(stderr, "usage: %s infile1 vol1 infile2 vol2 outfile\nSpecify vol1, vol2 in float\n", argv[0]);
exit(1);
}
vol1 = strtof(argv[2], NULL);
vol2 = strtof(argv[4], NULL);
if ((fin1 = fopen(infile1, "r")) == NULL) {
perror(infile1);
exit(1);
}
if ((fin2 = fopen(infile2, "r")) == NULL) {
perror(infile2);
exit(1);
}
if ((fout = fopen(outfile, "w")) == NULL) {
perror(outfile);
exit(1);
}
while (!feof(fin1) && !feof(fin2)) {
int read_len;
read_len1 = fread(inbuf1, 1, sizeof inbuf1, fin1);
read_len2 = fread(inbuf2, 1, sizeof inbuf2, fin2);
read_len = (read_len1 > read_len2)? read_len2 : read_len1;
write_len = mix(inbuf1, inbuf2, outbuf, read_len, vol1, vol2);
printf("read_len1=%d read_len2=%d read_len=%d write_len=%d\n", read_len1, read_len2, read_len, write_len);
fwrite(outbuf, 1, write_len, fout);
}
fclose(fin1);
fclose(fin2);
fclose(fout);
}
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int mono2left(char *inbuf, char *outbuf, int len)
{
int16_t *inp;
int16_t *outp;
int cnt = len / sizeof(int16_t);
inp = (int16_t*)inbuf;
outp = (int16_t*)outbuf;
while (cnt-- > 0) {
*outp++ = *inp;
*outp++ = 0;
inp++;
}
return (char*)outp - outbuf;
}
#define BUFSIZE 4096
int main(int argc, char** argv)
{
FILE *fin;
FILE *fout;
char inbuf[BUFSIZE], outbuf[BUFSIZE * 2];
int read_len, write_len;
char *infile = argv[1];
char *outfile = argv[2];
if (argc != 3) {
fprintf(stderr, "usage: %s infile outfile\n", argv[0]);
exit(1);
}
if ((fin = fopen(infile, "r")) == NULL) {
perror(infile);
exit(1);
}
if ((fout = fopen(outfile, "w")) == NULL) {
perror(outfile);
exit(1);
}
while (!feof(fin)) {
read_len = fread(inbuf, 1, sizeof inbuf, fin);
write_len = mono2left(inbuf, outbuf, read_len);
fwrite(outbuf, 1, write_len, fout);
}
fclose(fin);
fclose(fout);
}
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int mono2right(char *inbuf, char *outbuf, int len)
{
int16_t *inp;
int16_t *outp;
int cnt = len / sizeof(int16_t);
inp = (int16_t*)inbuf;
outp = (int16_t*)outbuf;
while (cnt-- > 0) {
*outp++ = 0;
*outp++ = *inp;
inp++;
}
return (char*)outp - outbuf;
}
#define BUFSIZE 4096
int main(int argc, char** argv)
{
FILE *fin;
FILE *fout;
char inbuf[BUFSIZE], outbuf[BUFSIZE * 2];
int read_len, write_len;
char *infile = argv[1];
char *outfile = argv[2];
if (argc != 3) {
fprintf(stderr, "usage: %s infile outfile\n", argv[0]);
exit(1);
}
if ((fin = fopen(infile, "r")) == NULL) {
perror(infile);
exit(1);
}
if ((fout = fopen(outfile, "w")) == NULL) {
perror(outfile);
exit(1);
}
while (!feof(fin)) {
read_len = fread(inbuf, 1, sizeof inbuf, fin);
write_len = mono2right(inbuf, outbuf, read_len);
fwrite(outbuf, 1, write_len, fout);
}
fclose(fin);
fclose(fout);
}
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int mono2stereo(char *inbuf, char *outbuf, int len)
{
int16_t *inp;
int16_t *outp;
int cnt = len / sizeof(int16_t);
inp = (int16_t*)inbuf;
outp = (int16_t*)outbuf;
while (cnt-- > 0) {
*outp++ = *inp;
*outp++ = *inp;
inp++;
}
return (char*)outp - outbuf;
}
#define BUFSIZE 4096
int main(int argc, char** argv)
{
FILE *fin;
FILE *fout;
char inbuf[BUFSIZE], outbuf[BUFSIZE * 2];
int read_len, write_len;
char *infile = argv[1];
char *outfile = argv[2];
if (argc != 3) {
fprintf(stderr, "usage: %s infile outfile\n", argv[0]);
exit(1);
}
if ((fin = fopen(infile, "r")) == NULL) {
perror(infile);
exit(1);
}
if ((fout = fopen(outfile, "w")) == NULL) {
perror(outfile);
exit(1);
}
while (!feof(fin)) {
read_len = fread(inbuf, 1, sizeof inbuf, fin);
write_len = mono2stereo(inbuf, outbuf, read_len);
fwrite(outbuf, 1, write_len, fout);
}
fclose(fin);
fclose(fout);
}
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int changeVol(char *inbuf, char *outbuf, int len, float vol_l, float vol_r)
{
int16_t *inp;
int16_t *outp;
int cnt = len / sizeof(int16_t) / 2;
inp = (int16_t*)inbuf;
outp = (int16_t*)outbuf;
while (cnt-- > 0) {
*outp++ = *inp++ * vol_l;
*outp++ = *inp++ * vol_r;
}
return (char*)outp - outbuf;
}
#define BUFSIZE 4096
int main(int argc, char** argv)
{
FILE *fin;
FILE *fout;
char inbuf[BUFSIZE], outbuf[BUFSIZE];
int read_len, write_len;
char *infile = argv[1];
char *outfile = argv[4];
float vol_l, vol_r;
if (argc != 5) {
fprintf(stderr, "usage: %s infile vol_l vol_r outfile\nSpecify vol_l, vol_r in float\n", argv[0]);
exit(1);
}
vol_l = strtof(argv[2], NULL);
vol_r = strtof(argv[3], NULL);
if ((fin = fopen(infile, "r")) == NULL) {
perror(infile);
exit(1);
}
if ((fout = fopen(outfile, "w")) == NULL) {
perror(outfile);
exit(1);
}
while (!feof(fin)) {
read_len = fread(inbuf, 1, sizeof inbuf, fin);
write_len = changeVol(inbuf, outbuf, read_len, vol_l, vol_r);
fwrite(outbuf, 1, write_len, fout);
}
fclose(fin);
fclose(fout);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment