-
Notifications
You must be signed in to change notification settings - Fork 4
/
class.bpm.php
197 lines (182 loc) · 5.41 KB
/
class.bpm.php
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
<?php
/*
* bpm_detect rev.4
* written by deseven
* dependencies: php 5.2+, soundtouch, (sox, mpg123 OR ffmpeg)
* website: http://deseven.info
*/
const USE_FFMPEG = true;
const USE_SOX = false;
const DETECT_LENGTH = false;
const SPLIT = true;
const DEF = false;
class bpm_detect {
const split_seconds = 16;
const default_path = "/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin";
protected $workdir;
protected $start_time;
protected $split_seconds;
protected $file;
protected $file_short;
protected $file_ext;
protected $file_length = 0;
protected $delete_file = false;
protected $use_ffmpeg = false;
protected $cmdlog = array();
function __construct($filepath,$length = false,$use_ffmpeg = false,$path = false) {
$this->start_time = microtime(true);
if (!$path) {
putenv("PATH=".$this::default_path);
} else {
putenv("PATH=".$path);
}
$this->workdir = getcwd();
$filedir = pathinfo($filepath,PATHINFO_DIRNAME);
if ((strlen($filedir)) && ($filedir != ".")) {
$this->file = $filedir."/".pathinfo($filepath,PATHINFO_FILENAME);
} else {
$this->file = pathinfo($filepath,PATHINFO_FILENAME);
}
$this->file_short = pathinfo($filepath,PATHINFO_FILENAME);
$this->file_ext = pathinfo($filepath,PATHINFO_EXTENSION);
if ($use_ffmpeg) {
$this->use_ffmpeg = true;
}
if (!$length) {
if (!$this->use_ffmpeg) {
$cmd = 'mpg123 -t "'.$this->file.".".$this->file_ext.'" 2>&1';
exec($cmd,$file_length);
$this->cmdlog[] = $cmd;
foreach ($file_length as $line) {
if (strpos($line,$this->file_short.".".$this->file_ext." finished") !== false) {
$line = preg_match("/\d+\:\d+/",$line,$match);
$line = explode(":",$match[0]);
$this->file_length = intval($line[0],10)*60 + intval($line[1],10);
break;
}
}
} else {
$cmd = 'ffprobe "'.$this->file.".".$this->file_ext.'" -show_format 2>&1';
exec($cmd,$file_length);
$cmdlog = $cmd;
foreach ($file_length as $line) {
if (strpos($line,"duration=") !== false) {
$line = explode("=",$line);
$this->file_length = round($line[1]);
break;
}
}
}
} else {
$this->file_length = $length;
}
}
function __destruct() {
if ($this->delete_file) {
chdir($this->workdir);
if (file_exists($this->file.".".$this->file_ext)) {
unlink($this->file.".".$this->file_ext);
}
}
}
public function getProcessingTime() {
$time = round(microtime(true) - $this->start_time,2);
$this->start_time = microtime(true);
return $time;
}
public function getCmdLog() {
return $this->cmdlog;
}
public function getSplitSeconds() {
if ($this->split_seconds) {
return $this->split_seconds;
} else {
return $this::split_seconds;
}
}
private function convertToWAV() {
if (!$this->use_ffmpeg) {
$cmd = 'mpg123 -q -w "'.$this->file.'.wav" "'.$this->file.".".$this->file_ext.'" 2>&1';
exec($cmd);
$this->cmdlog[] = $cmd;
} else {
$cmd = 'ffmpeg -y -i "'.$this->file.".".$this->file_ext.'" -acodec pcm_s16le "'.$this->file.'.wav" 2>&1';
exec($cmd);
$this->cmdlog[] = $cmd;
}
$this->file_ext = "wav";
}
public function getLength() {
return round($this->file_length,2);
}
public function detectBPM($split = false,$split_seconds = false) {
$bpm = 0;
if ($this->file_ext != "wav") {
if ((!$this->use_ffmpeg) && ($this->file_ext != "mp3")) {
return 0;
} else {
$this->convertToWAV();
$this->delete_file = true;
}
}
if (!$split_seconds) {
$this->split_seconds = $this::split_seconds;
} else {
$this->split_seconds = $split_seconds;
}
if (file_exists($this->file.".".$this->file_ext)) {
if ($split) {
if (strlen($this->file_length)) {
$pieces = ceil($this->file_length / $this->split_seconds);
if ($pieces) {
for ($piece = 1; $piece <= $pieces; $piece++) {
if (!$this->use_ffmpeg) {
$cmd = 'sox "'.$this->file.".".$this->file_ext.'" "'.$this->file."-$piece.".$this->file_ext.'" trim '.$piece * $this->split_seconds." ".$this->split_seconds." 2>&1";
exec($cmd);
$this->cmdlog[] = $cmd;
} else {
$cmd = 'ffmpeg -y -ss '.$piece * $this->split_seconds.' -i "'.$this->file.".".$this->file_ext.'" -t '.$this->split_seconds.' -acodec copy "'.$this->file."-$piece.".$this->file_ext.'" 2>&1';
exec($cmd);
$this->cmdlog[] = $cmd;
}
if (file_exists($this->file."-$piece.".$this->file_ext)) {
$cmd = 'soundstretch "'.$this->file."-$piece.".$this->file_ext.'" -bpm 2>&1';
exec($cmd,$piece_bpm);
$this->cmdlog[] = $cmd;
foreach ($piece_bpm as $line) {
if (strpos($line,"Detected BPM rate") !== false) {
$line = explode(" ",$line);
$piece_bpm = round($line[3]);
break;
}
}
unlink($this->file."-$piece.".$this->file_ext);
$pieces_bpm[] = round(intval($piece_bpm));
}
}
if (count($pieces_bpm)) {
$bpm = $pieces_bpm;
}
}
}
} else {
$this->split_seconds = -1;
$cmd = 'soundstretch "'.$this->file.".".$this->file_ext.'" -bpm 2>&1';
exec($cmd,$average_bpm);
$this->cmdlog[] = $cmd;
foreach ($average_bpm as $line) {
if (strpos($line,"Detected BPM rate") !== false) {
$line = explode(" ",$line);
$average_bpm = round($line[3]);
break;
}
}
if ((!is_array($average_bpm)) && (strlen($average_bpm))) {
$bpm = round($average_bpm);
}
}
}
return $bpm;
}
}
?>