3 Copyright (C) 1995 Be Incorporated. All Rights Reserved.
5 modified on November 03, 2003 by Jerome Duval
8 * was released as sample code; source:
9 * ftp://planetmirror.com/raid/13/beos/samples/media_kit/obsolete/play.cpp
18 #include <Directory.h>
28 play(int id
, scsi_play_track
*track
)
30 track
->start_index
= 1;
31 track
->end_track
= 99;
33 if (!ioctl(id
, B_SCSI_PLAY_TRACK
, track
))
34 printf("Playing audio...\n");
36 printf("Play audio failed\n");
41 show valid cd-rom id's
42 (from /boot/optional/sample-code/interface_kit/CDButton/CDEngine.cpp)
46 try_dir(const char *directory
, long *count
, bool show
)
51 if(dir
.InitCheck() != B_NO_ERROR
) {
56 while(dir
.GetNextEntry(&entry
) >= 0) {
61 if(entry
.GetPath(&path
) != B_NO_ERROR
)
65 if(entry
.GetRef(&e
) != B_NO_ERROR
)
68 if(entry
.IsDirectory()) {
69 if(strcmp(e
.name
, "floppy") == 0)
70 continue; // ignore floppy (it is not silent)
71 try_dir(name
, count
, show
);
77 if(strcmp(e
.name
, "raw") != 0)
78 continue; // ignore partitions
80 devfd
= open(name
, O_RDONLY
);
84 if(ioctl(devfd
, B_GET_GEOMETRY
, &g
, sizeof(g
)) >= 0) {
85 if(g
.device_type
== B_CD
)
95 printf(" %s\n", name
);
101 show valid cd-rom id's
104 int count_ids(bool show
)
108 try_dir("/dev/disk", &count
, show
);
118 main (int argc
, char **argv
)
132 scsi_play_track track
;
134 scsi_position position
;
135 scsi_read_cd read_cd
;
139 length
= count_ids(FALSE
);
141 printf("No CD-ROM drive present.\n");
147 command
= strtol (argv
[2], 0, 0);
151 if ((argc
< 2) || (errno
) ||
152 (command
< 0) || (command
> 8)) {
153 printf("Usage: play device [command [param]]\n\n");
154 printf(" Valid devices:\n");
157 printf(" Valid commands:\n");
158 printf(" 0 [n] - play from track n [1]\n");
159 printf(" 1 - pause\n");
160 printf(" 2 - resume\n");
161 printf(" 3 - stop\n");
162 printf(" 4 - eject\n");
163 printf(" 5 n - set volume to n (0 <= n <= 255)\n");
164 printf(" 6 - current position\n");
165 printf(" 7 n s - save track n to file s\n");
166 printf(" 8 [c] - scan in direction c (f = forward, b = backward)\n\n");
170 if ((id
= open(argv
[1], 0)) >= 0) {
173 if (!ioctl(id
, B_SCSI_GET_TOC
, &toc
)) {
174 track
.start_track
= 0;
175 for (i
= toc
.toc_data
[2]; i
<= toc
.toc_data
[3]; i
++) {
176 length
= (toc
.toc_data
[(((i
+ 1) - toc
.toc_data
[2]) * 8) + 4 + 5] * 60) +
177 (toc
.toc_data
[(((i
+ 1) - toc
.toc_data
[2]) * 8) + 4 + 6]);
178 length
-= ((toc
.toc_data
[((i
- toc
.toc_data
[2]) * 8) + 4 + 5] * 60) +
179 (toc
.toc_data
[((i
- toc
.toc_data
[2]) * 8) + 4 + 6]));
180 printf(" Track %.2d: %.2d:%.2d - ", i
, length
/ 60, length
% 60);
181 if (toc
.toc_data
[((i
- toc
.toc_data
[2]) * 8) + 4 + 1] & 4)
185 if (!track
.start_track
)
186 track
.start_track
= i
;
189 if (track
.start_track
) {
191 req_track
= strtol (argv
[3], 0, 0);
192 if ((req_track
< toc
.toc_data
[2]) || (req_track
> toc
.toc_data
[3]))
193 printf("Requested track is out of range\n");
195 if (toc
.toc_data
[((req_track
- toc
.toc_data
[2]) * 8) + 4 + 1] & 4)
196 printf("Requested track is not an audio track\n");
198 track
.start_track
= req_track
;
206 printf("No audio tracks on CD\n");
209 printf("Could not read table of contents on CD\n");
213 printf("Pausing audio\n");
214 ioctl(id
, B_SCSI_PAUSE_AUDIO
);
218 printf("Resuming audio\n");
219 ioctl(id
, B_SCSI_RESUME_AUDIO
);
223 printf("Stopping audio\n");
224 ioctl(id
, B_SCSI_STOP_AUDIO
);
228 printf("Ejecting CD\n");
229 ioctl(id
, B_SCSI_EJECT
);
234 printf("Specify the volume\n");
236 vol
= strtol (argv
[3], 0, 0);
237 if ((vol
< 0) || (vol
> 255))
238 printf("Volume is out of range (0 - 255)\n");
240 printf("Setting volume to %ld\n", vol
);
241 volume
.port0_volume
= vol
;
242 volume
.port1_volume
= vol
;
243 volume
.flags
= B_SCSI_PORT0_VOLUME
| B_SCSI_PORT1_VOLUME
;
244 ioctl(id
, B_SCSI_SET_VOLUME
, &volume
);
250 if (ioctl(id
, B_SCSI_GET_POSITION
, &position
) == B_ERROR
)
251 printf("Could not get current position\n");
253 switch(position
.position
[1]) {
255 printf("Position not supported by device\n");
259 printf("Playing track %d (%.2d:%.2d:%.2d)\n",
260 position
.position
[6],
261 position
.position
[9],
262 position
.position
[10],
263 position
.position
[11]);
267 printf("Paused at track %d (%.2d:%.2d:%.2d)\n",
268 position
.position
[6],
269 position
.position
[9],
270 position
.position
[10],
271 position
.position
[11]);
275 printf("Play has been completed\n");
279 printf("Play stopped due to error\n");
283 printf("No status to return\n");
287 printf("Unexpected result: %.2x\n",
288 position
.position
[1]);
295 printf("Specify the track to save\n");
300 printf("Specify a file to save to\n");
304 f
= fopen(argv
[4], "w");
306 printf("Un-able to create %s\n", argv
[4]);
310 req_track
= strtol (argv
[3], 0, 0);
311 if (!ioctl(id
, B_SCSI_GET_TOC
, &toc
)) {
312 if (req_track
> toc
.toc_data
[3]) {
313 printf("Track %ld is out of range [%d-%d]\n", req_track
,
314 toc
.toc_data
[2], toc
.toc_data
[3]);
318 while (toc
.toc_data
[4 + (index
* 8) + 2] != req_track
) {
321 start
= (toc
.toc_data
[4 + (index
* 8) + 5] * 60 * 75) +
322 (toc
.toc_data
[4 + (index
* 8) + 6] * 75) +
323 toc
.toc_data
[4 + (index
* 8) + 7];
325 length
= ((toc
.toc_data
[4 + (index
* 8) + 5] * 60 * 75) +
326 (toc
.toc_data
[4 + (index
* 8) + 6] * 75) +
327 toc
.toc_data
[4 + (index
* 8) + 7]) - start
;
329 frames
= min_c(1 * 75, (int) length
);
330 read_cd
.buffer
= (char *)malloc(frames
* 2352);
332 printf("Saving track %ld to %s...\n", req_track
, argv
[4]);
335 read_cd
.start_m
= index
/ (60 * 75);
337 read_cd
.start_s
= index
/ 75;
339 read_cd
.start_f
= index
;
341 index
= min_c((int)frames
, length
);
342 read_cd
.buffer_length
= index
* 2352;
346 read_cd
.length_m
= index
/ (60 * 75);
348 read_cd
.length_s
= index
/ 75;
350 read_cd
.length_f
= index
;
352 for (i
= 0; i
< 5; i
++)
353 if (ioctl(id
, B_SCSI_READ_CD
, &read_cd
) == B_NO_ERROR
)
356 printf("Error reading CD-DA\n");
359 buf
= (uchar
*)read_cd
.buffer
;
360 for (i
= 0; i
< read_cd
.buffer_length
; i
+= 2) {
361 tmp
= (short)((buf
[1] << 8) | buf
[0]);
362 *(short *)(&buf
[0]) = tmp
;
365 fwrite(read_cd
.buffer
, read_cd
.buffer_length
, 1, f
);
367 free(read_cd
.buffer
);
371 printf("Failed to read table of contents\n");
376 if (!strcmp(argv
[3], "f") || !strcmp(argv
[3], "F"))
378 else if (!strcmp(argv
[3], "b") || !strcmp(argv
[3], "B"))
381 printf("Use 'f' to scan forward, 'b' to scan backwards\n");
388 if (ioctl(id
, B_SCSI_SCAN
, &scan
) == B_ERROR
)
389 printf("Error trying to scan\n");
391 printf("Scanning...\n");
397 printf("CD player is empty or invalid scsi-id\n");