note description: "An AUDIO_SOUND that has been loaded from an MPG file" author: "Louis Marchand" date: "Wed, 22 Feb 2017 03:42:57 +0000" revision: "0.3" class MPG_SOUND_FILE create make feature {NONE} -- Initialization make (a_filename: READABLE_STRING_GENERAL) -- Initialization for Current using a_filename as filename require is_mpg_enabled: Mpg_library.is_mpg_enabled local l_pointer: POINTER do create {LINKED_LIST [READABLE_STRING_GENERAL]} titles.make create {LINKED_LIST [READABLE_STRING_GENERAL]} albums.make create {LINKED_LIST [READABLE_STRING_GENERAL]} artists.make create {LINKED_LIST [READABLE_STRING_GENERAL]} years.make create {LINKED_LIST [READABLE_STRING_GENERAL]} comments.make create {LINKED_LIST [READABLE_STRING_GENERAL]} genres.make file_ptr := {MPG_EXTERNAL}.mpg123_new (l_pointer, l_pointer) default_create filename := a_filename is_finished := False ensure handler_valid: not file_ptr.is_default_pointer end feature -- Access generating_type: TYPE [detachable MPG_SOUND_FILE] -- Type of current object -- (type of which it is a direct instance) -- (from ANY) external "built_in" ensure -- from ANY generating_type_not_void: Result /= Void end generator: STRING_8 -- Name of current object's generating class -- (base class of the type of which it is a direct instance) -- (from ANY) external "built_in" ensure -- from ANY generator_not_void: Result /= Void generator_not_empty: not Result.is_empty end has_error: BOOLEAN -- Is the library has generate an error -- (from GAME_ERROR_MANAGER) has_ressource_error: BOOLEAN -- Has an error occured while opening Current -- (from GAME_RESSOURCE) is_open: BOOLEAN -- Is Current has been properly opened -- (from GAME_RESSOURCE) last_error: READABLE_STRING_GENERAL -- The last error generate by the library -- (from GAME_ERROR_MANAGER) do if attached message as la_message then Result := la_message else Result := "" end end Mpg_library: MPG_LIBRARY_CONTROLLER -- Access to the MPG library internal fonctionnality -- (from MPG_LIBRARY_SHARED) once ("PROCESS") create Result if attached internal_mpg_library as la_mpg_library then Result := la_mpg_library else create Result end end feature --Access album: detachable READABLE_STRING_GENERAL -- The media that Current is from -- Void if no 'album' available in Current require -- from METADATA_AUDIO_SOUND True require else is_scanned: is_scanned do if not albums.is_empty then Result := albums.first end end albums: LIST [READABLE_STRING_GENERAL] -- The names of the medias that Current is from artist: detachable READABLE_STRING_GENERAL -- The name of the creator of Current -- Void if no 'artist' available in Current require -- from METADATA_AUDIO_SOUND True require else is_scanned: is_scanned do if not artists.is_empty then Result := artists.first end end artists: LIST [READABLE_STRING_GENERAL] -- The name of every creators of Current bits_per_sample: INTEGER_32 -- Get the bit resolution of one frame of Current. require -- from AUDIO_SOUND sound_is_open: is_open do Result := byte_per_buffer_sample * 8 end channel_count: INTEGER_32 -- Get the channel number of Current (1=mono, 2=stereo, etc.). require -- from AUDIO_SOUND sound_is_open: is_open do Result := channel_count_internal end close -- Stop the management of the stream require -- from AUDIO_SOUND sound_is_open: is_open local l_error: INTEGER_32 do l_error := {MPG_EXTERNAL}.mpg123_close (file_ptr) is_open := False ensure -- from AUDIO_SOUND sound_is_closed: not is_open end comment: detachable READABLE_STRING_GENERAL -- Anything that can be add to the definition of Current -- Void if no 'comment' available in Current require -- from METADATA_AUDIO_SOUND True require else is_scanned: is_scanned do if not comments.is_empty then Result := comments.first end end comments: LIST [READABLE_STRING_GENERAL] -- Everything that can be add to the definition of Current frequency: INTEGER_32 -- Get the frequency (sample rate) of Current. require -- from AUDIO_SOUND sound_is_open: is_open do Result := frequency_internal end genre: detachable READABLE_STRING_GENERAL -- The conventional category of Current -- Void if no 'genre' available in Current require -- from METADATA_AUDIO_SOUND True require else is_scanned: is_scanned do if not genres.is_empty then Result := genres.first end end genres: LIST [READABLE_STRING_GENERAL] -- Every conventional categories of Current is_finished: BOOLEAN -- Current has finished it's playback -- (from AUDIO_SOUND) is_openable: BOOLEAN -- Can Current be open local l_file: RAW_FILE do create l_file.make_with_name (filename) Result := l_file.exists and l_file.is_readable end is_scanned: BOOLEAN -- Is scan has been used on Current is_seekable: BOOLEAN -- Return true if Current support the seek functionnality. require -- from AUDIO_SOUND sound_is_open: is_open do Result := True end is_signed: BOOLEAN -- True if the frames in the buffer are signed. require -- from AUDIO_SOUND sound_is_open: is_open do Result := resolution_index = {MPG_EXTERNAL}.mpg123_enc_signed_16 end open -- Open Current require -- from GAME_RESSOURCE ressource_is_openable: is_openable ressource_is_not_open: not is_open and not has_ressource_error do open_from_file (filename) is_open := not has_error has_ressource_error := has_error ensure -- from GAME_RESSOURCE ressource_is_open: is_open or has_ressource_error end restart -- Restart Current to the beginning. require -- from AUDIO_SOUND sound_is_open: is_open local l_error: INTEGER_32 do l_error := {MPG_EXTERNAL}.mpg123_seek (file_ptr, 0, {MPG_EXTERNAL}.seek_set) is_finished := False if l_error < 0 then read_mpg_error ("Cannot restart playback", l_error) end end sample_count: INTEGER_64 -- The total number of frames in Current require -- from AUDIO_SOUND sound_is_open: is_open is_seekable: is_seekable local l_error: INTEGER_32 do l_error := {MPG_EXTERNAL}.mpg123_length (file_ptr) if l_error < 0 then read_mpg_error ("Cannot seek in file", l_error) Result := 0 else Result := l_error.to_integer_64 end end sample_position: INTEGER_64 -- The number of frames since the beginning of Current require -- from AUDIO_SOUND sound_is_open: is_open is_seekable: is_seekable local l_error: INTEGER_32 do l_error := {MPG_EXTERNAL}.mpg123_tell (file_ptr) if l_error = {MPG_EXTERNAL}.mpg123_err then read_mpg_error ("Cannot get the sample position", l_error) end Result := l_error + 1.to_integer_64 end sample_seek (a_frame_number: INTEGER_64) -- Seek at the frame a_frame_number from the beginning of Current require -- from AUDIO_SOUND sound_is_open: is_open is_seekable: is_seekable is_seek_inside: a_frame_number >= 1 and a_frame_number <= sample_count local l_error: INTEGER_32 do l_error := {MPG_EXTERNAL}.mpg123_seek (file_ptr, (a_frame_number).to_integer_32 - 1, {MPG_EXTERNAL}.seek_set) is_finished := False if l_error < 0 then read_mpg_error ("Cannot seek in file", l_error) end end scan -- Full parsing scan of each frame in the file. -- Find Metadatas and optimize seek require open: is_open seakable: is_seekable local l_error: INTEGER_32 do l_error := {MPG_EXTERNAL}.mpg123_scan (file_ptr) if l_error /= {MPG_EXTERNAL}.mpg123_ok then read_mpg_error ("Cannot scan MPG file", l_error) else is_scanned := True l_error := {MPG_EXTERNAL}.mpg123_meta_check (file_ptr) if l_error.bit_and ({MPG_EXTERNAL}.mpg123_id3_constant) /= 0 then manage_id3 end end end time_count: INTEGER_64 -- The total number of milliseconds in Current -- (from AUDIO_SOUND) require -- from AUDIO_SOUND sound_is_open: is_open is_seekable: is_seekable do Result := (sample_count * 1000) // frequency.to_integer_64 end time_position: INTEGER_64 -- The number of milliseconds since the beginning of Current -- (from AUDIO_SOUND) require -- from AUDIO_SOUND sound_is_open: is_open is_seekable: is_seekable do Result := ((sample_position - 1) * 1000) // frequency.to_integer_64 end time_seek (a_milliseconds: INTEGER_64) -- Seek at a_milliseconds from the beginning of Current -- (from AUDIO_SOUND) require -- from AUDIO_SOUND sound_is_open: is_open is_seekable: is_seekable is_seek_inside: a_milliseconds >= 1 and a_milliseconds <= time_count do sample_seek (((a_milliseconds * frequency.to_integer_64) // 1000) + 1) end title: detachable READABLE_STRING_GENERAL -- The readable identifier of Current -- Void if no 'title' available in Current require -- from METADATA_AUDIO_SOUND True require else is_scanned: is_scanned do if not titles.is_empty then Result := titles.first end end titles: LIST [READABLE_STRING_GENERAL] -- Every readable identifier of Current track_number: INTEGER_32 -- The number of Current in the album -- 0 if no 'track_number' available in Current year: detachable READABLE_STRING_GENERAL -- The creation's date of Current -- Void if no 'date' available in Current require -- from METADATA_AUDIO_SOUND True require else is_scanned: is_scanned do if not years.is_empty then Result := years.first end end years: LIST [READABLE_STRING_GENERAL] -- The creation's years of Current feature -- Comparison frozen deep_equal (a: detachable ANY; b: like arg #1): BOOLEAN -- Are a and b either both void -- or attached to isomorphic object structures? -- (from ANY) do if a = Void then Result := b = Void else Result := b /= Void and then a.is_deep_equal (b) end ensure -- from ANY instance_free: class shallow_implies_deep: standard_equal (a, b) implies Result both_or_none_void: (a = Void) implies (Result = (b = Void)) same_type: (Result and (a /= Void)) implies (b /= Void and then a.same_type (b)) symmetric: Result implies deep_equal (b, a) end frozen equal (a: detachable ANY; b: like arg #1): BOOLEAN -- Are a and b either both void or attached -- to objects considered equal? -- (from ANY) do if a = Void then Result := b = Void else Result := b /= Void and then a.is_equal (b) end ensure -- from ANY instance_free: class definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.is_equal (b)) end frozen is_deep_equal alias "≡≡≡" (other: MPG_SOUND_FILE): BOOLEAN -- Are Current and other attached to isomorphic object structures? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" ensure -- from ANY shallow_implies_deep: standard_is_equal (other) implies Result same_type: Result implies same_type (other) symmetric: Result implies other.is_deep_equal (Current) end is_equal (other: MPG_SOUND_FILE): BOOLEAN -- Is other attached to an object considered -- equal to current object? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" ensure -- from ANY symmetric: Result implies other ~ Current consistent: standard_is_equal (other) implies Result end frozen standard_equal (a: detachable ANY; b: like arg #1): BOOLEAN -- Are a and b either both void or attached to -- field-by-field identical objects of the same type? -- Always uses default object comparison criterion. -- (from ANY) do if a = Void then Result := b = Void else Result := b /= Void and then a.standard_is_equal (b) end ensure -- from ANY instance_free: class definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.standard_is_equal (b)) end frozen standard_is_equal alias "≜" (other: MPG_SOUND_FILE): BOOLEAN -- Is other attached to an object of the same type -- as current object, and field-by-field identical to it? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" ensure -- from ANY same_type: Result implies same_type (other) symmetric: Result implies other.standard_is_equal (Current) end feature -- Status report conforms_to (other: ANY): BOOLEAN -- Does type of current object conform to type -- of other (as per Eiffel: The Language, chapter 13)? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" end same_type (other: ANY): BOOLEAN -- Is type of current object identical to type of other? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" ensure -- from ANY definition: Result = (conforms_to (other) and other.conforms_to (Current)) end feature {NONE} -- Status report is_in_final_collect: BOOLEAN -- Is GC currently performing final collection -- after execution of current program? -- Safe to use in dispose. -- (from DISPOSABLE) external "C inline use %"eif_memory.h%"" alias "return eif_is_in_final_collect;" end feature -- Duplication frozen clone (other: detachable ANY): like other obsolete "Use `twin' instead. [2017-05-31]" -- Void if other is void; otherwise new object -- equal to other -- -- For non-void other, clone calls copy; -- to change copying/cloning semantics, redefine copy. -- (from ANY) do if other /= Void then Result := other.twin end ensure -- from ANY instance_free: class equal: Result ~ other end copy (other: MPG_SOUND_FILE) -- Update current object using fields of object attached -- to other, so as to yield equal objects. -- (from ANY) require -- from ANY other_not_void: other /= Void type_identity: same_type (other) external "built_in" ensure -- from ANY is_equal: Current ~ other end frozen deep_clone (other: detachable ANY): like other obsolete "Use `deep_twin' instead. [2017-05-31]" -- Void if other is void: otherwise, new object structure -- recursively duplicated from the one attached to other -- (from ANY) do if other /= Void then Result := other.deep_twin end ensure -- from ANY instance_free: class deep_equal: deep_equal (other, Result) end frozen deep_copy (other: MPG_SOUND_FILE) -- Effect equivalent to that of: -- copy (other . deep_twin) -- (from ANY) require -- from ANY other_not_void: other /= Void do copy (other.deep_twin) ensure -- from ANY deep_equal: deep_equal (Current, other) end frozen deep_twin: MPG_SOUND_FILE -- New object structure recursively duplicated from Current. -- (from ANY) external "built_in" ensure -- from ANY deep_twin_not_void: Result /= Void deep_equal: deep_equal (Current, Result) end frozen standard_clone (other: detachable ANY): like other obsolete "Use `standard_twin' instead. [2017-05-31]" -- Void if other is void; otherwise new object -- field-by-field identical to other. -- Always uses default copying semantics. -- (from ANY) do if other /= Void then Result := other.standard_twin end ensure -- from ANY instance_free: class equal: standard_equal (Result, other) end frozen standard_copy (other: MPG_SOUND_FILE) -- Copy every field of other onto corresponding field -- of current object. -- (from ANY) require -- from ANY other_not_void: other /= Void type_identity: same_type (other) external "built_in" ensure -- from ANY is_standard_equal: standard_is_equal (other) end frozen standard_twin: MPG_SOUND_FILE -- New object field-by-field identical to other. -- Always uses default copying semantics. -- (from ANY) external "built_in" ensure -- from ANY standard_twin_not_void: Result /= Void equal: standard_equal (Result, Current) end frozen twin: MPG_SOUND_FILE -- New object equal to Current -- twin calls copy; to change copying/twinning semantics, redefine copy. -- (from ANY) external "built_in" ensure -- from ANY twin_not_void: Result /= Void is_equal: Result ~ Current end feature -- Basic operations frozen as_attached: attached MPG_SOUND_FILE obsolete "Remove calls to this feature. [2017-05-31]" -- Attached version of Current. -- (Can be used during transitional period to convert -- non-void-safe classes to void-safe ones.) -- (from ANY) do Result := Current end frozen default: detachable MPG_SOUND_FILE -- Default value of object's type -- (from ANY) do end frozen default_pointer: POINTER -- Default value of type POINTER -- (Avoid the need to write p.default for -- some p of type POINTER.) -- (from ANY) do ensure -- from ANY instance_free: class end default_rescue -- Process exception for routines with no Rescue clause. -- (Default: do nothing.) -- (from ANY) do end frozen do_nothing -- Execute a null action. -- (from ANY) do ensure -- from ANY instance_free: class end feature {NONE} -- Implementation clear_error -- Remove error pending in Current -- (from GAME_ERROR_MANAGER) do has_error := False ensure -- from GAME_ERROR_MANAGER no_error: not has_error end disable_print_on_error -- Desactive the print_on_error functionnality. -- (from GAME_ERROR_MANAGER) do Print_on_error_internal.put (False) end enable_print_on_error -- Active the print_on_error functionnality. -- (from GAME_ERROR_MANAGER) do Print_on_error_internal.put (True) end get_id3_v1_string (a_pointer: POINTER; a_size: INTEGER_32): READABLE_STRING_GENERAL -- Extract the (not necessary null terminated) C string pointed by a_pointer of lenght a_size require pointer_not_null: not a_pointer.is_default_pointer local l_managed_pointer: MANAGED_POINTER l_c_string: C_STRING do create l_managed_pointer.make (a_size + 1); l_managed_pointer.item.memory_copy (a_pointer, a_size); l_managed_pointer.put_natural_8 (0, a_size) create l_c_string.make_shared_from_pointer (l_managed_pointer.item) Result := l_c_string.string end id3_v1_genre_string (a_number: INTEGER_32): detachable READABLE_STRING_GENERAL -- Map the ID3v1 genre ID a_number to it's text representation do if a_number = 0 then Result := "Blues" elseif a_number = 1 then Result := "Classic Rock" elseif a_number = 2 then Result := "Country" elseif a_number = 3 then Result := "Dance" elseif a_number = 4 then Result := "Disco" elseif a_number = 5 then Result := "Funk" elseif a_number = 6 then Result := "Grunge" elseif a_number = 7 then Result := "Hip-Hop" elseif a_number = 8 then Result := "Jazz" elseif a_number = 9 then Result := "Metal" elseif a_number = 10 then Result := "New Age" elseif a_number = 11 then Result := "Oldies" elseif a_number = 12 then Result := "Other" elseif a_number = 13 then Result := "Pop" elseif a_number = 14 then Result := "R&B" elseif a_number = 15 then Result := "Rap" elseif a_number = 16 then Result := "Reggae" elseif a_number = 17 then Result := "Rock" elseif a_number = 18 then Result := "Techno" elseif a_number = 19 then Result := "Industrial" elseif a_number = 20 then Result := "Alternative" elseif a_number = 21 then Result := "Ska" elseif a_number = 22 then Result := "Death Metal" elseif a_number = 23 then Result := "Pranks" elseif a_number = 24 then Result := "Soundtrack" elseif a_number = 25 then Result := "Euro-Techno" elseif a_number = 26 then Result := "Ambient" elseif a_number = 27 then Result := "Trip-Hop" elseif a_number = 28 then Result := "Vocal" elseif a_number = 29 then Result := "Jazz+Funk" elseif a_number = 30 then Result := "Fusion" elseif a_number = 31 then Result := "Trance" elseif a_number = 32 then Result := "Classical" elseif a_number = 33 then Result := "Instrumental" elseif a_number = 34 then Result := "Acid" elseif a_number = 35 then Result := "House" elseif a_number = 36 then Result := "Game" elseif a_number = 37 then Result := "Sound Clip" elseif a_number = 38 then Result := "Gospel" elseif a_number = 39 then Result := "Noise" elseif a_number = 40 then Result := "Alternative Rock" elseif a_number = 41 then Result := "Bass" elseif a_number = 42 then Result := "Soul" elseif a_number = 43 then Result := "Punk" elseif a_number = 44 then Result := "Space" elseif a_number = 45 then Result := "Meditative" elseif a_number = 46 then Result := "Instrumental Pop" elseif a_number = 47 then Result := "Instrumental Rock" elseif a_number = 48 then Result := "Ethnic" elseif a_number = 49 then Result := "Gothic" elseif a_number = 50 then Result := "Darkwave" elseif a_number = 51 then Result := "Techno-Industrial" elseif a_number = 52 then Result := "Electronic" elseif a_number = 53 then Result := "Pop-Folk" elseif a_number = 54 then Result := "Eurodance" elseif a_number = 55 then Result := "Dream" elseif a_number = 56 then Result := "Southern Rock" elseif a_number = 57 then Result := "Comedy" elseif a_number = 58 then Result := "Cult" elseif a_number = 59 then Result := "Gangsta" elseif a_number = 60 then Result := "Top 40" elseif a_number = 61 then Result := "Christian Rap" elseif a_number = 62 then Result := "Pop/Funk" elseif a_number = 63 then Result := "Jungle" elseif a_number = 64 then Result := "Native US" elseif a_number = 65 then Result := "Cabaret" elseif a_number = 66 then Result := "New Wave" elseif a_number = 67 then Result := "Psychadelic" elseif a_number = 68 then Result := "Rave" elseif a_number = 69 then Result := "Showtunes" elseif a_number = 70 then Result := "Trailer" elseif a_number = 71 then Result := "Lo-Fi" elseif a_number = 72 then Result := "Tribal" elseif a_number = 73 then Result := "Acid Punk" elseif a_number = 74 then Result := "Acid Jazz" elseif a_number = 75 then Result := "Polka" elseif a_number = 76 then Result := "Retro" elseif a_number = 77 then Result := "Musical" elseif a_number = 78 then Result := "Rock & Roll" elseif a_number = 79 then Result := "Hard Rock" elseif a_number = 80 then Result := "Folk" elseif a_number = 81 then Result := "Folk-Rock" elseif a_number = 82 then Result := "National Folk" elseif a_number = 83 then Result := "Swing" elseif a_number = 84 then Result := "Fast Fusion" elseif a_number = 85 then Result := "Bebob" elseif a_number = 86 then Result := "Latin" elseif a_number = 87 then Result := "Revival" elseif a_number = 88 then Result := "Celtic" elseif a_number = 89 then Result := "Bluegrass" elseif a_number = 90 then Result := "Avantgarde" elseif a_number = 91 then Result := "Gothic Rock" elseif a_number = 92 then Result := "Progressive Rock" elseif a_number = 93 then Result := "Psychedelic Rock" elseif a_number = 94 then Result := "Symphonic Rock" elseif a_number = 95 then Result := "Slow Rock" elseif a_number = 96 then Result := "Big Band" elseif a_number = 97 then Result := "Chorus" elseif a_number = 98 then Result := "Easy Listening" elseif a_number = 99 then Result := "Acoustic" elseif a_number = 100 then Result := "Humour" elseif a_number = 101 then Result := "Speech" elseif a_number = 102 then Result := "Chanson" elseif a_number = 103 then Result := "Opera" elseif a_number = 104 then Result := "Chamber Music" elseif a_number = 105 then Result := "Sonata" elseif a_number = 106 then Result := "Symphony" elseif a_number = 107 then Result := "Booty Bass" elseif a_number = 108 then Result := "Primus" elseif a_number = 109 then Result := "Porn Groove" elseif a_number = 110 then Result := "Satire" elseif a_number = 111 then Result := "Slow Jam" elseif a_number = 112 then Result := "Club" elseif a_number = 113 then Result := "Tango" elseif a_number = 114 then Result := "Samba" elseif a_number = 115 then Result := "Folklore" elseif a_number = 116 then Result := "Ballad" elseif a_number = 117 then Result := "Power Ballad" elseif a_number = 118 then Result := "Rhytmic Soul" elseif a_number = 119 then Result := "Freestyle" elseif a_number = 120 then Result := "Duet" elseif a_number = 121 then Result := "Punk Rock" elseif a_number = 122 then Result := "Drum Solo" elseif a_number = 123 then Result := "Acapella" elseif a_number = 124 then Result := "Euro-House" elseif a_number = 125 then Result := "Dance Hall" elseif a_number = 126 then Result := "Goa" elseif a_number = 127 then Result := "Drum & Bass" elseif a_number = 128 then Result := "Club-House" elseif a_number = 129 then Result := "Hardcore" elseif a_number = 130 then Result := "Terror" elseif a_number = 131 then Result := "Indie" elseif a_number = 132 then Result := "BritPop" elseif a_number = 133 then Result := "Negerpunk" elseif a_number = 134 then Result := "Polsk Punk" elseif a_number = 135 then Result := "Beat" elseif a_number = 136 then Result := "Christian Gangsta" elseif a_number = 137 then Result := "Heavy Metal" elseif a_number = 138 then Result := "Black Metal" elseif a_number = 139 then Result := "Crossover" elseif a_number = 140 then Result := "Contemporary C" elseif a_number = 141 then Result := "Christian Rock" elseif a_number = 142 then Result := "Merengue" elseif a_number = 143 then Result := "Salsa" elseif a_number = 144 then Result := "Thrash Metal" elseif a_number = 145 then Result := "Anime" elseif a_number = 146 then Result := "JPop" elseif a_number = 147 then Result := "SynthPop" end end internal_mpg_library: detachable MPG_LIBRARY_CONTROLLER -- Assign to this attribute prior to use Mpg_library to inject a specific MPG_LIBRARY_CONTROLLER singleton. -- (from MPG_LIBRARY_SHARED) manage_id3 -- Get every ID3 metadatas from Current require is_open: is_open is_scanned: is_scanned local l_v1, l_v2: POINTER l_error: INTEGER_32 do l_error := {MPG_EXTERNAL}.mpg123_id3 (file_ptr, $l_v1.to_pointer, $l_v2.to_pointer) if l_error /= {MPG_EXTERNAL}.mpg123_ok then read_mpg_error ("Cannot retreive ID3 metadata.", l_error) else track_number := -1 if not l_v2.is_default_pointer then manage_id3_v2 (l_v2) end if not l_v1.is_default_pointer then manage_id3_v1 (l_v1) end if track_number = -1 then track_number := 0 end end end manage_id3_v1 (a_pointer: POINTER) -- Get every ID3v1 metadatas from Current require pointer_not_null: not a_pointer.is_default_pointer local l_pointer: POINTER do if titles.is_empty then l_pointer := {MPG_EXTERNAL}.get_mpg123_id3v1_struct_title (a_pointer) if not l_pointer.is_default_pointer then titles.extend (get_id3_v1_string (l_pointer, {MPG_EXTERNAL}.sizeof_mpg123_id3v1_title)) end end if artists.is_empty then l_pointer := {MPG_EXTERNAL}.get_mpg123_id3v1_struct_artist (a_pointer) if not l_pointer.is_default_pointer then artists.extend (get_id3_v1_string (l_pointer, {MPG_EXTERNAL}.sizeof_mpg123_id3v1_artist)) end end if albums.is_empty then l_pointer := {MPG_EXTERNAL}.get_mpg123_id3v1_struct_album (a_pointer) if not l_pointer.is_default_pointer then albums.extend (get_id3_v1_string (l_pointer, {MPG_EXTERNAL}.sizeof_mpg123_id3v1_album)) end end if years.is_empty then l_pointer := {MPG_EXTERNAL}.get_mpg123_id3v1_struct_year (a_pointer) if not l_pointer.is_default_pointer then years.extend (get_id3_v1_string (l_pointer, {MPG_EXTERNAL}.sizeof_mpg123_id3v1_year)) end end set_comments_from_id3_v1 (a_pointer) if genres.is_empty and attached id3_v1_genre_string ({MPG_EXTERNAL}.get_mpg123_id3v1_struct_genre (a_pointer).to_integer_32) as la_genre then genres.extend (la_genre) end end manage_id3_v2 (a_pointer: POINTER) -- Get every ID3v2 metadatas from Current require pointer_not_null: not a_pointer.is_default_pointer do manage_id3_v2_strings ({MPG_EXTERNAL}.get_mpg123_id3v2_struct_title (a_pointer), titles) manage_id3_v2_strings ({MPG_EXTERNAL}.get_mpg123_id3v2_struct_artist (a_pointer), artists) manage_id3_v2_strings ({MPG_EXTERNAL}.get_mpg123_id3v2_struct_album (a_pointer), albums) manage_id3_v2_strings ({MPG_EXTERNAL}.get_mpg123_id3v2_struct_year (a_pointer), years) manage_id3_v2_strings ({MPG_EXTERNAL}.get_mpg123_id3v2_struct_genre (a_pointer), genres) manage_id3_v2_strings ({MPG_EXTERNAL}.get_mpg123_id3v2_struct_comment (a_pointer), comments) end manage_id3_v2_string (a_string: STRING_32; a_list: LIST [READABLE_STRING_GENERAL]) -- Put every individual lines of a_string in a_list local l_n_split, l_r_split: LIST [STRING_32] do l_n_split := a_string.split ({CHARACTER_32}'%N') across l_n_split as la_n_split loop l_r_split := la_n_split.item.split ({CHARACTER_32}'%R') across l_r_split as la_r_split loop if not la_r_split.item.is_empty then a_list.extend (la_r_split.item) end end end end manage_id3_v2_strings (a_pointer: POINTER; a_list: LIST [READABLE_STRING_GENERAL]) -- Extract every C string pointed by a_pointer and put then in a_list local l_string_start: BOOLEAN l_managed_pointer: MANAGED_POINTER l_count, l_index: INTEGER_32 l_utf_converter: UTF_CONVERTER do if not a_pointer.is_default_pointer then create l_utf_converter create l_managed_pointer.share_from_pointer ({MPG_EXTERNAL}.get_mpg123_string_struct_p (a_pointer), {MPG_EXTERNAL}.get_mpg123_string_struct_fill (a_pointer)) from l_index := 0 l_string_start := False until l_index >= l_managed_pointer.count loop if not l_string_start and l_managed_pointer.read_natural_8 (l_index) /= 0 then manage_id3_v2_string (l_utf_converter.utf_8_0_subpointer_to_escaped_string_32 (l_managed_pointer, l_index, l_managed_pointer.count - 1, True), a_list) l_string_start := True end if l_managed_pointer.read_natural_8 (l_index) = 0 then l_string_start := False end l_index := l_index + 1 end end end message: detachable READABLE_STRING_GENERAL -- The specific message for the last error -- (from GAME_ERROR_MANAGER) open_from_file (a_filename: READABLE_STRING_GENERAL) -- open Current using a_filename local l_filename_c: C_STRING l_error: INTEGER_32 l_converter: UTF_CONVERTER do create l_converter create l_filename_c.make (l_converter.string_32_to_utf_8_string_8 (filename.to_string_32)) l_error := {MPG_EXTERNAL}.mpg123_open (file_ptr, l_filename_c.item) if l_error /= {MPG_EXTERNAL}.mpg123_ok then read_mpg_error ({STRING_32}"Cannot open file " + a_filename.as_string_32, l_error) else l_error := {MPG_EXTERNAL}.mpg123_getformat (file_ptr, $frequency_internal.to_pointer, $channel_count_internal.to_pointer, $resolution_index.to_pointer) if l_error /= {MPG_EXTERNAL}.mpg123_ok then read_mpg_error ({STRING_32}"Cannot get audio format from " + a_filename.as_string_32, l_error) else l_error := {MPG_EXTERNAL}.mpg123_format_none (file_ptr) if l_error /= {MPG_EXTERNAL}.mpg123_ok then read_mpg_error ({STRING_32}"Cannot get audio format from " + a_filename.as_string_32, l_error) else l_error := {MPG_EXTERNAL}.mpg123_format (file_ptr, frequency_internal, channel_count_internal, resolution_index) read_mpg_error ({STRING_32}"Cannot get audio format from " + a_filename.as_string_32, l_error) end end end end print_on_error: BOOLEAN -- When an error occured, the library will print -- informations about the error on the error console -- output (default is True). -- (from GAME_ERROR_MANAGER) do Result := Print_on_error_internal.item end Print_on_error_internal: CELL [BOOLEAN] -- True when an error occured, -- The library will print it right away. -- (from GAME_ERROR_MANAGER) once ("PROCESS") create Result.put (True) end put_error (a_general_message, a_specific_error: READABLE_STRING_GENERAL) -- Create an error using a_general_error for the debug information -- and a_specific_error for the lasting information -- (from GAME_ERROR_MANAGER) local l_converter: UTF_CONVERTER do message := a_specific_error has_error := True if Print_on_error_internal.item then if a_general_message.is_valid_as_string_8 then Io.Error.put_string (a_general_message.to_string_8 + "%N") else create l_converter; Io.Error.put_string (l_converter.string_32_to_utf_8_string_8 (a_general_message.to_string_32) + "%N") end if a_specific_error.is_valid_as_string_8 then Io.Error.put_string (a_specific_error.to_string_8 + "%N") else create l_converter; Io.Error.put_string (l_converter.string_32_to_utf_8_string_8 (a_specific_error.to_string_32) + "%N") end end ensure -- from GAME_ERROR_MANAGER has_error end read_mpg_error (a_message: READABLE_STRING_GENERAL; a_code: INTEGER_32) -- Read an error message from the internal library using the error code a_code -- and using a general error message a_message do Precursor (a_message, a_code) has_ressource_error := has_error end set_comments_from_id3_v1 (a_pointer: POINTER) -- Extract the ID3v1 comment (including track_number) from Current require pointer_not_null: not a_pointer.is_default_pointer local l_pointer: POINTER l_managed_pointer: MANAGED_POINTER l_count: INTEGER_32 do l_count := {MPG_EXTERNAL}.sizeof_mpg123_id3v1_comment l_pointer := {MPG_EXTERNAL}.get_mpg123_id3v1_struct_comment (a_pointer) if not l_pointer.is_default_pointer then create l_managed_pointer.share_from_pointer (l_pointer, l_count) if l_managed_pointer.read_natural_8 (l_count - 2) = 0 then if track_number = -1 then track_number := l_managed_pointer.read_natural_8 (l_count - 1).to_integer_32 end l_count := l_count - 2 end if comments.is_empty then comments.extend (get_id3_v1_string (l_managed_pointer.item, l_count)) end end end set_print_on_error (a_value: BOOLEAN) -- Assign to print_on_error the value of a_value -- (from GAME_ERROR_MANAGER) do if a_value then enable_print_on_error else disable_print_on_error end ensure -- from GAME_ERROR_MANAGER is_assign: print_on_error ~ a_value end feature -- Implementation dispose -- Action to be executed just before garbage collection -- reclaims an object. -- Effect it in descendants to perform specific dispose -- actions. Those actions should only take care of freeing -- external resources; they should not perform remote calls -- on other objects since these may also be dead and reclaimed. local error: INTEGER_32 do if is_open then close end {MPG_EXTERNAL}.mpg123_delete (file_ptr) end feature {AUDIO_SOURCE}{AUDIO_SOURCE} byte_per_buffer_sample: INTEGER_32 -- The number of byte for one frame of Current. require -- from AUDIO_SOUND sound_is_open: is_open do if resolution_index = {MPG_EXTERNAL}.mpg123_enc_ulaw_8 then Result := 1 elseif resolution_index = {MPG_EXTERNAL}.mpg123_enc_signed_16 then Result := 2 end end fill_buffer (a_buffer: POINTER; a_max_length: INTEGER_32) -- Fill the next data samples in a_buffer (no more than a_max_length byte) -- The actual number of byte placed in a_buffer will be available in last_buffer_size -- Warning: side effect on a_buffer require -- from AUDIO_SOUND sound_is_open: is_open local l_size, l_error: INTEGER_32 do l_error := {MPG_EXTERNAL}.mpg123_read (file_ptr, a_buffer, a_max_length, $l_size.to_pointer) read_mpg_error ("Cannot read stream", l_error) last_buffer_size := l_size if last_buffer_size = 0 then is_finished := True end end feature {AUDIO_SOURCE} last_buffer_size: INTEGER_32 -- The size of the last buffer filled by fill_buffer -- (from AUDIO_SOUND) feature {NONE} -- Implementation - Variables channel_count_internal: INTEGER_32 -- The value of the channel_count attributes file_ptr: POINTER -- C pointer to the sound file handle filename: READABLE_STRING_GENERAL -- The name of the file containing the audio data frequency_internal: INTEGER_32 -- The value of the frequency attributes resolution_index: INTEGER_32 -- Index to differenciate between signed 16 bits samples and unsigned 8 bits samples. feature {NONE} -- Initialisation default_create -- Initialization of Current -- (from GAME_RESSOURCE) require -- from ANY True do is_open := False has_ressource_error := False end feature -- Output Io: STD_FILES -- Handle to standard file setup -- (from ANY) once create Result; Result.set_output_default ensure -- from ANY instance_free: class io_not_void: Result /= Void end out: STRING_8 -- New string containing terse printable representation -- of current object -- (from ANY) do Result := tagged_out ensure -- from ANY out_not_void: Result /= Void end print (o: detachable ANY) -- Write terse external representation of o -- on standard output. -- (from ANY) local s: READABLE_STRING_8 do if attached o then s := o.out if attached {READABLE_STRING_32} s as s32 then Io.put_string_32 (s32) elseif attached {READABLE_STRING_8} s as s8 then Io.put_string (s8) else Io.put_string_32 (s.as_string_32) end end ensure -- from ANY instance_free: class end frozen tagged_out: STRING_8 -- New string containing terse printable representation -- of current object -- (from ANY) external "built_in" ensure -- from ANY tagged_out_not_void: Result /= Void end feature -- Platform Operating_environment: OPERATING_ENVIRONMENT -- Objects available from the operating system -- (from ANY) once create Result ensure -- from ANY instance_free: class operating_environment_not_void: Result /= Void end feature {NONE} -- Retrieval frozen internal_correct_mismatch -- Called from runtime to perform a proper dynamic dispatch on correct_mismatch -- from MISMATCH_CORRECTOR. -- (from ANY) local l_msg: STRING_32 l_exc: EXCEPTIONS do if attached {MISMATCH_CORRECTOR} Current as l_corrector then l_corrector.correct_mismatch else create l_msg.make_from_string ("Mismatch: ".as_string_32) create l_exc; l_msg.append (generating_type.name_32); l_exc.raise_retrieval_exception (l_msg) end end invariant -- from AUDIO_SOUND errors_valid: has_error ~ has_ressource_error -- from ANY reflexive_equality: standard_is_equal (Current) reflexive_conformance: conforms_to (Current) end -- class MPG_SOUND_FILE
Generated by ISE EiffelStudio