comparison src/amidi-plug/amidi-plug.c @ 1275:b839faa693e2

updated amidi-plug to handle URIs in fileinfo and all-over in code
author Giacomo Lozito <james@develia.org>
date Sun, 15 Jul 2007 23:41:06 +0200
parents 6674f72343e7
children 504c0dc36c0a
comparison
equal deleted inserted replaced
1274:bcf6dc9564f4 1275:b839faa693e2
22 22
23 InputPlugin *amidiplug_iplist[] = { &amidiplug_ip, NULL }; 23 InputPlugin *amidiplug_iplist[] = { &amidiplug_ip, NULL };
24 24
25 DECLARE_PLUGIN(amidi-plug, NULL, NULL, amidiplug_iplist, NULL, NULL, NULL, NULL); 25 DECLARE_PLUGIN(amidi-plug, NULL, NULL, amidiplug_iplist, NULL, NULL, NULL, NULL);
26 26
27 static gboolean amidiplug_detect_by_content( gchar * filename , VFSFile * fp ) 27 static gboolean amidiplug_detect_by_content( gchar * filename_uri , VFSFile * fp )
28 { 28 {
29 gchar magic_bytes[4]; 29 gchar magic_bytes[4];
30 gint res = 0; 30 gint res = 0;
31 31
32 if ( fp == NULL ) 32 if ( fp == NULL )
35 if ( VFS_FREAD( magic_bytes , 1 , 4 , fp ) != 4 ) 35 if ( VFS_FREAD( magic_bytes , 1 , 4 , fp ) != 4 )
36 return FALSE; 36 return FALSE;
37 37
38 if ( !strncmp( magic_bytes , "MThd" , 4 ) ) 38 if ( !strncmp( magic_bytes , "MThd" , 4 ) )
39 { 39 {
40 DEBUGMSG( "MIDI found, %s is a standard midi file\n" , filename ); 40 DEBUGMSG( "MIDI found, %s is a standard midi file\n" , filename_uri );
41 return TRUE; 41 return TRUE;
42 } 42 }
43 43
44 if ( !strncmp( magic_bytes , "RIFF" , 4 ) ) 44 if ( !strncmp( magic_bytes , "RIFF" , 4 ) )
45 { 45 {
51 if ( VFS_FREAD( magic_bytes , 1 , 4 , fp ) != 4 ) 51 if ( VFS_FREAD( magic_bytes , 1 , 4 , fp ) != 4 )
52 return FALSE; 52 return FALSE;
53 53
54 if ( !strncmp( magic_bytes , "RMID" , 4 ) ) 54 if ( !strncmp( magic_bytes , "RMID" , 4 ) )
55 { 55 {
56 DEBUGMSG( "MIDI found, %s is a riff midi file\n" , filename ); 56 DEBUGMSG( "MIDI found, %s is a riff midi file\n" , filename_uri );
57 return TRUE; 57 return TRUE;
58 } 58 }
59 } 59 }
60 60
61 return FALSE; 61 return FALSE;
62 } 62 }
63 63
64 64
65 static gint amidiplug_is_our_file( gchar * filename ) 65 static gint amidiplug_is_our_file( gchar * filename_uri )
66 { 66 {
67 VFSFile * fp; 67 VFSFile * fp;
68 gboolean result = FALSE; 68 gboolean result = FALSE;
69 69
70 fp = VFS_FOPEN( filename , "rb" ); 70 fp = VFS_FOPEN( filename_uri , "rb" );
71 71
72 if ( fp == NULL ) 72 if ( fp == NULL )
73 return FALSE; 73 return FALSE;
74 74
75 result = amidiplug_detect_by_content( filename , fp ); 75 result = amidiplug_detect_by_content( filename_uri , fp );
76 VFS_FCLOSE( fp ); 76 VFS_FCLOSE( fp );
77 77
78 return result; 78 return result;
79 } 79 }
80 80
81 81
82 static gint amidiplug_is_our_file_from_vfs( gchar *filename , VFSFile *fp ) 82 static gint amidiplug_is_our_file_from_vfs( gchar *filename_uri , VFSFile *fp )
83 { 83 {
84 return amidiplug_detect_by_content( filename , fp ); 84 return amidiplug_detect_by_content( filename_uri , fp );
85 } 85 }
86 86
87 87
88 static void amidiplug_init( void ) 88 static void amidiplug_init( void )
89 { 89 {
116 { 116 {
117 i_about_gui(); 117 i_about_gui();
118 } 118 }
119 119
120 120
121 static void amidiplug_file_info_box( gchar * filename ) 121 static void amidiplug_file_info_box( gchar * filename_uri )
122 { 122 {
123 i_fileinfo_gui( filename ); 123 i_fileinfo_gui( filename_uri );
124 } 124 }
125 125
126 126
127 static void amidiplug_stop( InputPlayback * playback ) 127 static void amidiplug_stop( InputPlayback * playback )
128 { 128 {
333 } 333 }
334 return 0; 334 return 0;
335 } 335 }
336 336
337 337
338 static void amidiplug_get_song_info( gchar * filename , gchar ** title , gint * length ) 338 static void amidiplug_get_song_info( gchar * filename_uri , gchar ** title , gint * length )
339 { 339 {
340 /* song title, get it from the filename */ 340 /* song title, get it from the filename */
341 *title = G_PATH_GET_BASENAME(filename); 341 *title = G_PATH_GET_BASENAME(filename_uri);
342 342
343 /* sure, it's possible to calculate the length of a MIDI file anytime, 343 /* sure, it's possible to calculate the length of a MIDI file anytime,
344 but the file must be entirely parsed to calculate it; this could 344 but the file must be entirely parsed to calculate it; this could
345 lead to a bit of performance loss, so let the user decide here */ 345 lead to a bit of performance loss, so let the user decide here */
346 if ( amidiplug_cfg_ap.ap_opts_length_precalc ) 346 if ( amidiplug_cfg_ap.ap_opts_length_precalc )
347 { 347 {
348 /* let's calculate the midi length, using this nice helper function that 348 /* let's calculate the midi length, using this nice helper function that
349 will return 0 if a problem occurs and the length can't be calculated */ 349 will return 0 if a problem occurs and the length can't be calculated */
350 midifile_t mf; 350 midifile_t mf;
351 351
352 if ( i_midi_parse_from_filename( filename , &mf ) ) 352 if ( i_midi_parse_from_filename( filename_uri , &mf ) )
353 *length = (gint)(mf.length / 1000); 353 *length = (gint)(mf.length / 1000);
354 else 354 else
355 *length = -1; 355 *length = -1;
356 356
357 i_midi_free( &mf ); 357 i_midi_free( &mf );
361 361
362 return; 362 return;
363 } 363 }
364 364
365 365
366 static void amidiplug_play( InputPlayback * playback) 366 static void amidiplug_play( InputPlayback * playback )
367 { 367 {
368 gchar * filename = playback->filename; 368 gchar * filename_uri = playback->filename;
369 gint port_count = 0; 369 gint port_count = 0;
370 gint au_samplerate = -1, au_bitdepth = -1, au_channels = -1; 370 gint au_samplerate = -1, au_bitdepth = -1, au_channels = -1;
371
372 g_print("PLAY %s\n", filename_uri );
371 373
372 if ( backend.gmodule == NULL ) 374 if ( backend.gmodule == NULL )
373 { 375 {
374 g_warning( "No sequencer backend selected\n" ); 376 g_warning( "No sequencer backend selected\n" );
375 i_message_gui( _("AMIDI-Plug - warning") , 377 i_message_gui( _("AMIDI-Plug - warning") ,
401 g_warning( "No ports selected\n" ); 403 g_warning( "No ports selected\n" );
402 amidiplug_playing_status = AMIDIPLUG_ERR; 404 amidiplug_playing_status = AMIDIPLUG_ERR;
403 return; 405 return;
404 } 406 }
405 407
406 DEBUGMSG( "PLAY requested, opening file: %s\n" , filename ); 408 DEBUGMSG( "PLAY requested, opening file: %s\n" , filename_uri );
407 midifile.file_pointer = VFS_FOPEN( filename , "rb" ); 409 midifile.file_pointer = VFS_FOPEN( filename_uri , "rb" );
408 if (!midifile.file_pointer) 410 if (!midifile.file_pointer)
409 { 411 {
410 g_warning( "Cannot open %s\n" , filename ); 412 g_warning( "Cannot open %s\n" , filename_uri );
411 amidiplug_playing_status = AMIDIPLUG_ERR; 413 amidiplug_playing_status = AMIDIPLUG_ERR;
412 return; 414 return;
413 } 415 }
414 midifile.file_name = filename; 416 midifile.file_name = filename_uri;
415 417
416 switch( i_midi_file_read_id( &midifile ) ) 418 switch( i_midi_file_read_id( &midifile ) )
417 { 419 {
418 case MAKE_ID('R', 'I', 'F', 'F'): 420 case MAKE_ID('R', 'I', 'F', 'F'):
419 { 421 {
420 DEBUGMSG( "PLAY requested, RIFF chunk found, processing...\n" ); 422 DEBUGMSG( "PLAY requested, RIFF chunk found, processing...\n" );
421 /* read riff chunk */ 423 /* read riff chunk */
422 if ( !i_midi_file_parse_riff( &midifile ) ) 424 if ( !i_midi_file_parse_riff( &midifile ) )
423 WARNANDBREAKANDPLAYERR( "%s: invalid file format (riff parser)\n" , filename ); 425 WARNANDBREAKANDPLAYERR( "%s: invalid file format (riff parser)\n" , filename_uri );
424 426
425 /* if that was read correctly, go ahead and read smf data */ 427 /* if that was read correctly, go ahead and read smf data */
426 } 428 }
427 429
428 case MAKE_ID('M', 'T', 'h', 'd'): 430 case MAKE_ID('M', 'T', 'h', 'd'):
429 { 431 {
430 DEBUGMSG( "PLAY requested, MThd chunk found, processing...\n" ); 432 DEBUGMSG( "PLAY requested, MThd chunk found, processing...\n" );
431 if ( !i_midi_file_parse_smf( &midifile , port_count ) ) 433 if ( !i_midi_file_parse_smf( &midifile , port_count ) )
432 WARNANDBREAKANDPLAYERR( "%s: invalid file format (smf parser)\n" , filename ); 434 WARNANDBREAKANDPLAYERR( "%s: invalid file format (smf parser)\n" , filename_uri );
433 435
434 if ( midifile.time_division < 1 ) 436 if ( midifile.time_division < 1 )
435 WARNANDBREAKANDPLAYERR( "%s: invalid time division (%i)\n" , filename , midifile.time_division ); 437 WARNANDBREAKANDPLAYERR( "%s: invalid time division (%i)\n" , filename_uri , midifile.time_division );
436 438
437 DEBUGMSG( "PLAY requested, setting ppq and tempo...\n" ); 439 DEBUGMSG( "PLAY requested, setting ppq and tempo...\n" );
438 /* fill midifile.ppq and midifile.tempo using time_division */ 440 /* fill midifile.ppq and midifile.tempo using time_division */
439 if ( !i_midi_setget_tempo( &midifile ) ) 441 if ( !i_midi_setget_tempo( &midifile ) )
440 WARNANDBREAKANDPLAYERR( "%s: invalid values while setting ppq and tempo\n" , filename ); 442 WARNANDBREAKANDPLAYERR( "%s: invalid values while setting ppq and tempo\n" , filename_uri );
441 443
442 DEBUGMSG( "PLAY requested, sequencer start\n" ); 444 DEBUGMSG( "PLAY requested, sequencer start\n" );
443 /* sequencer start */ 445 /* sequencer start */
444 if ( !backend.seq_start( filename ) ) 446 if ( !backend.seq_start( filename_uri ) )
445 WARNANDBREAKANDPLAYERR( "%s: problem with seq_start, play aborted\n" , filename ); 447 WARNANDBREAKANDPLAYERR( "%s: problem with seq_start, play aborted\n" , filename_uri );
446 448
447 DEBUGMSG( "PLAY requested, sequencer on\n" ); 449 DEBUGMSG( "PLAY requested, sequencer on\n" );
448 /* sequencer on */ 450 /* sequencer on */
449 if ( !backend.seq_on() ) 451 if ( !backend.seq_on() )
450 WARNANDBREAKANDPLAYERR( "%s: problem with seq_on, play aborted\n" , filename ); 452 WARNANDBREAKANDPLAYERR( "%s: problem with seq_on, play aborted\n" , filename_uri );
451 453
452 DEBUGMSG( "PLAY requested, setting sequencer queue tempo...\n" ); 454 DEBUGMSG( "PLAY requested, setting sequencer queue tempo...\n" );
453 /* set sequencer queue tempo using ppq and tempo (call only after i_midi_setget_tempo) */ 455 /* set sequencer queue tempo using ppq and tempo (call only after i_midi_setget_tempo) */
454 if ( !backend.seq_queue_tempo( midifile.current_tempo , midifile.ppq ) ) 456 if ( !backend.seq_queue_tempo( midifile.current_tempo , midifile.ppq ) )
455 { 457 {
456 backend.seq_off(); /* kill the sequencer */ 458 backend.seq_off(); /* kill the sequencer */
457 WARNANDBREAKANDPLAYERR( "%s: ALSA queue problem, play aborted\n" , filename ); 459 WARNANDBREAKANDPLAYERR( "%s: ALSA queue problem, play aborted\n" , filename_uri );
458 } 460 }
459 461
460 /* fill midifile.length, keeping in count tempo-changes */ 462 /* fill midifile.length, keeping in count tempo-changes */
461 i_midi_setget_length( &midifile ); 463 i_midi_setget_length( &midifile );
462 DEBUGMSG( "PLAY requested, song length calculated: %i msec\n" , (gint)(midifile.length / 1000) ); 464 DEBUGMSG( "PLAY requested, song length calculated: %i msec\n" , (gint)(midifile.length / 1000) );
463 465
464 /* our length is in microseconds, but the player wants milliseconds */ 466 /* our length is in microseconds, but the player wants milliseconds */
465 amidiplug_ip.set_info( G_PATH_GET_BASENAME(filename) , 467 amidiplug_ip.set_info( G_PATH_GET_BASENAME(filename_uri) ,
466 (gint)(midifile.length / 1000) , 468 (gint)(midifile.length / 1000) ,
467 au_bitdepth * au_samplerate * au_channels / 8 , 469 au_bitdepth * au_samplerate * au_channels / 8 ,
468 au_samplerate , au_channels ); 470 au_samplerate , au_channels );
469 471
470 /* play play play! */ 472 /* play play play! */
475 } 477 }
476 478
477 default: 479 default:
478 { 480 {
479 amidiplug_playing_status = AMIDIPLUG_ERR; 481 amidiplug_playing_status = AMIDIPLUG_ERR;
480 g_warning( "%s is not a Standard MIDI File\n" , filename ); 482 g_warning( "%s is not a Standard MIDI File\n" , filename_uri );
481 break; 483 break;
482 } 484 }
483 } 485 }
484 486
485 VFS_FCLOSE( midifile.file_pointer ); 487 VFS_FCLOSE( midifile.file_pointer );