Skip to content

Commit

Permalink
Releax BLACK_THRESHOLD value & optimize wav header initialization
Browse files Browse the repository at this point in the history
Set BLACK_THRESHOLD to 100 down from 200
Avoid unnecessary wav_hdr copy when initializing it
Update outdated reference to the WAV spec in the wav.h
  • Loading branch information
tairrzayev committed Jan 21, 2016
1 parent acfe93c commit 746bb70
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
4 changes: 2 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "surface.h"

#define DECODED_SOUND_FILE "vinyl_decoded.wav"
#define BLACK_THRESHOLD 200
#define BLACK_THRESHOLD 100
#define DEGREE_STRIDE 0.1
#define SCREEN_WIDTH 2000
#define SCREEN_HEIGHT 2000
Expand Down Expand Up @@ -171,7 +171,7 @@ void vinyl_decode(SDL_Window* sdl_window, SDL_Surface* screen_surface, SDL_Surfa
}

// replace placeholder wav_hdr struct in the beginning of file with the actual one
hdr = mk_wav_hdr(samples_written);
init_wav_hdr(&hdr, samples_written);
rewind(decoded_sound);
fwrite(&hdr, sizeof(hdr), 1, decoded_sound);
fclose(decoded_sound);
Expand Down
42 changes: 20 additions & 22 deletions wav.c
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
#include <string.h>
#include "wav.h"

#define CHUNK_ID "RIFF"
#define FORMAT "WAVE"
#define SUBCHUNK1_ID "fmt "
#define SUBCHUNK2_ID "data"

//TODO: determine payload size dynamically
struct wav_hdr mk_wav_hdr(uint32_t payload_size)
void init_wav_hdr(struct wav_hdr *hdr, uint32_t payload_size)
{
struct wav_hdr hdr;
const char* chunk_id = "RIFF";
const char* format = "WAVE";
const char* subchunk1_id = "fmt ";
const char* subchunk2_id = "data";

strncpy(hdr.chunk_id, chunk_id, strlen(chunk_id));
hdr.chunk_size = 0x6424;
strncpy(hdr.format, format, strlen(format));
strncpy(hdr.subchunk1_id, subchunk1_id, strlen(subchunk1_id));
hdr.subchunk1_size = 0x10;
hdr.audio_format = 0x01;
hdr.channels = 0x01;
hdr.sample_rate = 0x1F40;
hdr.byte_rate = 0x1F40;
hdr.block_align = 0x01;
hdr.bits_per_sample = 0x08;
strncpy(hdr.subchunk2_id, subchunk2_id, strlen(subchunk2_id));
hdr.subchunk2_size = 0x45A00;

return hdr;
memset(hdr, 0, sizeof(*hdr));
strncpy(hdr->chunk_id, CHUNK_ID, strlen(CHUNK_ID));
hdr->chunk_size = 0x6424;
strncpy(hdr->format, FORMAT, strlen(FORMAT));
strncpy(hdr->subchunk1_id, SUBCHUNK1_ID, strlen(SUBCHUNK1_ID));
hdr->subchunk1_size = 0x10;
hdr->audio_format = 0x01;
hdr->channels = 0x01;
hdr->sample_rate = 0x1F40;
hdr->byte_rate = 0x1F40;
hdr->block_align = 0x01;
hdr->bits_per_sample = 0x08;
strncpy(hdr->subchunk2_id, SUBCHUNK2_ID, strlen(SUBCHUNK2_ID));
hdr->subchunk2_size = 0x45A00;
}
4 changes: 2 additions & 2 deletions wav.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <stdint.h>
// WAV header
// https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
// http://soundfile.sapp.org/doc/WaveFormat/
struct wav_hdr {
char chunk_id[4];
uint32_t chunk_size;
Expand All @@ -17,4 +17,4 @@ struct wav_hdr {
uint32_t subchunk2_size;
} __attribute__((packed));

struct wav_hdr mk_wav_hdr(uint32_t payload_size);
void init_wav_hdr(struct wav_hdr *hdr, uint32_t payload_size);

0 comments on commit 746bb70

Please sign in to comment.