taglib example

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
module Main where 
 
import qualified Audio.TagLib.TagLib as TagLib
import Data.Maybe
import qualified Data.ByteString as B

main = showFile "1.mp3"

withMaybe :: (Maybe j) -> (j -> IO ()) -> IO () 
withMaybe mebbe action = do
    case mebbe of 
        Just x -> do action x 
                     return ()
        Nothing -> return ()

showFile filename = do
    f <- B.readFile filename
    t <- TagLib.tagFileOpen f
    withMaybe t showTagFile

showTagFile :: TagLib.TagFile -> IO ()
showTagFile tagFile = do
    t <- TagLib.tagFileGetTag tagFile
    withMaybe t showTag
    p <- TagLib.tagFileGetAudioProperties tagFile
    withMaybe p showAudioProperties 

showTag :: TagLib.Tag -> IO ()
showTag tag = do 
    artist <- TagLib.tagGetArtist tag
    album <- TagLib.tagGetAlbum tag
    title <- TagLib.tagGetTitle tag
    comment <- TagLib.tagGetComment tag
    year <- TagLib.tagGetYear tag
    track <- TagLib.tagGetTrack tag
    print (artist, album, title, year, track)
 
showAudioProperties :: TagLib.AudioProperties -> IO ()
showAudioProperties props = do
    bitrate <- TagLib.audioPropertiesGetBitRate props
    length1 <- TagLib.audioPropertiesGetDuration props
    samplerate <- TagLib.audioPropertiesGetSampleRate props
    channels <- TagLib.audioPropertiesGetChannels props
    print (bitrate, length1, channels, samplerate)
9:14: Warning: Redundant bracket
Found:
(Maybe j) -> (j -> IO ()) -> IO ()
Why not:
Maybe j -> (j -> IO ()) -> IO ()
10:26: Error: Redundant do
Found:
do case mebbe of
Just x -> do action x
return ()
Nothing -> return ()
Why not:
case mebbe of
Just x -> do action x
return ()
Nothing -> return ()