wew
my
This commit is contained in:
parent
041b57d76c
commit
ab44ae3b2c
5 changed files with 1843 additions and 18 deletions
1782
waif/frog.jpg
Normal file
1782
waif/frog.jpg
Normal file
File diff suppressed because it is too large
Load diff
BIN
waif/frog.wav
Normal file
BIN
waif/frog.wav
Normal file
Binary file not shown.
BIN
waif/frog_in.jpg
Normal file
BIN
waif/frog_in.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 85 KiB |
BIN
waif/frog_out.jpg
Normal file
BIN
waif/frog_out.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 85 KiB |
79
waif/main.c
79
waif/main.c
|
@ -9,7 +9,7 @@
|
||||||
#define FALSE 0
|
#define FALSE 0
|
||||||
|
|
||||||
#define SAMPLE_RATE 48000
|
#define SAMPLE_RATE 48000
|
||||||
#define BYTE_RATE 800
|
#define BYTE_RATE 1000
|
||||||
|
|
||||||
/* CMD LINE ARG STRUCT */
|
/* CMD LINE ARG STRUCT */
|
||||||
struct {
|
struct {
|
||||||
|
@ -21,8 +21,12 @@ struct {
|
||||||
FILE *inFile, *outFile;
|
FILE *inFile, *outFile;
|
||||||
void writeWavHeader(uint32_t inputFileSize);
|
void writeWavHeader(uint32_t inputFileSize);
|
||||||
void writeBytes(uint64_t bytes, int byteCount) {
|
void writeBytes(uint64_t bytes, int byteCount) {
|
||||||
for (int i = 0; i < byteCount; ++i)
|
for (int i = 0; i < byteCount; ++i) {
|
||||||
fputc(((char*)bytes)[7-byteCount+i+1], outFile);
|
int offset = 8 * (byteCount - i - 1);
|
||||||
|
uint64_t byte = bytes & (0xFFll << offset);
|
||||||
|
uint8_t bytesh = byte >> offset;
|
||||||
|
fputc(bytesh, outFile);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* MATH */
|
/* MATH */
|
||||||
|
@ -38,15 +42,12 @@ int samplesPerByte() {
|
||||||
return SAMPLE_RATE / BYTE_RATE;
|
return SAMPLE_RATE / BYTE_RATE;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t audioFileSize(uint32_t inputFileSize) {
|
|
||||||
return samplesPerByte() * inputFileSize * 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* MISC */
|
/* MISC */
|
||||||
void printHelp();
|
void printHelp();
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
if (argc < 2 || argc > 4) {
|
if (argc < 2 || argc > 4) {
|
||||||
|
printf("a\n");
|
||||||
printHelp();
|
printHelp();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -54,13 +55,14 @@ int main(int argc, char *argv[]) {
|
||||||
args.source = NULL;
|
args.source = NULL;
|
||||||
args.destination = "wout.raw";
|
args.destination = "wout.raw";
|
||||||
args.encoding = TRUE;
|
args.encoding = TRUE;
|
||||||
for (int i = 0; i < argc; ++i) {
|
for (int i = 1; i < argc; ++i) {
|
||||||
switch (i) {
|
switch (i) {
|
||||||
case 0:
|
case 1:
|
||||||
if (strlen(argv[i]) == 2 && argv[i][0] == '/') {
|
if (strlen(argv[i]) == 2 && argv[i][0] == '/') {
|
||||||
if (argv[i][1] == 'D' || argv[i][1] == 'd')
|
if (argv[i][1] == 'D' || argv[i][1] == 'd')
|
||||||
args.encoding = FALSE;
|
args.encoding = FALSE;
|
||||||
else if (argv[i][1] != 'E' && argv[i][1] != 'e') {
|
else if (argv[i][1] != 'E' && argv[i][1] != 'e') {
|
||||||
|
printf("b\n");
|
||||||
printHelp();
|
printHelp();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -69,23 +71,19 @@ int main(int argc, char *argv[]) {
|
||||||
default:
|
default:
|
||||||
if (args.source == NULL)
|
if (args.source == NULL)
|
||||||
args.source = argv[i];
|
args.source = argv[i];
|
||||||
else if (args.destination == NULL)
|
else
|
||||||
args.destination = argv[i];
|
args.destination = argv[i];
|
||||||
else {
|
|
||||||
printHelp();
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((inFile = fopen(args.source, "rb")) == NULL) {
|
if ((inFile = fopen(args.source, "rb")) == NULL) {
|
||||||
printf("Source file does not exist or cannot be accessed.");
|
printf("Source file does not exist or cannot be accessed.\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((outFile = fopen(args.destination, "wb+")) == NULL) {
|
if ((outFile = fopen(args.destination, "wb+")) == NULL) {
|
||||||
printf("Destination file cannot be written to.");
|
printf("Destination file cannot be written to.\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,10 +93,56 @@ int main(int argc, char *argv[]) {
|
||||||
rewind(inFile);
|
rewind(inFile);
|
||||||
|
|
||||||
writeWavHeader(fileSize);
|
writeWavHeader(fileSize);
|
||||||
|
double t = 0;
|
||||||
double dt = samplePeriod();
|
double dt = samplePeriod();
|
||||||
int spb = samplesPerByte();
|
int spb = samplesPerByte();
|
||||||
for(;;) {
|
for(;;) {
|
||||||
|
uint8_t byte = fgetc(inFile);
|
||||||
|
if (feof(inFile))
|
||||||
|
break;
|
||||||
|
|
||||||
|
double amplitude = byte == 0 ? 0 : 128 * byte - 64;
|
||||||
|
for (int i = 0; i < spb; ++i) {
|
||||||
|
int pt = i < spb/2 ? amplitude : -amplitude;//(int)round(amplitude * sin(2.f*3.14159*BYTE_RATE * t));
|
||||||
|
writeBytes(pt, 2);
|
||||||
|
t += dt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
int spb = samplesPerByte();
|
||||||
|
fseek(inFile, 36, SEEK_SET);
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
int16_t max = 0;
|
||||||
|
for (int i = 0; i < spb; ++i) {
|
||||||
|
uint8_t msb = fgetc(inFile);
|
||||||
|
uint8_t lsb = fgetc(inFile);
|
||||||
|
if (feof(inFile))
|
||||||
|
break;
|
||||||
|
|
||||||
|
int16_t inval = (msb << 8) | lsb;
|
||||||
|
max = inval > max ? inval : max;
|
||||||
|
}
|
||||||
|
|
||||||
|
int value;
|
||||||
|
for (value = 0; value < 256; ++value) {
|
||||||
|
if (value == 0) {
|
||||||
|
if (max >= 0 && max <= 32)
|
||||||
|
break;
|
||||||
|
} else if (value == 1) {
|
||||||
|
if (max > 32 && max <= 128)
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
if (max > (value - 1) * 128 && max <= value * 128)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fputc(value, outFile);
|
||||||
|
|
||||||
|
if (feof(inFile))
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,7 +158,7 @@ void printHelp() {
|
||||||
printf(" destination\tSpecifies the directory and/or filename\n \t\tfor the converted file.\n");
|
printf(" destination\tSpecifies the directory and/or filename\n \t\tfor the converted file.\n");
|
||||||
printf(" /E\t\tEncodes the file.\n");
|
printf(" /E\t\tEncodes the file.\n");
|
||||||
printf(" /D\t\tDecodes the file.\n\n");
|
printf(" /D\t\tDecodes the file.\n\n");
|
||||||
printf("The /E and /D switches may not be used together. If neither\nis specified, /E is implied. If destination is not supplied,\noutput will be written to 'wout'.");
|
printf("The /E and /D switches may not be used together. If neither\nis specified, /E is implied. If destination is not supplied,\noutput will be written to 'wout.raw'.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeWavHeader(uint32_t inputFileSize) {
|
void writeWavHeader(uint32_t inputFileSize) {
|
||||||
|
@ -131,6 +175,5 @@ void writeWavHeader(uint32_t inputFileSize) {
|
||||||
writeBytes(2, 2);
|
writeBytes(2, 2);
|
||||||
writeBytes(16, 2);
|
writeBytes(16, 2);
|
||||||
fputs("data", outFile);
|
fputs("data", outFile);
|
||||||
fputs("fmt ", outFile);
|
|
||||||
writeBytes(dataSize, 4);
|
writeBytes(dataSize, 4);
|
||||||
}
|
}
|
Loading…
Reference in a new issue