-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound.cpp
98 lines (80 loc) · 2.83 KB
/
sound.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: Copyright (c) 2021-2024 Chris Dragan
#include "minivulkan.h"
#include "mstdc.h"
#include "vmath.h"
#include "vecfloat.h"
#ifdef __APPLE__
# define NEED_WAV_HEADER 1
#else
# define NEED_WAV_HEADER 0
#endif
struct WAVHeader {
char riff[4]; // "RIFF"
uint32_t file_size; // data_size + sizeof(WAVHeader) - 8
char wave_fmt[8]; // "WAVEfmt\0"
uint32_t fmt_len; // 16
uint16_t type; // 1 is PCM
uint16_t num_channels; // 2
uint32_t rate; // 44100
uint32_t bytes_per_sec; // 176400 = rate * bits_per_sample * num_channels / 8
uint16_t bytes_per_sch; // 4 = num_channels * bits_per_sample / 8
uint16_t bits_per_sample; // 16
char data_hdr[4]; // "data"
uint32_t data_size; // size of all data
};
static_assert(sizeof(WAVHeader) == 44);
struct Sample16Stereo {
int16_t left;
int16_t right;
};
struct WAVFile16Stereo {
#if NEED_WAV_HEADER
WAVHeader header;
#endif
Sample16Stereo data[1];
};
static constexpr uint32_t sampling_rate = 44100;
static constexpr uint16_t num_channels = 2;
static constexpr uint16_t bits_per_sample = 16;
#if NEED_WAV_HEADER
static const WAVHeader wav_header = {
{ 'R', 'I', 'F', 'F' },
0,
{ 'W', 'A', 'V', 'E', 'f', 'm', 't', ' ' },
16,
1, // PCM
num_channels,
sampling_rate,
(sampling_rate * num_channels * bits_per_sample) / 8,
(num_channels * bits_per_sample) / 8,
bits_per_sample,
{ 'd', 'a', 't', 'a' },
0
};
#endif
bool init_sound()
{
constexpr uint64_t duration_ms = 50;
constexpr uint32_t frequency_hz = 440; // A
constexpr uint32_t total_samples = static_cast<uint32_t>((duration_ms * sampling_rate) / 1000u);
constexpr uint32_t data_size = total_samples * num_channels * (bits_per_sample / 8u);
constexpr uint32_t wav_hdr_size = NEED_WAV_HEADER ? static_cast<uint32_t>(sizeof(WAVHeader)) : 0u;
constexpr uint32_t alloc_size = wav_hdr_size + data_size;
static uint8_t audio_buf[alloc_size];
WAVFile16Stereo& wav_file = *reinterpret_cast<WAVFile16Stereo*>(audio_buf);
#if NEED_WAV_HEADER
mstd::mem_copy(audio_buf, &wav_header, sizeof(wav_header));
wav_file.header.file_size = alloc_size - 8;
wav_file.header.data_size = data_size;
#endif
constexpr float coeff = vmath::two_pi * frequency_hz / sampling_rate;
for (uint32_t i = 0; i < total_samples; i++) {
const vmath::sin_cos_result sc = vmath::sincos(static_cast<float>(i) * coeff);
const int16_t value = static_cast<int16_t>(sc.cos * 0.001f * 32767);
Sample16Stereo& out = wav_file.data[i];
out.left = value;
out.right = value;
}
return load_sound_track(audio_buf, mstd::array_size(audio_buf));
}