歌词解析


#import <Foundation/Foundation.h>

@interface JYLrcItem : NSObject

@property (nonatomic) float time;
@property (nonatomic) NSString *lrc;

- (BOOL)isBiggerTimeThan:(JYLrcItem *)aItem;

@end
---------------------------------------------------------------------------

#import "JYLrcItem.h"

@implementation JYLrcItem

- (BOOL)isBiggerTimeThan:(JYLrcItem *)aItem
{
    return _time > aItem.time;
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"%.2f %@", _time,_lrc];
}

@end

---------------------------------------------------------------------------

#import <Foundation/Foundation.h>

@interface JYLrcParser : NSObject

// 歌词作者 [ar:歌词作者]
@property NSString *author;
// 歌词所在的唱片集 [al:这首歌所在的唱片集]
@property NSString *albume;
// 本lrc编辑作者 [by:本LRC文件的创建者]
@property NSString *byEditor;
// 歌曲标题 [ti:歌词(歌曲)的标题]
@property NSString *title;
// 歌曲版本 [ve:程序的版本]
@property NSString *version;

//把解析的歌词模型 对象地址放入数组中
//存放的是JYLrcItem类的对象地址
@property NSMutableArray *allLrcItems;

// 初始化这个lrc
- (id) initWithFile:(NSString *)file;
// 通过second取得当前的歌词
- (NSString *) getLrcByTime:(float)second;

//显示歌曲信息
- (void)showSongInfo;

- (void)showAllInfo;

@end

-----------------------------------------------------------------------------------

#import "JYLrcParser.h"
#import "JYLrcItem.h"

@implementation JYLrcParser

- (id)initWithFile:(NSString *)file
{
    if (self = [super init]) {
        _allLrcItems = [[NSMutableArray alloc] init];
        //调用方法解析歌词文件
        [self parseLrcWithFile:file];
    }
    return self;
}

- (void)parseLrcWithFile:(NSString *)file
{
    //1、读取歌词文件
    NSString *lrc = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];
    //2、按照'\n'进行切割
    NSArray *ary = [lrc componentsSeparatedByString:@"\n"];
    /*
     [ti:蓝莲花]
     [ar:许巍]
     [al:时光-漫步]
     [by:fanver]
     [offset:500]
     [00:00.20]蓝莲花
     
     [00:20.81]
     */
    //3、遍历数组,解析每行信息
    for (NSString *str in ary) {
        //过滤空行(哪个二哥随意敲回车)
        if ([str isEqualToString:@""]) {
            continue;
        }
        
        //获取第一个字符
        unichar ch = [str characterAtIndex:1];
        //判读该行是否为歌词信息
        if (ch>='0' && ch<='9') {
            //4-1、是歌词信息
            [self parseLrcWithTimeString:str];
        } else {
            //4-2、是歌曲信息
            [self parseLrcWithSongInformation:str];
        }
    }
    //5、将歌词按照时间进行排序
    [_allLrcItems sortUsingSelector:@selector(isBiggerTimeThan:)];
}

//4-1、歌词信息解析
- (void)parseLrcWithTimeString:(NSString *)timeString
{
    /*
     [02:11.27][01:50.22][00:21.95]穿过幽暗地岁月
     */
    //按照']'进行切割
    NSArray *ary = [timeString componentsSeparatedByString:@"]"];
    
    //[00:20.81、@""
    //过滤没有歌词的时间行
    if ([ary.lastObject isEqualToString:@""]) {
        return;
    }
    /*
     [02:11.27
     [01:50.22
     [00:21.95
     穿过幽暗地岁月
     */
    for (int i=0; i<ary.count-1; i++) {
        //[02:11.27
        int min = [[ary[i] substringWithRange:NSMakeRange(1, 2)] intValue];
        float sec = [[ary[i] substringFromIndex:4] floatValue];
        float time = min*60 + sec;
        //创建歌词对象,保存信息
        JYLrcItem *item = [[JYLrcItem alloc] init];
        item.time = time;
        item.lrc = [ary lastObject];
        //将歌词对象添加到_allLrcItems
        [_allLrcItems addObject:item];
    }
}

//4-2、歌曲信息解析
- (void)parseLrcWithSongInformation:(NSString *)info
{
    /*
     [ti:蓝莲花]
     [ar:许巍]
     [al:时光-漫步]
     [by:fanver]
     */
    NSString *item = [info substringWithRange:NSMakeRange(1, 2)];
    NSString *content = [info substringWithRange:NSMakeRange(4, info.length-5)];
    if ([item isEqualToString:@"ti"]) {
        _title = content;
    } else if ([item isEqualToString:@"al"]) {
        _albume = content;
    } else if ([item isEqualToString:@"ar"]) {
        _author = content;
    } else if ([item isEqualToString:@"by"]) {
        _byEditor = content;
    }
}

- (void)showAllInfo
{
    for (JYLrcItem *item in _allLrcItems) {
        printf("%s\n", [[item description] UTF8String]);
    }
}

- (NSString *)getLrcByTime:(float)second
{
    /*
     1
     3\5\8\9\11\15...\100
     */
#if 0
    for (int i=0; i<_allLrcItems.count; i++) {
        if ([_allLrcItems[i] time] > second) {
            return [_allLrcItems[i-(i!=0)] lrc];
        }
    }
    return [[_allLrcItems lastObject] lrc];
#else
    int index = (int)_allLrcItems.count - 1;
    for (int i=0; i<_allLrcItems.count; i++) {
        if ([_allLrcItems[i] time] > second) {
            index = i - (i!=0);
            break;
        }
    }
    return [_allLrcItems[index] lrc];
#endif
}

- (void)showSongInfo
{
    if (_title) {
        printf("ti:%s\n", [_title UTF8String]);
    }
    if (_author) {
        printf("ar:%s\n", [_author UTF8String]);
    }
    if (_albume) {
        printf("al:%s\n", [_albume UTF8String]);
    }
    if (_byEditor) {
        printf("by:%s\n", [_byEditor UTF8String]);
    }
}

@end

--------------------------------------------------------------------------------------------


#import <Foundation/Foundation.h>
#import "JYLrcParser.h"

#define PATH @"/Users/qianfeng001/Desktop/wenzhe/RAIN/十五天/day15/歌词文件/蓝莲花.lrc"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        JYLrcParser *parser = [[JYLrcParser alloc] initWithFile:PATH];
        //[parser showAllInfo];
        //NSString *str = [parser getLrcByTime:10];
        //printf("%s\n", [str UTF8String]);
        
        int i = 0;
        NSString *last = nil;
        while (i < 100) {
            NSString *str = [parser getLrcByTime:i];
            if (last != str) {
                last = str;
                system("clear");
                [parser showSongInfo];
                printf("%s\n", [str UTF8String]);
            }
            i++;
            sleep(1);
        }
        
    }
    return 0;
}

posted @ 2015-06-19 16:16  天使雨儿  阅读(267)  评论(0编辑  收藏  举报