wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Content / AudioLyricsUtil.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include "AudioLyricsUtil.h"
19 #include <dpl/singleton_impl.h>
20 #include <metadata_extractor.h>
21 #include <Logger.h>
22
23 using namespace DPL;
24  
25 IMPLEMENT_SINGLETON(DeviceAPI::Content::MediaLyricsUtil)
26
27 namespace DeviceAPI {
28 namespace Content {
29
30 metadata_extractor_h m_extractor = NULL;
31 MediaLyricsUtil::MediaLyricsUtil()
32 {
33          LoggerD("entered ");
34         if ( METADATA_EXTRACTOR_ERROR_NONE != metadata_extractor_create(&m_extractor))
35         {
36                 LoggerD("MetaData Extractor create Fail...");
37         }
38 }
39
40 MediaLyricsUtil::~MediaLyricsUtil()
41 {
42         LoggerD("entered ");
43         if ( m_extractor && METADATA_EXTRACTOR_ERROR_NONE != metadata_extractor_destroy(m_extractor))
44         {
45                 LoggerD("Metadata extractor destory Fail...");
46         }
47 }
48
49 MediacontentLyricsPtr MediaLyricsUtil::fetchLyrics(string filepath)
50 {
51         MediacontentLyricsPtr result;
52         //MediacontentLyricsPtr lyricsPtr(new MediacontentLyrics());
53         //set path.
54
55         if (m_extractor)
56         {
57                 LoggerD("Valide Extractor");
58
59                 //setpath
60                 LoggerD("set Path : " << filepath);
61
62                 if ( METADATA_EXTRACTOR_ERROR_NONE == metadata_extractor_set_path(m_extractor, filepath.c_str()) )
63                 {                               
64                         char* strSyncTextNum=NULL;
65                         metadata_extractor_attr_e attr = METADATA_SYNCLYRICS_NUM;
66                         if ( METADATA_EXTRACTOR_ERROR_NONE == metadata_extractor_get_metadata(m_extractor, attr, &strSyncTextNum) 
67                                 && strSyncTextNum )
68                         {
69                                 LoggerD("SYNCHRONIZED");                        
70                                 int nSyncTextNum = 0;
71                                 istringstream(strSyncTextNum) >> nSyncTextNum;
72                                 LoggerD("nSyncTextNum : " << nSyncTextNum);
73
74                                 free(strSyncTextNum);
75                                 strSyncTextNum = NULL;
76                                 
77                                 if ( nSyncTextNum > 0)
78                                 {
79
80                                         MediacontentLyricsPtr lyricsPtr(new MediacontentLyrics());
81                                         lyricsPtr->setMediaLyricsType("SYNCHRONIZED");
82                                         for(int i=0; i < nSyncTextNum; i++)
83                                         {       
84                                                 unsigned long time_info = 0;
85                                                 char * lyrics = NULL;
86                                                 if ( METADATA_EXTRACTOR_ERROR_NONE == 
87                                                         metadata_extractor_get_synclyrics(m_extractor, i, &time_info, &lyrics))
88                                                 {
89                                                         lyricsPtr->addMediaLyricsTimeStamp(time_info);
90                                                         lyricsPtr->addMediaLyricsText(lyrics);
91                                                         LoggerD( "timestamp " << time_info << "lyrics : " << lyrics);
92                                                 }
93                                         }
94
95                                         result = lyricsPtr;
96                                 }
97                                 else
98                                 {       
99                                         LoggerD("sync Lyrics Error. text num is 0");
100
101                                         char* unSyncText = NULL;
102                                         attr = METADATA_UNSYNCLYRICS;
103
104                                         if ( METADATA_EXTRACTOR_ERROR_NONE == metadata_extractor_get_metadata(m_extractor, attr, &unSyncText) )
105                                         {
106                                                 if (unSyncText)
107                                                 {
108                                                         MediacontentLyricsPtr lyricsPtr(new MediacontentLyrics());
109                                                         LoggerD("UNSYNCHRONIZED: " << unSyncText);
110                                                         lyricsPtr->setMediaLyricsType("UNSYNCHRONIZED");
111                                                         lyricsPtr->addMediaLyricsTimeStamp(0);
112                                                         lyricsPtr->addMediaLyricsText(unSyncText);
113                                                         
114                                                         result = lyricsPtr;
115                                                         
116                                                         free(unSyncText);
117                                                         unSyncText = NULL;
118                                                 }
119                                         }
120                                         
121                                 }
122                                 
123                         }
124                         else
125                         {       
126                                 LoggerD("extractor Error!!!");
127                         }
128
129                 }
130
131         }
132         else
133         {
134                 LoggerD(" extractor is invaild ");
135         }
136         
137         return result;
138 }
139
140 }
141 }
142
143   
144
145
146