type == 'audio') { if ($op == 'insert' || $op == 'update') { // update the metadata in the file _audio_getid3_save_to_file($node); } } } /** * Implements the audio module's hook_audio() */ function audio_getid3_audio($op, &$node) { switch ($op) { case 'upload': if ($info = audio_read_id3tags($node->audio['file']->filepath, TRUE)) { $node->audio_tags = $info['tags']; $node->audio_images = $info['images']; // use array_merge so that the play count and downloadable settings aren't // overwritten. $node->audio = array_merge($node->audio, $info['fileinfo']); } break; } } /** * Edit the audio node form and insert our file info stuff. */ function audio_getid3_form_alter(&$form, &$form_state, $form_id) { // We only alter audio node edit forms with a file attached. if ($form_id == 'audio_node_form' && !empty($form['#node']->audio['file']->filepath)) { $form['#submit'][] = 'audio_getid3_node_submit'; $form['audio']['#description'] = t('This file information was loaded from the file by the getID3 library.'); // Refresh the meta data everytime they display the edit form. $info = audio_read_id3tags($form['#node']->audio['file']->filepath, FALSE); // Overwrite the default audio module fields with a hidden value so that // so there's something POSTed back for the preview. $fields = array('format', 'file_size', 'playtime', 'sample_rate', 'channel_mode', 'bitrate', 'bitrate_mode'); foreach ($fields as $key) { $form['audio'][$key] = array( '#type' => 'hidden', '#default_value' => isset($info['fileinfo'][$key]) ? $info['fileinfo'][$key] : '', ); } $form['audio']['display_format'] = array( '#type' => 'item', '#title' => t('Format'), '#value' => $info['fileinfo']['format'], ); $form['audio']['display_file_size'] = array( '#type' => 'item', '#title' => t('File Size'), '#value' => t('@filesize bytes', array('@filesize' => number_format($info['fileinfo']['file_size']))), ); $form['audio']['display_playtime'] = array( '#type' => 'item', '#title' => t('Length'), '#value' => $info['fileinfo']['playtime'], ); $form['audio']['display_sample_rate'] = array( '#type' => 'item', '#title' => t('Sample rate'), '#value' => t('@samplerate Hz', array('@samplerate' => number_format($info['fileinfo']['sample_rate']))), ); $form['audio']['display_channel_mode'] = array( '#type' => 'item', '#title' => t('Channel mode'), '#value' => ucfirst($info['fileinfo']['channel_mode']), ); $form['audio']['display_bitrate'] = array( '#type' => 'item', '#title' => t('Bitrate'), '#value' => t('@bitrate bytes/second', array('@bitrate' => number_format($info['fileinfo']['bitrate']))), ); $form['audio']['display_bitrate_mode'] = array( '#type' => 'item', '#title' => t('Bitrate mode'), '#value' => strtoupper($info['fileinfo']['bitrate_mode']), ); // Check that the audio is compatible with Flash (MP3 with sample rate of // 11, 22, or 44 kHz). Display a warning if it is not. switch ($info['fileinfo']['sample_rate']) { case '44100': case '22050': case '11025': if ($info['fileinfo']['format'] == 'mp3') { break; } default: drupal_set_message(t('This file is not compatible with the Flash audio player. Flash can only play MP3 files with a sample rate of 11, 22 or 44KHz.'), 'status'); } } } function audio_getid3_node_submit($form, &$form_state) { } /** * Build a getID3 object. * * @return * a getID3 object. */ function _audio_getid3_load() { if (!getid3_load(TRUE)) { return NULL; } $getid3 = new getID3; $getid3->encoding = 'UTF-8'; $getid3->encoding_id3v1 = 'ISO-8859-1'; $getid3->option_tags_html = FALSE; return $getid3; } /** * Uses getID3 to get information about an audio file... * * @param $filepath * string full path to audio file to examine * @param $load_pics * boolean indicating if embedded images should be saved to temp files and * returned in a sub array 'images'. * @return * array with two sub arrays keyed to 'tags' and 'fileinfo', or FALSE if * there's an error. */ function audio_read_id3tags($filepath, $load_pics = FALSE) { $getid3 = _audio_getid3_load(); if (!$getid3) { // getid3 isn't setup correctly. an error should have already been printed // so just return. return FALSE; } // Analyze file $info = $getid3->analyze($filepath); // copy out the basic file info $ret = array( 'tags' => array(), 'images' => array(), 'fileinfo' => array( 'format' => $info['fileformat'], 'file_size' => $info['filesize'], 'filemime' => $info['mime_type'], 'playtime' => $info['playtime_string'], 'bitrate' => $info['audio']['bitrate'], 'bitrate_mode' => $info['audio']['bitrate_mode'], 'channel_mode' => $info['audio']['channelmode'], 'sample_rate' => $info['audio']['sample_rate'], ), ); // First read in the id3v1 tags then overwrite them with the v2 tags. foreach (array('id3v1', 'id3v2') as $format) { if (isset($info['tags'][$format])) { foreach ((array) $info['tags'][$format] as $key => $value ) { $ret['tags'][$key] = trim(array_pop($value)); } } } // copy the images if ($load_pics) { // check both flavors id3v2 image tags foreach (array('PIC', 'APIC') as $type) { if (isset($info['id3v2'][$type])) { foreach ((array)$info['id3v2'][$type] as $image) { // Save it to a temp file $file = audio_image_save_data(basename($filepath), $image['data'], $image['image_mime'], $image['picturetypeid']); $ret['images'][$file->pictype .'-'. $file->fid] = $file; } } } } // warnings if (!empty($info['warning']) && variable_get('audio_getid3_show_warnings', FALSE)) { $warning = t('While reading the ID3 tags, the following warnings were encountered:'); $warning .= '