forked from fulldecent/system-bus-radio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
103 lines (87 loc) · 2.65 KB
/
main.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
99
100
101
102
103
// SYSTEM BUS RADIO
// https://github.com/fulldecent/system-bus-radio
// Copyright 2016 William Entriken
// C++11 port by Ryou Ezoe
#include <iostream>
#include <iomanip>
#include <chrono>
#include <thread>
#include <atomic>
#include <mutex>
#include <condition_variable>
std::mutex m ;
std::condition_variable cv ;
std::chrono::high_resolution_clock::time_point mid ;
std::chrono::high_resolution_clock::time_point reset ;
void boost_song()
{
using namespace std::chrono ;
while( true )
{
std::unique_lock<std::mutex> lk{m} ;
cv.wait( lk ) ;
std::atomic<unsigned> x{0} ;
while( high_resolution_clock::now() < mid )
{
++x ;
}
std::this_thread::sleep_until( reset ) ;
}
}
void square_am_signal(float time, float frequency)
{
using namespace std::chrono ;
std::cout << "Playing / " << time << " seconds / " << frequency << " Hz\n" ;
seconds const sec{1} ;
nanoseconds const nsec{ sec } ;
using rep = nanoseconds::rep ;
auto nsec_per_sec = nsec.count() ;
nanoseconds const period( static_cast<rep>( nsec_per_sec / frequency) ) ;
auto start = high_resolution_clock::now() ;
auto const end = start + nanoseconds( static_cast<rep>(time * nsec_per_sec) ) ;
while (high_resolution_clock::now() < end)
{
mid = start + period / 2 ;
reset = start + period ;
cv.notify_all() ;
std::this_thread::sleep_until( reset ) ;
start = reset;
}
}
int main()
{
for ( unsigned i = 0 ; i < std::thread::hardware_concurrency() ; ++i )
{
std::thread t( boost_song ) ;
t.detach() ;
}
while (1)
{
square_am_signal(0.400, 2673);
square_am_signal(0.400, 2349);
square_am_signal(0.400, 2093);
square_am_signal(0.400, 2349);
square_am_signal(0.400, 2673);
square_am_signal(0.400, 2673);
square_am_signal(0.790, 2673);
square_am_signal(0.400, 2349);
square_am_signal(0.400, 2349);
square_am_signal(0.790, 2349);
square_am_signal(0.400, 2673);
square_am_signal(0.400, 3136);
square_am_signal(0.790, 3136);
square_am_signal(0.400, 2673);
square_am_signal(0.400, 2349);
square_am_signal(0.400, 2093);
square_am_signal(0.400, 2349);
square_am_signal(0.400, 2673);
square_am_signal(0.400, 2673);
square_am_signal(0.400, 2673);
square_am_signal(0.400, 2673);
square_am_signal(0.400, 2349);
square_am_signal(0.400, 2349);
square_am_signal(0.400, 2673);
square_am_signal(0.400, 2349);
square_am_signal(0.790, 2093);
}
}