~ubuntu-branches/ubuntu/hardy/gnomad2/hardy

« back to all changes in this revision

Viewing changes to src/wavfile.c

  • Committer: Bazaar Package Importer
  • Author(s): Shaun Jackman
  • Date: 2005-08-19 16:09:28 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050819160928-l2glu227nh0algdc
Tags: 2.8.0-2
Add a versioned dependency for libnjb-dev (>> 2.2). Closes: #324036.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* wavfile.c
 
2
   WAV interface
 
3
   Copyright (C) 2004 Linus Walleij
 
4
 
 
5
This file is part of the GNOMAD package.
 
6
 
 
7
GNOMAD is free software; you can redistribute it and/or modify
 
8
it under the terms of the GNU General Public License as published by
 
9
the Free Software Foundation; either version 2, or (at your option)
 
10
any later version.
 
11
 
 
12
You should have received a copy of the GNU General Public License
 
13
along with GNOMAD; see the file COPYING.  If not, write to
 
14
the Free Software Foundation, 59 Temple Place - Suite 330,
 
15
Boston, MA 02111-1307, USA. 
 
16
 
 
17
Much of the code in this file was derived from the getid3() code,
 
18
written in PHP. The C implementation here is however made from
 
19
scratch.
 
20
 
 
21
*/
 
22
 
 
23
#include "common.h"
 
24
#include "metadata.h"
 
25
#include "filesystem.h"
 
26
#include "util.h"
 
27
#include <unistd.h>
 
28
#include <sys/types.h>
 
29
#include <sys/stat.h>
 
30
#include <fcntl.h>
 
31
 
 
32
static guint32 le_to_guint(guchar *data)
 
33
{
 
34
  return data[3] << 24 | data[2] << 16 | data[1] << 8 | data[0];
 
35
}
 
36
 
 
37
/* -------------------------------------- */
 
38
/* EXPORTED FUNCTIONS                     */
 
39
/* -------------------------------------- */
 
40
 
 
41
void
 
42
get_tag_for_wavfile (metadata_t *meta)
 
43
{
 
44
  gint fd;
 
45
  guchar header[46];
 
46
  gchar *tmppath = filename_fromutf8(meta->path);
 
47
  gint n;
 
48
 
 
49
  // g_print("Getting WAV tag info for %s...\n", meta->path);
 
50
  fd = (gint) open(tmppath, O_RDONLY, 0);
 
51
  if (fd < 0) {
 
52
    g_free(tmppath);
 
53
    return;
 
54
  }
 
55
  // g_print("Opened file\n");
 
56
  g_free(tmppath);
 
57
 
 
58
  // Read in some stuff...
 
59
  n = read(fd,header,46);
 
60
  if (n == 46) {
 
61
    // Hardcoded RIFF header
 
62
    if ((header[0] == 'R' || header[0] == 'r') &&
 
63
        (header[1] == 'I' || header[1] == 'i') &&
 
64
        (header[2] == 'F' || header[2] == 'f') &&
 
65
        (header[3] == 'F' || header[3] == 'f') &&
 
66
        (header[8] == 'W' || header[8] == 'w') &&
 
67
        (header[9] == 'A' || header[9] == 'a') &&
 
68
        (header[10] == 'V' || header[10] == 'v') &&
 
69
        (header[11] == 'E' || header[11] == 'e') &&
 
70
        (header[12] == 'f' || header[12] == 'F') &&
 
71
        (header[13] == 'm' || header[13] == 'M') &&
 
72
        (header[14] == 't' || header[14] == 'T') &&
 
73
        header[15] == ' ') {
 
74
      // This is indeed a RIFF/WAVE file
 
75
      guint32 chunksize = le_to_guint(&header[4]);
 
76
      guint32 fmtchunksize = le_to_guint(&header[16]);
 
77
      guint32 samplerate = le_to_guint(&header[24]);
 
78
      guint32 byterate = le_to_guint(&header[28]);
 
79
      guint32 calctime;
 
80
 
 
81
      // Calculate the run time. Remove the format chunk size,
 
82
      // the four remaining bytes of the RIFF header and the
 
83
      // data header (8 bytes).
 
84
      calctime = (chunksize - fmtchunksize - 8 - 4) / byterate;
 
85
      if (calctime == 0) {
 
86
        // Atleast one second, please.
 
87
        calctime = 1;
 
88
      }
 
89
 
 
90
      //g_print("RIFF/WAVE chunksize: %d bytes\n", chunksize);
 
91
      //g_print("Sample rate: %d samples/s\n", samplerate);
 
92
      //g_print("Byte rate: %d bytes/s\n", byterate);
 
93
      //g_print("Calculated time: %d seconds\n", calctime);
 
94
      if (meta->length != NULL) {
 
95
        g_free(meta->length);
 
96
      }
 
97
      meta->length = seconds_to_mmss(calctime);
 
98
    }
 
99
  }
 
100
 
 
101
  close(fd);
 
102
}