You are here

function audiofield_details_formatter in AudioField 7

Get details formater.

1 call to audiofield_details_formatter()
audiofield_field_formatter_view in ./audio.field.inc
Implements hook_field_formatter_view().

File

./audiofield.module, line 132
Audio Field module for displaying audio files as usable players.

Code

function audiofield_details_formatter($audio_path, $audiofield_detail) {
  $details = array(
    'list' => array(),
  );

  // Display filename.
  switch ($audiofield_detail['filename']) {

    // Actual filename.
    case 1:
      $details['list']['filename'] = t('Filename: @filename', array(
        '@filename' => drupal_basename($audio_path),
      ));
      break;

    // File name (remove extension).
    case 2:
      $details['list']['filename'] = t('Filename: @filename', array(
        '@filename' => pathinfo($audio_path, PATHINFO_FILENAME),
      ));
      break;

    // Only extension.
    case 3:
      $details['list']['filename'] = t('Extension: @extension', array(
        '@extension' => pathinfo($audio_path, PATHINFO_EXTENSION),
      ));
      break;
  }

  // File size.
  if (!empty($audiofield_detail['filesize'])) {
    $filesize = filesize(drupal_realpath($audio_path));
    if ($audiofield_detail['filesize'] == '1') {
      $filesize = format_size($filesize);
    }
    $details['list']['filesize'] = t('Filesize: @filesize', array(
      '@filesize' => $filesize,
    ));
  }
  foreach ($audiofield_detail as $key => $val) {
    if ($val && !in_array($key, array(
      'ffprobe_path',
      'filename',
      'filesize',
    ))) {
      $file_info['getid3'] = audiofield_getid3_analyze($audio_path);
      $file_info['ffprobe'] = audiofield_ffprobe_analyze($audio_path);

      // Return existing details if we don't have a way to read tags.
      if (!$file_info) {
        return $details;
      }
      switch ($key) {
        case 'filename':

          // Add the filename from the tags if we couldn't load it directly.
          if (empty($details['list']['filename'])) {
            $details['list']['filename'] = t('Filename: @filename', array(
              '@filename' => $file_info['getid3']['filename'],
            ));
          }
          break;
        case 'filesize':

          // Add the filesize from the tags if we couldn't load it directly.
          if (empty($details['list']['filesize'])) {
            $details['list']['filesize'] = t('Filesize: $filesize', array(
              '@filesize' => $val == '1' ? format_size($file_info['getid3']['filesize']) : $file_info['getid3']['filesize'],
            ));
          }
          break;
        case 'codec':
          $codec = '';
          $codec_long = '';
          if (!empty($file_info['ffprobe']['_audio']['codec_name'])) {
            $codec = $file_info['ffprobe']['_audio']['codec_name'];
            $codec_long = $file_info['ffprobe']['_audio']['codec_long_name'];
          }
          elseif (!empty($file_info['getid3']['audio']['codec'])) {
            $codec = $file_info['getid3']['audio']['codec'] . ' (' . (isset($file_info['getid3']['audio']['encoder_settings']) ? $file_info['getid3']['audio']['encoder_settings'] : @$file_info['getid3']['audio']['dataformat']) . ')';
            $codec_long = $codec;
          }
          $details['list'][] = t('Codec: @codec', array(
            '@codec' => $val == '2' ? $codec_long : $codec,
          ));
          break;
        case 'length':
          $length_timeformat = '';
          $length_sec = '';
          if (!empty($file_info['ffprobe']['format']['duration'])) {
            $length_timeformat = gmdate('H:i:s', $file_info['ffprobe']['format']['duration']);
            if (substr($length_timeformat, 0, 3) == '00:') {
              $length_timeformat = substr($length_timeformat, 3);
            }
            $length_sec = $file_info['ffprobe']['format']['duration'];
          }
          elseif (!empty($file_info['getid3']['playtime_seconds'])) {
            $length_timeformat = $file_info['getid3']['playtime_string'];
            $length_sec = $file_info['getid3']['playtime_seconds'];
          }
          $details['list'][] = t('Length: @length', array(
            '@length' => $val == '2' ? round($length_sec, 3) . ' ' . t('seconds') : $length_timeformat,
          ));
          break;
        case 'channelmode':
          $channelmode = '';
          if (!empty($file_info['ffprobe']['_audio']['channel_layout'])) {
            $channelmode = $file_info['ffprobe']['_audio']['channel_layout'];
          }
          elseif (!empty($file_info['getid3']['audio']['channelmode'])) {
            $channelmode = $file_info['getid3']['audio']['channelmode'];
          }
          $details['list'][] = t('Channel mode: @channelmode', array(
            '@channelmode' => $channelmode,
          ));
          break;
        case 'samplerate':
          $samplerate = '';
          if (!empty($file_info['ffprobe']['_audio']['sample_rate'])) {
            $samplerate = $file_info['ffprobe']['_audio']['sample_rate'];
          }
          elseif (!empty($file_info['getid3']['audio']['sample_rate'])) {
            $samplerate = $file_info['getid3']['audio']['sample_rate'];
          }
          $details['list'][] = t('Sample rate: @samplerate Hz', array(
            '@samplerate' => $samplerate,
          ));
          break;
        case 'bitrate':
          $bitrate = '';
          if (!empty($file_info['ffprobe']['_audio']['bit_rate'])) {
            $bitrate = $file_info['ffprobe']['_audio']['bit_rate'];
          }
          elseif (!empty($file_info['getid3']['bitrate'])) {
            $bitrate = $file_info['getid3']['bitrate'];
          }
          $details['list'][] = t('Bitrate: @bitrate', array(
            '@bitrate' => (substr($val, 0, 1) == 'k' ? substr($bitrate, 0, -3) : $bitrate) . ' ' . $val,
          ));
          break;
        case 'tags_id3':
          $tags = array();
          if ($val == 'id3') {
            if (isset($file_info['getid3']['id3v2']['comments'])) {
              $id3_encoding = $file_info['getid3']['id3v2']['encoding'];
              $id3 = $file_info['getid3']['id3v2']['comments'];
            }
            elseif (isset($file_info['getid3']['id3v1']['comments'])) {
              $id3_encoding = $file_info['getid3']['id3v1']['encoding'];
              $id3 = $file_info['getid3']['id3v1']['comments'];
            }
            if (!empty($id3)) {
              foreach ($id3 as $key => $val) {
                if (is_array($val)) {
                  if (in_array($id3_encoding, array(
                    'ISO-8859-1',
                  ))) {
                    $id3[$key] = utf8_encode(implode('', $val));
                  }
                  else {
                    $id3[$key] = implode('', $val);
                  }
                }
              }
              $tags[] = '<code class="audiofield_id3">' . json_encode($id3) . '</code>';
            }
          }
          else {
            $get_tags = explode('-', $val);
            foreach ($get_tags as $tag) {
              if (isset($file_info['getid3']['id3v2']['comments'][$tag])) {
                $tags[$tag] = implode(';', $file_info['getid3']['id3v2']['comments'][$tag]);
              }
              elseif (isset($file_info['getid3']['id3v1'][$tag])) {
                $tags[$tag] = $file_info['getid3']['id3v1'][$tag];
              }
            }
          }
          $details['list']['tags'] = t('ID3 tags: !tags', array(
            '!tags' => implode(' - ', $tags),
          ));
          break;
        case 'tags_id3_picture':
          if (isset($file_info['getid3']['id3v2']['APIC'][0]['data'])) {
            if ($val != 'original') {
              $img_size = explode('x', $val);
              $details['img']['attributes'] = array(
                'width' => $img_size[0],
                'height' => $img_size[1],
              );
            }
            $src = 'data:' . $file_info['getid3']['id3v2']['APIC'][0]['image_mime'] . ';charset=utf-8;base64,' . base64_encode($file_info['getid3']['id3v2']['APIC'][0]['data']);
            $details['img']['attributes']['src'] = $src;
          }
          break;
        default:
          $details['list'][] = $key . ': not support';
          break;
      }
    }
  }
  return $details;
}