-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctimer.c
268 lines (238 loc) · 8.9 KB
/
ctimer.c
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/********************************************************************************
Compile:
* GCC: cc ctimer.c -lasound -o ctimer
********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define PROJECT_NAME "ctimer"
#define PROJECT_VERSION "Version: 0.1"
const char *help_message = "Timer and Stopwatch writtern in C.\n\n"
"Usage:\n"
" "PROJECT_NAME" [OPTIONS] [FORMAT]\n"
"Format:\n"
" 0s Seconds\n"
" 0m Minute\n"
" 0h Hour\n"
"Example:\n"
" "PROJECT_NAME" 1h 5m 30s\n"
"Options:\n"
" -d --d-beep Duration of beep for Timer in seconds (default: 1)\n"
" -n --no-beep Disable beep for Timer\n"
" -h --help Print help\n"
" -v --version Print version\n\n"
PROJECT_VERSION
" | SPDX-License-Identifier: MIT (https://spdx.org/licenses/MIT)\n";
//===============================================================================
/*******************************************************************************
* Following code is taken from https://github.com/zserge/beep by Serge Zaitsev
* Some parts of the code is modified by me.
*
*-------------------------------------------------------------------------------
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Serge Zaitsev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*******************************************************************************/
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
/* On Windows use the built-in Beep() function from <utilapiset.h> */
int beep(int freq, int ms) { return Beep(freq, ms); }
#elif __linux__
/* On Linux use alsa in synchronous mode, open "default" device in signed 8-bit
* mode at 8kHz, mono, request for 20ms latency. Device is opened on first call
* and never closed. */
#include <alsa/asoundlib.h>
int beep(int freq, int ms) {
static snd_pcm_t *pcm = NULL;
if (pcm == NULL) {
if (snd_pcm_open(&pcm, "default", 0, 0)) {
return -1;
}
snd_pcm_set_params(pcm, 1, 3, 1, 8000, 1, 20000);
}
unsigned char buf[2400];
for (int i = 0; i < ms / 50; i++) {
snd_pcm_prepare(pcm);
for (long unsigned int j = 0; j < sizeof(buf); j++) {
buf[j] = freq > 0 ? (500 * j * freq / 8000) : 0;
}
int r = snd_pcm_writei(pcm, buf, sizeof(buf));
if (r < 0) {
snd_pcm_recover(pcm, r, 0);
}
}
return 0;
}
#elif __APPLE__
#include <AudioUnit/AudioUnit.h>
static dispatch_semaphore_t stopped, playing, done;
static int beep_freq;
static int beep_samples;
static int counter = 0;
static int initialized = 0;
static unsigned char theta = 0;
static OSStatus tone_cb(void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber,
UInt32 inNumberFrames, AudioBufferList *ioData) {
unsigned int frame;
unsigned char *buf = ioData->mBuffers[0].mData;
unsigned long i = 0;
for (i = 0; i < inNumberFrames; i++) {
while (counter == 0) {
dispatch_semaphore_wait(playing, DISPATCH_TIME_FOREVER);
counter = beep_samples;
}
buf[i] = beep_freq > 0 ? (255 * theta * beep_freq / 8000) : 0;
theta++;
counter--;
if (counter == 0) {
dispatch_semaphore_signal(done);
dispatch_semaphore_signal(stopped);
}
}
return 0;
}
int beep(int freq, int ms) {
if (!initialized) {
AudioComponent output;
AudioUnit unit;
AudioComponentDescription descr;
AURenderCallbackStruct cb;
AudioStreamBasicDescription stream;
initialized = 1;
stopped = dispatch_semaphore_create(1);
playing = dispatch_semaphore_create(0);
done = dispatch_semaphore_create(0);
descr.componentType = kAudioUnitType_Output,
descr.componentSubType = kAudioUnitSubType_DefaultOutput,
descr.componentManufacturer = kAudioUnitManufacturer_Apple,
cb.inputProc = tone_cb;
stream.mFormatID = kAudioFormatLinearPCM;
stream.mFormatFlags = 0;
stream.mSampleRate = 8000;
stream.mBitsPerChannel = 8;
stream.mChannelsPerFrame = 1;
stream.mFramesPerPacket = 1;
stream.mBytesPerFrame = 1;
stream.mBytesPerPacket = 1;
output = AudioComponentFindNext(NULL, &descr);
AudioComponentInstanceNew(output, &unit);
AudioUnitSetProperty(unit, kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Input, 0, &cb, sizeof(cb));
AudioUnitSetProperty(unit, kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input, 0, &stream, sizeof(stream));
AudioUnitInitialize(unit);
AudioOutputUnitStart(unit);
}
dispatch_semaphore_wait(stopped, DISPATCH_TIME_FOREVER);
beep_freq = freq;
beep_samples = ms * 8;
dispatch_semaphore_signal(playing);
dispatch_semaphore_wait(done, DISPATCH_TIME_FOREVER);
return 0;
}
#else
#error "unknown platform"
#endif
//===============================================================================
int main(int argc, char *argv[]) {
unsigned int hour = 0, minute = 0, second = 0, time = 0;
unsigned int max_hour = 0, max_minute = 0, max_second = 0, max_time = 0;
char on_beep = 1;
int d_beep = 1;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
printf(help_message);
return 0;
} else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) {
printf(PROJECT_VERSION);
puts("");
return 0;
} else if (strcmp(argv[i], "-n") == 0 || strcmp(argv[i], "--no-beep") == 0) {
on_beep = 0;
continue;
} else if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--d-beep") == 0) {
if (argv[i+1] == NULL) {
fprintf(
stderr, "Error: `%s` requires integer value. No value is given.\n\n%s",
argv[i], help_message
);
exit(1);
}
d_beep = atoi(argv[i+1]);
if (d_beep == 0 && strcmp(argv[i+1], "0") != 0) {
fprintf(
stderr, "Error: `%s` only supports integer values. Given value: %s\n\n%s",
argv[i], argv[i+1], help_message
);
exit(1);
}
i++;
continue;
} else if (argv[i][0] == '-') {
fprintf(stderr, "Error: wrong argument provided `%s`\n\n%s", argv[i], help_message);
exit(1);
}
size_t arg_len = strlen(argv[i]);
char arg_end = argv[i][arg_len-1];
char arg_num[4];
strncpy (arg_num, argv[i], arg_len-1);
if (arg_end == 'h') max_hour = atoi(arg_num);
else if (arg_end == 'm') max_minute = atoi(arg_num);
else if (arg_end == 's') max_second = atoi(arg_num);
else {
fprintf(stderr, "Error: wrong argument provided `%s`\n\n%s", argv[i], help_message);
exit(1);
}
}
max_time = (max_hour*60*60) + (max_minute*60) + max_second;
while(1) {
time = (hour*60*60) + (minute*60) + second;
if (max_time == 0) {
printf("\rStopwatch: %02d:%02d:%02d", hour, minute, second);
} else {
unsigned int progress = (time*100)/max_time;
unsigned int i = 0;
printf("\rTimer: %02d:%02d:%02d ", hour, minute, second);
for (; i < progress/2; i++) printf("█");
for (; i < 50; i++) printf("░");
printf(" %2d%%", (time*100)/max_time);
if (time == max_time) {
printf("\n");
if (on_beep) beep(10, d_beep*1000);
break;
}
}
fflush(stdout);
second++;
if (second == 60) {
minute += 1;
second = 0;
}
if(minute == 60) {
hour += 1;
minute = 0;
}
sleep(1);
}
}