pinebuds/apps/audioplayers/rbplay/rbplaysd.cpp

223 lines
5.5 KiB
C++
Raw Permalink Normal View History

2022-08-15 04:20:27 -05:00
/***************************************************************************
*
* Copyright 2015-2019 BES.
* All rights reserved. All unpublished rights reserved.
*
* No part of this work may be used or reproduced in any form or by any
* means, or stored in a database or retrieval system, without prior written
* permission of BES.
*
* Use of this work is governed by a license granted by BES.
* This work contains confidential and proprietary information of
* BES. which is protected by copyright, trade secret,
* trademark and other intellectual property rights.
*
****************************************************************************/
/* rbplay source */
/* playback control & rockbox codec porting & codec thread */
#include <ctype.h>
#include <fcntl.h>
2022-08-15 04:20:27 -05:00
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2022-08-15 04:20:27 -05:00
#include <unistd.h>
#include "SDFileSystem.h"
2022-08-15 04:20:27 -05:00
#include "hal_trace.h"
#include "metadata.h"
#include "rbplaysd.h"
#include "utils.h"
#define SD_LABEL "sd"
#define PLAYLIST_PATH "/sd/Playlist"
static SDFileSystem *sdfs = NULL;
// TODO: remove ?
static playlist_item sd_curritem;
playlist_struct sd_playlist;
static void playlist_insert(playlist_item *item) {
int fd;
fd = open(PLAYLIST_PATH, O_RDWR | O_CREAT);
if (fd <= 0) {
error("Playlist open fail");
return;
}
lseek(fd, item->song_idx * sizeof(playlist_item), SEEK_SET);
write(fd, item, sizeof(playlist_item));
close(fd);
2022-08-15 04:20:27 -05:00
}
static bool sdcard_mount(void) {
if (sdfs) {
info("SD card already mount");
return true;
}
2022-08-15 04:20:27 -05:00
sdfs = new SDFileSystem(SD_LABEL);
if (sdfs == NULL) {
error("No memory for sd file system");
return false;
}
2022-08-15 04:20:27 -05:00
DIR *d;
d = opendir("/" SD_LABEL);
2022-08-15 04:20:27 -05:00
if (!d) {
warn("SD card mount error");
return false;
}
2022-08-15 04:20:27 -05:00
closedir(d);
return true;
2022-08-15 04:20:27 -05:00
}
static void app_rbplay_gen_playlist(playlist_struct *list) {
struct dirent *p;
DIR *d;
uint32_t total;
int fd = 0;
struct mp3entry current_id3;
playlist_item *sd_curritem_p = &sd_curritem;
2022-08-15 04:20:27 -05:00
memset(list, 0x0, sizeof(playlist_struct));
2022-08-15 04:20:27 -05:00
d = opendir("/" SD_LABEL);
if (!d) {
error("SD card open fail");
return;
}
2022-08-15 04:20:27 -05:00
info("---------gen audio list---------");
2022-08-15 04:20:27 -05:00
total = 0;
while (p = readdir(d)) {
if (probe_file_format(p->d_name) == AFMT_UNKNOWN)
continue;
2022-08-15 04:20:27 -05:00
memset(&sd_curritem, 0x0, sizeof(playlist_item));
sd_curritem.song_idx = total;
2022-08-15 04:20:27 -05:00
sprintf(sd_curritem.file_path, "/" SD_LABEL "/%s", p->d_name);
sprintf(sd_curritem.file_name, "%s", p->d_name);
2022-08-15 04:20:27 -05:00
info("Adding music: %s", sd_curritem.file_path);
2022-08-15 04:20:27 -05:00
fd = open(sd_curritem.file_path, O_RDONLY);
2022-08-15 04:20:27 -05:00
if (fd <= 0) {
error("File %s open error", p->d_name);
break;
}
get_metadata(&current_id3, fd, sd_curritem.file_path);
close(fd);
if (current_id3.bitrate == 0 || current_id3.filesize == 0 ||
current_id3.length == 0)
break;
2022-08-15 04:20:27 -05:00
info("bits:%d, type:%d, freq:%d", current_id3.bitrate,
current_id3.codectype, current_id3.frequency);
2022-08-15 04:20:27 -05:00
sd_curritem_p->bitrate = current_id3.bitrate;
sd_curritem_p->codectype = current_id3.codectype;
sd_curritem_p->filesize = current_id3.filesize;
sd_curritem_p->length = current_id3.length;
sd_curritem_p->frequency = current_id3.frequency;
2022-08-15 04:20:27 -05:00
#ifdef PARSER_DETAIL
char *str;
str = current_id3.title;
if (str != NULL) {
memset(sd_curritem_p->title, 0x0, MP3_TITLE_LEN);
memcpy(sd_curritem_p->title, str,
strlen(str) > MP3_TITLE_LEN ? MP3_TITLE_LEN : strlen(str));
2022-08-15 04:20:27 -05:00
}
str = current_id3.artist;
if (str != NULL) {
memset(sd_curritem_p->artist, 0x0, MP3_ARTIST_LEN);
memcpy(sd_curritem_p->artist, str,
strlen(str) > MP3_ARTIST_LEN ? MP3_ARTIST_LEN : strlen(str));
}
2022-08-15 04:20:27 -05:00
str = current_id3.album;
if (str != NULL) {
memset(sd_curritem_p->album, 0x0, MP3_ALBUM_LEN);
memcpy(sd_curritem_p->album, str,
strlen(str) > MP3_ALBUM_LEN ? MP3_ALBUM_LEN : strlen(str));
}
2022-08-15 04:20:27 -05:00
str = current_id3.genre_string;
if (str != NULL) {
memset(sd_curritem_p->genre, 0x0, MP3_GENRE_LEN);
memcpy(sd_curritem_p->genre, str,
strlen(str) > MP3_GENRE_LEN ? MP3_GENRE_LEN : strlen(str));
}
2022-08-15 04:20:27 -05:00
str = current_id3.composer;
if (str != NULL) {
memset(sd_curritem_p->composer, 0x0, MP3_COMPOSER_LEN);
memcpy(sd_curritem_p->composer, str,
strlen(str) > MP3_COMPOSER_LEN ? MP3_COMPOSER_LEN : strlen(str));
}
#endif
playlist_insert(sd_curritem_p);
total++;
}
2022-08-15 04:20:27 -05:00
list->total_songs = total;
list->current_item = sd_curritem_p;
2022-08-15 04:20:27 -05:00
closedir(d);
info("---------%d audio file searched---------", total);
2022-08-15 04:20:27 -05:00
}
void app_rbplay_load_playlist(playlist_struct *list) {
if (sdcard_mount() == false)
return;
2022-08-15 04:20:27 -05:00
remove(PLAYLIST_PATH);
2022-08-15 04:20:27 -05:00
app_rbplay_gen_playlist(list);
}
2022-08-15 04:20:27 -05:00
playlist_item *app_rbplay_get_playitem(const int idx) {
int fd;
if (idx >= sd_playlist.total_songs) {
warn("Index exceed: %d / %d", idx, sd_playlist.total_songs);
return NULL;
}
2022-08-15 04:20:27 -05:00
fd = open(PLAYLIST_PATH, O_RDONLY);
2022-08-15 04:20:27 -05:00
if (fd <= 0) {
warn("SD card playlist can not open");
return NULL;
}
2022-08-15 04:20:27 -05:00
lseek(fd, sizeof(playlist_item) * idx, SEEK_SET);
read(fd, sd_playlist.current_item, sizeof(playlist_item));
info("Get playitem: %d: %s", idx, sd_playlist.current_item->file_path);
2022-08-15 04:20:27 -05:00
close(fd);
return sd_playlist.current_item;
2022-08-15 04:20:27 -05:00
}
int app_ctl_remove_file(const int idx) {
playlist_item *item = app_rbplay_get_playitem(idx);
if (!item)
return -1;
remove(item->file_path);
2022-08-15 04:20:27 -05:00
return 0;
}