Skip to content

Commit 746bb70

Browse files
committed
Releax BLACK_THRESHOLD value & optimize wav header initialization
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
1 parent acfe93c commit 746bb70

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed

main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "surface.h"
1111

1212
#define DECODED_SOUND_FILE "vinyl_decoded.wav"
13-
#define BLACK_THRESHOLD 200
13+
#define BLACK_THRESHOLD 100
1414
#define DEGREE_STRIDE 0.1
1515
#define SCREEN_WIDTH 2000
1616
#define SCREEN_HEIGHT 2000
@@ -171,7 +171,7 @@ void vinyl_decode(SDL_Window* sdl_window, SDL_Surface* screen_surface, SDL_Surfa
171171
}
172172

173173
// replace placeholder wav_hdr struct in the beginning of file with the actual one
174-
hdr = mk_wav_hdr(samples_written);
174+
init_wav_hdr(&hdr, samples_written);
175175
rewind(decoded_sound);
176176
fwrite(&hdr, sizeof(hdr), 1, decoded_sound);
177177
fclose(decoded_sound);

wav.c

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
#include <string.h>
22
#include "wav.h"
33

4+
#define CHUNK_ID "RIFF"
5+
#define FORMAT "WAVE"
6+
#define SUBCHUNK1_ID "fmt "
7+
#define SUBCHUNK2_ID "data"
8+
49
//TODO: determine payload size dynamically
5-
struct wav_hdr mk_wav_hdr(uint32_t payload_size)
10+
void init_wav_hdr(struct wav_hdr *hdr, uint32_t payload_size)
611
{
7-
struct wav_hdr hdr;
8-
const char* chunk_id = "RIFF";
9-
const char* format = "WAVE";
10-
const char* subchunk1_id = "fmt ";
11-
const char* subchunk2_id = "data";
12-
13-
strncpy(hdr.chunk_id, chunk_id, strlen(chunk_id));
14-
hdr.chunk_size = 0x6424;
15-
strncpy(hdr.format, format, strlen(format));
16-
strncpy(hdr.subchunk1_id, subchunk1_id, strlen(subchunk1_id));
17-
hdr.subchunk1_size = 0x10;
18-
hdr.audio_format = 0x01;
19-
hdr.channels = 0x01;
20-
hdr.sample_rate = 0x1F40;
21-
hdr.byte_rate = 0x1F40;
22-
hdr.block_align = 0x01;
23-
hdr.bits_per_sample = 0x08;
24-
strncpy(hdr.subchunk2_id, subchunk2_id, strlen(subchunk2_id));
25-
hdr.subchunk2_size = 0x45A00;
26-
27-
return hdr;
12+
memset(hdr, 0, sizeof(*hdr));
13+
strncpy(hdr->chunk_id, CHUNK_ID, strlen(CHUNK_ID));
14+
hdr->chunk_size = 0x6424;
15+
strncpy(hdr->format, FORMAT, strlen(FORMAT));
16+
strncpy(hdr->subchunk1_id, SUBCHUNK1_ID, strlen(SUBCHUNK1_ID));
17+
hdr->subchunk1_size = 0x10;
18+
hdr->audio_format = 0x01;
19+
hdr->channels = 0x01;
20+
hdr->sample_rate = 0x1F40;
21+
hdr->byte_rate = 0x1F40;
22+
hdr->block_align = 0x01;
23+
hdr->bits_per_sample = 0x08;
24+
strncpy(hdr->subchunk2_id, SUBCHUNK2_ID, strlen(SUBCHUNK2_ID));
25+
hdr->subchunk2_size = 0x45A00;
2826
}

wav.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <stdint.h>
22
// WAV header
3-
// https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
3+
// http://soundfile.sapp.org/doc/WaveFormat/
44
struct wav_hdr {
55
char chunk_id[4];
66
uint32_t chunk_size;
@@ -17,4 +17,4 @@ struct wav_hdr {
1717
uint32_t subchunk2_size;
1818
} __attribute__((packed));
1919

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

0 commit comments

Comments
 (0)