Created
December 3, 2012 12:40
-
-
Save bolero-MURAKAMI/4194796 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// サンプル数 : デフォルト = 44100 | |
#ifndef CMP_SAMPLE | |
# define CMP_SAMPLE 44100 | |
#endif | |
// 長さ[sec] : デフォルト = 1 | |
#ifndef CMP_LENGTH | |
# define CMP_LENGTH 1 | |
#endif | |
// 基本音周波数[Hz] : デフォルト = 440(ラ音) | |
#ifndef CMP_BASE | |
# define CMP_BASE 440 | |
#endif | |
// 基音からの半音数 : デフォルト = 0 | |
#ifndef CMP_SEMITONES | |
# define CMP_SEMITONES 0 | |
#endif | |
#include <cstddef> | |
#include <cstdint> | |
#include <climits> | |
#include <sprout/compost.hpp> | |
#include <sprout/array.hpp> | |
#include "wave_io.hpp" | |
int main(int argc, char* argv[]) { | |
namespace cmp = sprout::compost; | |
constexpr std::size_t sample_per_sec = CMP_SAMPLE; | |
constexpr double length = CMP_LENGTH; | |
constexpr double base = CMP_BASE; | |
constexpr double semitones = CMP_SEMITONES; | |
constexpr std::size_t size = static_cast<std::size_t>(length * sample_per_sec); | |
typedef double src_elem_t; | |
typedef sprout::array<src_elem_t, size> src_t; | |
typedef std::int16_t wav_elem_t; | |
typedef sprout::array<wav_elem_t, size> wav_t; | |
// WAVE の情報 | |
constexpr sprout::compost::sources::info_type wav_info{ | |
1, // フォーマットID | |
1, // チャンネル数 | |
sample_per_sec, // サンプリングレート | |
sample_per_sec * sizeof(wav_elem_t), // データ速度 (Byte/sec) | |
sizeof(wav_elem_t), // ブロックサイズ (Byte/sample*チャンネル数) | |
sizeof(wav_elem_t) * CHAR_BIT, // サンプルあたりのビット数 (bit/sample) | |
size // 要素数 | |
}; | |
// 波形の生成 | |
constexpr wav_t wav_data = | |
cmp::waves::sinusoidal( // 指定音階の正弦波を生成 | |
cmp::equal_temperament_value(semitones) * base / sample_per_sec, | |
0.8 | |
) | |
| cmp::formats::as_pcm_wave16 // 16bitWAVE に変換 | |
| cmp::ranges::copied // 任意のコンテナに変換可能にする | |
; | |
// ================ ここまでコンパイル時 ================ | |
// WAVE ファイルに出力 | |
char const* filename = argc >= 2 ? argv[1] : "sine_sound.wav"; | |
output_wav(filename, wav_info, wav_data); | |
// dsp デバイスのセットアップ | |
int fd = setup_dev_dsp(wav_info); | |
if (fd < 0) { | |
return 0; | |
} | |
// dsp デバイスに書き込み | |
write_dev_dsp(fd, wav_data.data(), wav_data.size() * sizeof(wav_elem_t)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment