-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathterminal_rpg_music.c
143 lines (123 loc) · 3.53 KB
/
terminal_rpg_music.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
/*
* =====================================================================================
*
* Filename: terminal_rpg_music.c
*
* Description:
*
* Version: 1.0
* Created: 02/18/2019 02:13:37 PM
* Revision: none
* Compiler: gcc
*
* Author: Miguel Fernandes
* Organization:
*
* =====================================================================================
*/
#include <stdlib.h>
#include <stdio.h>
#include <ncurses.h>
#include "terminal_rpg_music.h"
int music_volume = 10; //0-10, 0 = 0%, 5 = 50%, 10 = 100%, volume increases and decreases by 10%.
const char * fifo = "/tmp/mplayercontrol";
const char * audio_tracks = "audio_tracks/*.mp3";
const char * log_file = "audio_tracks/log.txt";
//Using mkfifo to create a file onto which to echo commands to mplayer;
//First (%s) is 'fifo' and second is 'log_file';
const char * mkfifo = "mkfifo %s &> %s";
//First (%s) is 'fifo', then (%d) is '(music_volume * 10)', the last two (%s) are 'audio_tracks' and 'log_file';
const char * mplayer = "mplayer -slave -input file=%s -softvol -volstep 10 -volume %d -loop 0 %s &>> %s &";
//(%s) is 'fifo' for each.
const char * volume_dec = "echo 'volume -1 0' >> %s";
const char * volume_inc = "echo 'volume 1 0' >> %s";
const char * quit_mplayer = "echo 'quit 0' >> %s";
const char * error_closing_file = "There was an error closing the file %s !!!";
void music_inc_music_volume () {
music_volume++;
char temp [128];
sprintf(temp, volume_inc, fifo);
system(temp);
}
void music_dec_music_volume () {
music_volume--;
char temp [128];
sprintf(temp, volume_dec, fifo);
system(temp);
}
int music_set_mplayer () {
if((music_volume = music_read_m_volume(log_file)) == -1)
music_volume = 10;
if(music_volume == -2) {
mvprintw(0,0, error_closing_file, log_file);
getch();
}
int m_vol = (10 * music_volume);
char temp [256];
sprintf(temp, mkfifo, fifo, log_file);
system(temp);
sprintf(temp, mplayer, fifo, m_vol, audio_tracks, log_file);
system(temp);
return 0;
}
int music_quit_mplayer () {
char temp [128];
sprintf(temp, quit_mplayer, fifo);
system(temp);
if(music_write_m_volume(log_file) == 1)
return 1;
return 0;
}
int music_write_m_volume (const char * filename) {
FILE * fs;
fs = fopen(filename, "w");
fprintf(fs, "%d\n", music_volume);
if(fclose(fs) == EOF) {
mvprintw(0,0, error_closing_file, log_file);
getch();
}
return 0;
}
int music_read_m_volume (const char * filename) {
FILE * fs;
fs = fopen(filename, "a+");
int i = fgetc(fs);
int mvol = -1;
if(i >= '\x30' && i <= '\x39') {
mvol = i - 48;
if(i == '\x31') {
i = fgetc(fs);
if(i == '\n') {
if(fclose(fs) == EOF)
return -2;
return mvol;
}
if(i == '\x30') {
i = fgetc(fs);
if(i == '\n') {
mvol = 10;
if(fclose(fs) == EOF)
return -2;
return mvol;
}
}
else {
if(fclose(fs) == EOF)
return -2;
return -1;
}
}
else {
i = fgetc(fs);
if(i != '\n')
mvol = -1;
}
}
if(fclose(fs) == EOF)
return -2;
return mvol;
}
int music_return_music_volume () {
return music_volume;
}
//eof