コード例 #1
0
K2sendPlayListItem::K2sendPlayListItem (KListView * p, QString fn):
KListViewItem (p), m_filename (fn), _playing (FALSE), _tag ("k2sendtag")
{

    QString rate;
    _id++;
    _my_id = _id;
    _valid = TRUE;
    TagLib::MPEG::File mp3file (m_filename.latin1 ());
    if (!mp3file.tag () || !mp3file.isValid ()) {
        kdDebug (200010) << "K2sendPlayListItem " << m_filename << " not valid " << endl;
        _valid = FALSE;
        return;
    }

    str_id.sprintf ("%03i", p->childCount ());
    setText (0, str_id);

    if (!mp3file.tag ()->title ().isEmpty ()) {
        m_title = mp3file.tag ()->title ().toCString ();
        setText (1, m_title);
        m_album = mp3file.tag ()->album ().toCString ();
        setText (2, m_album);
        m_artist = mp3file.tag ()->artist ().toCString ();
        setText (3, m_artist);

    }
    else {
        QFileInfo info (m_filename);
        m_title = info.baseName ();
        setText (1, m_title);
    }
    int len = mp3file.audioProperties ()->length ();
    if (len)
        m_length.sprintf ("%02i:%02i", len / 60, len % 60);
    else {
        kdDebug (200010) << "K2sendPlayListItem " << m_filename << " is zero lenght " << endl;
        _valid = FALSE;
        return;
    }
    rate.sprintf ("%i", mp3file.audioProperties ()->bitrate ());
    setText (4, m_length);
    setText (5, rate);
    _normal_height = height ();
    _double_height = (int) (height () * 1.5);

}
コード例 #2
0
bool
K2sendPlayer::bluePlay (QString * filename)
{

    char buffer[buffer_size];
    int bytes_done;
    int file_len;
    int bytes_sent;
    int current_bytes_sent;
    int rc;
    int play_length;
    bool stop = FALSE;
    unsigned long long now;
    fd_set rfds, wfds;
    struct timeval tv;
    K2sendPlayerCommand *cmd = 0;
    K2sendPlayerCommand *new_cmd = 0;
    K2sendStatusEvent *se = 0;

    TagLib::MPEG::File mp3file (filename->latin1 ());
    play_length = mp3file.audioProperties ()->length ();
    QFile file (*filename);
    kdDebug (200010) << "K2sendPlayer::bluePlay name=" << *filename << " lenght=" << play_length << endl;
    setReset (0);
    setLoudness (loud_filt);
    setVolume (100 - volume);

    if (!file.open (IO_ReadOnly)) {
        QString msg = QString ("Error opening file ");
        se = new K2sendStatusEvent (K2sendStatusEvent::EventError, msg);
        //kdDebug(200010) << "post 34" << endl;
        QApplication::postEvent (m_parent, se);
        return FALSE;
    }
    se = new K2sendStatusEvent (K2sendStatusEvent::EventTitle, *filename);
    //kdDebug(200010) << "post 35" << endl;
    QApplication::postEvent (m_parent, se);

    file_len = file.size ();
    current_bytes_sent = bytes_sent = 0;
    last_ticks = start_ticks = getTime ();

    do {
        /* check if we can send(write) or receive(read) data */
        FD_ZERO (&rfds);
        FD_SET (blue_sock, &rfds);
        FD_ZERO (&wfds);
        FD_SET (blue_sock, &wfds);

        tv.tv_sec = 3;
        tv.tv_usec = 0;

        rc = select (FD_SETSIZE, &rfds, &wfds, NULL, &tv);
        if (rc < 1) {
            kdDebug (200010) << "K2sendPlayer::bluePlay select()" << endl;
            QString msg = QString ("Communication error");
            se = new K2sendStatusEvent (K2sendStatusEvent::EventMessage, msg, 2000);
            //kdDebug(200010) << "post 36" << endl;
            QApplication::postEvent (m_parent, se);
            blueClose ();
            return FALSE;
        }


        /* are we allowed to write? */
        if (FD_ISSET (blue_sock, &wfds)) {
            buffer[0] = K2sendPlayerCommand::Data;

            if (!(bytes_done = file.readBlock (buffer + 1, sizeof (buffer) - 1))) {
                if (file.atEnd ()) {
                    kdDebug (200010) << "K2sendPlayer::bluePlay Error reading file " << endl;
                    QString msg = QString ("Error reading file");
                    se = new K2sendStatusEvent (K2sendStatusEvent::EventMessage, msg, 2000);
                    //kdDebug(200010) << "post 37" << endl;
                    QApplication::postEvent (m_parent, se);

                }
            }
            else {
                if (write (blue_sock, buffer, bytes_done + 1) != bytes_done + 1) {
                    kdDebug (200010) << "K2sendPlayer::bluePlay Error sending" << endl;
                    QString msg = QString ("Error sending data");
                    se = new K2sendStatusEvent (K2sendStatusEvent::EventMessage, msg, 2000);
                    //kdDebug(200010) << "post 38" << endl;
                    QApplication::postEvent (m_parent, se);
                }
            }
            bytes_sent += bytes_done;
            current_bytes_sent += bytes_done;
        }
        if (FD_ISSET (blue_sock, &rfds)) {
            rc = read (blue_sock, buffer, sizeof (buffer));
            kdDebug (200010) << "K2sendPlayer::bluePlay  got data " << rc << " " << buffer[0] << " " << buffer[1] <<
                endl;
            /* handle commands */
            switch (buffer[0]) {
            case K2sendPlayerCommand::Buttons:
                if ((buffer[1] & 1) == 1) {
                    volume += 10;
                    if (volume > 100)
                        volume = 100;
                    //setVolume(100 - volume,TRUE);
                    kdDebug (200010) << "got cmd volume" << endl;
                    new_cmd = new K2sendPlayerCommand (K2sendPlayerCommand::Volume, volume);
                    addCommand (new_cmd);
                }
                if (buffer[1] & 2) {
                    se = new K2sendStatusEvent (K2sendStatusEvent::EventSkip, NULL, 0);
                    //kdDebug(200010) << "post 39" << endl;
                    kdDebug (200010) << "got cmd skip" << endl;
                    QApplication::postEvent (m_parent, se);
                    new_cmd = new K2sendPlayerCommand (K2sendPlayerCommand::Skip, 0);
                    addCommand (new_cmd);
                }
                if (buffer[1] & 4) {
                    volume -= 10;
                    if (volume < 0)
                        volume = 0;
                    //setVolume(100 - volume,TRUE);
                    kdDebug (200010) << "got cmd volume" << endl;
                    new_cmd = new K2sendPlayerCommand (K2sendPlayerCommand::Volume, volume);
                    addCommand (new_cmd);
                }
                if (buffer[1] & 8) {
                    se = new K2sendStatusEvent (K2sendStatusEvent::EventError, "Battery low", 2000);
                    kdDebug (200010) << "battery low" << endl;
                    QApplication::postEvent (m_parent, se);
                    new_cmd = new K2sendPlayerCommand (K2sendPlayerCommand::Stop, 0);
                    addCommand (new_cmd);
                }
                if (buffer[1] & 16) {
                    new_cmd = new K2sendPlayerCommand (K2sendPlayerCommand::Loudness, 0);
                    kdDebug (200010) << "got cmd loudness" << endl;
                    addCommand (new_cmd);
                }
                break;
            default:
                QString msg = QString ("Unknown command from BlueMP3");
                se = new K2sendStatusEvent (K2sendStatusEvent::EventMessage, msg, 2000);
                //kdDebug(200010) << "post 41" << endl;
                QApplication::postEvent (m_parent, se);
                break;
            }
        }
        now = getTime ();

        if ((last_ticks) < now - 250) {
            updateStatus (bytes_sent, current_bytes_sent, file_len, play_length);
            last_ticks = now;
        }
        cmd = 0;
        cmd = command ();
        if (cmd) {
            switch (cmd->command ()) {
            case K2sendPlayerCommand::Loudness:
                if (++loud_filt > 8)
                    loud_filt = 0;
                setLoudness (loud_filt);
                delete cmd;
                cmd = 0;
                break;
            case K2sendPlayerCommand::Volume:
                volume = cmd->value ();
                setVolume (100 - volume, FALSE);
                delete cmd;
                cmd = 0;
                break;
            case K2sendPlayerCommand::Play:
                bytes_done = 0;
                stop = TRUE;
                addCommand (cmd);
                break;
            case K2sendPlayerCommand::Stop:
                stop = TRUE;
                /* forward to global handling */
                addCommand (cmd);
                break;
            case K2sendPlayerCommand::Skip:
                stop = TRUE;
                addCommand (cmd);
                break;

            case K2sendPlayerCommand::Length:
                start_ticks = getTime ();
                current_bytes_sent = 0;
                bytes_sent = (file_len / 100) * cmd->value ();
                file.at (bytes_sent);
                delete cmd;
                cmd = 0;
                break;
            default:
                break;
            }
        }

    } while (bytes_sent != file_len && !stop);
    file.close ();

    se = new K2sendStatusEvent (K2sendStatusEvent::EventTitle, "");
    //kdDebug(200010) << "post 42" << endl;
    QApplication::postEvent (m_parent, se);

    this->clearStatus ();
    /* keep playing */
    if (!stop) {
        kdDebug (200010) << "K2sendPlayer::bluePlay keep playing" << endl;
        se = new K2sendStatusEvent (K2sendStatusEvent::EventSkip, 0, 0);
        //kdDebug(200010) << "post 43" << endl;
        QApplication::postEvent (m_parent, se);
        new_cmd = new K2sendPlayerCommand (K2sendPlayerCommand::Play, 0);
        addCommand (new_cmd);
    }
    kdDebug (200010) << "K2sendPlayer::bluePlay done" << endl;
    return TRUE;
}
コード例 #3
0
ファイル: mp3unicode.cpp プロジェクト: alonbl/mp3unicode
int main (int argc, char *argv[]) {
	try {
		static struct option long_options[] = {
			{ "source-encoding", required_argument, NULL, 's' },
			{ "id3v1-encoding", required_argument, NULL, '1' },
			{ "id3v2-encoding", required_argument, NULL, '2' },
			{ "preserve-unicode", no_argument, NULL, 'p' },
			{ "preview", no_argument, NULL, 'w' },
			{ "version", no_argument, NULL, 'v' },
			{ "help", no_argument, NULL, 'h' },
   			{ "quiet", no_argument, NULL, 'q' },
			{ NULL, 0, NULL, 0 }
		};
		int long_options_ret;
		std::string source_encoding;
		std::string id3v1_encoding = "none";
		std::string id3v2_encoding = "none";
		bool preserve_unicode = false;
		bool preview = false;
		bool usage_ok = true;
		bool verbose = true;
		
		if(argc == 1) {
			printf("%s", msg::usage);
			exit(1);
		}

		while (
			(long_options_ret = getopt_long (argc, argv, "s:1:2:pwdvhq", long_options, NULL)) != -1
		) {
			switch (long_options_ret) {
				case 's':
					source_encoding = optarg;
					tolower(source_encoding);
				break;
				case '1':
					id3v1_encoding = optarg;
					tolower(id3v1_encoding);
				break;
				case '2':
					id3v2_encoding = optarg;
					tolower(id3v2_encoding);
				break;
				case 'p':
					preserve_unicode = true;
				break;
				case 'w':
					preview = true;
				break;
				case 'v':
					printf("%s %s\n", PACKAGE, PACKAGE_VERSION);
					printf("%s", msg::copyright);
					exit (1);
				break;
				case 'h':
					printf("%s", msg::usage);
					exit(1);
				case 'q':
					verbose = false;
				break;
				default:
					usage_ok = false;
				break;
			}
		}

		if (usage_ok && source_encoding.empty ()) {
			printf("%s", msg::nosenc);
			usage_ok = false;
		}
		
		if(optind == argc) {
			printf("%s", msg::nofiles);
			usage_ok = false;
		}

		if (!usage_ok) {
			printf("%s", msg::seehelp);
			exit (1);
		}

		TagLib::ID3v2::FrameFactory::instance()->setDefaultTextEncoding (TagLib::String::UTF8);

		for (int i=optind;i<argc;i++) {
			TagLib::MPEG::File mp3file(argv[i]);

			if (!mp3file.isOpen ()) {
				throw msg::nofile(argv[i]);
			}
			
			TagLib::Tag *tag = mp3file.tag();
			
			if(tag->isEmpty()) {
				printf("%s", msg::emptyfile(argv[i]).c_str());
			}
			else {
				Converter converter (
					source_encoding,
					id3v1_encoding,
					id3v2_encoding,
					mp3file.ID3v1Tag (true),
					mp3file.ID3v2Tag (true),
					preserve_unicode
				);
	
				converter.Convert (tag->title (), &TagLib::Tag::setTitle);
				converter.Convert (tag->artist (), &TagLib::Tag::setArtist);
				converter.Convert (tag->album (), &TagLib::Tag::setAlbum);
				converter.Convert (tag->comment (), &TagLib::Tag::setComment);
				converter.Convert (tag->genre (), &TagLib::Tag::setGenre);
				
				if (preview) {
					converter.printTags(argv[i]);
				}
				else {
					mp3file.strip(~converter.Tags());		
					if (!mp3file.save (converter.Tags ())) {
						printf("%s", msg::writefail(argv[i]).c_str());
					}
					else if(verbose) {
						printf("%s", msg::filedone(argv[i]).c_str());
					}
				}
			}
		}

		exit (0);
	}
	catch (const std::string &e) {
		printf ("%s", msg::error(e).c_str());
		exit (1);
	}
	catch (const char *e) {
		printf ("%s", msg::error(e).c_str());
		exit (1);
	}

	return 1;
}