Gambas France BETA


Pas de compte ? Incription

Exécuter fichiers audio MP3 en utilisant les Classes MediaPipeline et MediaControl du Composant gb.media

1
AuteurMessages
vuott#1 Posté le 21/8/2017 à 01:28:41
Ne cedere ineluctabili possimusCiao, :heart:

mon code pour exécuter file audio MP3 en utilisant les Classes "MediaPipeline" et "MediaControl" du Composant "gb.media".

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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
PUBLIC SUB Main()

DIM pl AS MediaPipeline
DIM src, dcd, snk AS MediaControl
DIM fileMP3 AS STRING
DIM dur, i AS INTEGER
DIM tm AS DATE

fileMP3 = "/Emplacement/du/fichier.mp3"
IF NOT Exist(fileMP3) THEN Error.Raise("Erreur, ton file MP3 n'existe pas !")

pl = NEW MediaPipeline

src = NEW MediaControl(pl, "filesrc")
src["location"] = fileMP3
dcd = NEW MediaControl(pl, "mad")
snk = NEW MediaControl(pl, "alsasink")
src.LinkTo(dcd)
dcd.LinkTo(snk)

pl.Play()

' Calls the function to get mp3 file duration:
dur = DurataMP3(fileMP3)

tm = Now

REPEAT
i = DateDiff(tm, Now, gb.Millisecond)
WRITE "\rTemps ecoule: " & Date(0, 0, 0, 0, 0, 0, i)
WAIT 0.01
UNTIL i >= dur

' Frees memory and goes to end:
pl.Stop
pl.Close

WRITE "\nExécution complétée !"

END


PRIVATE FUNCTION DurataMP3(mp3 AS STRING) AS INTEGER

DIM s, ver_mp3, layer AS STRING
DIM j, frequenza, durata AS INTEGER
DIM vB, lB, brB, frB AS BYTE
DIM initium, bitrate AS SHORT

PRINT "Fichier audio mp3: "; File.Name(mp3)

s = File.Load(mp3)
PRINT "\nTaille: "; Len(s); " byte"

initium = 1

FOR j = initium TO Len(s) - 1
IF (Asc(s, j) = 255) AND (Asc(s, j + 1) > 241) AND (Asc(s, j + 2) > 15) THEN

' Find the MP3 file version:
vB = Asc(s, j + 1) AND 24
SELECT CASE vB
CASE 0
ver_mp3 = "2.5"
CASE 16
ver_mp3 = "2"
CASE 24
ver_mp3 = "1"
END SELECT

' Find the "Layer" of the mp3 file:
lB = Asc(s, j + 1) AND 6
SELECT CASE lB
CASE 2
layer = "III"
CASE 4
layer = "II"
CASE 6
layer = "I"
END SELECT

PRINT "Version MPEG = "; ver_mp3, "Layer = "; layer

' Extract the frequency and bitrate values of MP3 file:
brB = Asc(s, j + 2) AND 240
bitrate = EstraeBitRate(ver_mp3, layer, brB)

frB = Asc(s, j + 2) AND 12
frequenza = EstraeFrequenza(ver_mp3, frB)

EXIT

ENDIF
NEXT

durata = Fix((Len(s) / bitrate) * 8)

PRINT "BitRate = "; bitrate; " kbps"
PRINT "Frequence = hz "; frequenza
PRINT "Duree = "; Date(0, 0, 0, 0, 0, 0, durata)

RETURN durata

END


PRIVATE FUNCTION EstraeBitRate(Vmpeg AS STRING, layB AS STRING, bitB AS BYTE) AS SHORT

DIM velCamp AS SHORT

IF Vmpeg = "1" THEN ' In case of Mpeg vers. 1
SELECT CASE layB ' Check the Layer
CASE "I"
SELECT CASE bitB
CASE 16
velCamp = 32
CASE 32
velCamp = 64
CASE 48
velCamp = 96
CASE 64
velCamp = 128
CASE 80
velCamp = 160
CASE 96
velCamp = 192
CASE 112
velCamp = 224
CASE 128
velCamp = 256
CASE 144
velCamp = 288
CASE 160
velCamp = 320
CASE 176
velCamp = 352
CASE 192
velCamp = 384
CASE 208
velCamp = 416
CASE 224
velCamp = 448
END SELECT
CASE "II"
SELECT CASE bitB
CASE 16
velCamp = 32
CASE 32
velCamp = 48
CASE 48
velCamp = 56
CASE 64
velCamp = 64
CASE 80
velCamp = 80
CASE 96
velCamp = 96
CASE 112
velCamp = 112
CASE 128
velCamp = 128
CASE 144
velCamp = 160
CASE 160
velCamp = 192
CASE 176
velCamp = 224
CASE 192
velCamp = 256
CASE 208
velCamp = 320
CASE 224
velCamp = 384
END SELECT
CASE "III"
SELECT CASE bitB
CASE 16
velCamp = 32
CASE 32
velCamp = 40
CASE 48
velCamp = 48
CASE 64
velCamp = 56
CASE 80
velCamp = 64
CASE 96
velCamp = 80
CASE 112
velCamp = 96
CASE 128
velCamp = 112
CASE 144
velCamp = 128
CASE 160
velCamp = 160
CASE 176
velCamp = 192
CASE 192
velCamp = 224
CASE 208
velCamp = 256
CASE 224
velCamp = 320
END SELECT
END SELECT

ELSE

SELECT CASE layB ' Check the Layer
CASE "I"
SELECT CASE bitB
CASE 16
velCamp = 32
CASE 32
velCamp = 48
CASE 48
velCamp = 56
CASE 64
velCamp = 64
CASE 80
velCamp = 80
CASE 96
velCamp = 96
CASE 112
velCamp = 112
CASE 128
velCamp = 128
CASE 144
velCamp = 144
CASE 160
velCamp = 160
CASE 176
velCamp = 176
CASE 192
velCamp = 192
CASE 208
velCamp = 224
CASE 224
velCamp = 256
END SELECT
CASE "II" TO "III"
SELECT CASE bitB
CASE 16
velCamp = 8
CASE 32
velCamp = 16
CASE 48
velCamp = 24
CASE 64
velCamp = 32
CASE 80
velCamp = 40
CASE 96
velCamp = 48
CASE 112
velCamp = 56
CASE 128
velCamp = 64
CASE 144
velCamp = 80
CASE 160
velCamp = 96
CASE 176
velCamp = 112
CASE 192
velCamp = 128
CASE 208
velCamp = 144
CASE 224
velCamp = 320
END SELECT
END SELECT

ENDIF

RETURN velCamp

END


PRIVATE FUNCTION EstraeFrequenza(Vmpeg AS STRING, fre AS BYTE) AS INTEGER

DIM frq AS INTEGER

SELECT CASE Vmpeg
CASE "1" ' In case of Mpeg vers. 1
SELECT CASE fre
CASE 0
frq = 44100
CASE 4
frq = 48000
CASE 8
frq = 32000
END SELECT
CASE "2" ' In case of Mpeg vers. 2
SELECT CASE fre
CASE 0
frq = 22050
CASE 4
frq = 24000
CASE 8
frq = 16000
END SELECT
CASE "2.5" ' In case of Mpeg vers. 2.5
SELECT CASE fre
CASE 0
frq = 11025
CASE 4
frq = 12000
CASE 8
frq = 8000
END SELECT
END SELECT

RETURN frq

END
« Vita non suavis esse potest, nec Mors amara. »
Flachy Joe#2 Posté le 21/8/2017 à 14:10:58
Iguane : Il Gambas Uniquement pour Activer ses NEuronesC'est chouette, merci.

On doit pouvoir faire quelques simplifications dans les select
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
CASE "I"
velCamp = 2*bitB
CASE "II"
SELECT CASE bitB
CASE 16
velCamp = 32
CASE 32
velCamp = 48
CASE 48
velCamp = 56
CASE 64 TO 128
velCamp = bitB
CASE 144
velCamp = 160
CASE 160
velCamp = 192
CASE 176
velCamp = 224
CASE 192
velCamp = 256
CASE 208
velCamp = 320
CASE 224
velCamp = 384
END SELECT


ou plus compact :
1
2
3
4
5
6
DIM allowedBitB AS Short[]
DIM layIIvelCamp AS Short[]
allowedBitB = [16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224]
layIIvelCamp = [32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384]

velCamp = layIIvelCamp[allowedBitB.Find(bitB)]
;) Flachy Joe ;)
1