summaryrefslogtreecommitdiff
path: root/mp3lame.rs
blob: 1cfb0c68d2f6ebffe9b5a8c86065828dbc7504ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use std::libc::{c_float, c_int, c_short, c_uchar, c_ulong, c_void};
use std::vec;

type GlobalFlags_ = *c_void;

enum MpegMode {
  Stereo = 0,
  JointStereo,
  DualChannel,
  Mono,
  NotSet,
  MaxIndicator
}

#[link_args = "-lmp3lame"]

extern {
  fn lame_init() -> GlobalFlags_;
  fn lame_set_num_samples(gfp: GlobalFlags_, samples: c_ulong) -> c_int;
  fn lame_get_num_samples(gfp: GlobalFlags_) -> c_ulong;
  fn lame_set_in_samplerate(gfp: GlobalFlags_, rate: c_int) -> c_int;
  fn lame_get_in_samplerate(gfp: GlobalFlags_) -> c_int;
  fn lame_set_num_channels(gfp: GlobalFlags_, channels: c_int) -> c_int;
  fn lame_get_num_channels(gfp: GlobalFlags_) -> c_int;
  fn lame_set_scale(gfp: GlobalFlags_, scale: c_float) -> c_int;
  fn lame_get_scale(gfp: GlobalFlags_) -> c_float;
  fn lame_set_scale_left(gfp: GlobalFlags_, scale: c_float) -> c_int;
  fn lame_get_scale_left(gfp: GlobalFlags_) -> c_float;
  fn lame_set_scale_right(gfp: GlobalFlags_, scale: c_float) -> c_int;
  fn lame_get_scale_right(gfp: GlobalFlags_) -> c_float;
  fn lame_set_out_samplerate(gfp: GlobalFlags_, rate: c_int) -> c_int;
  fn lame_get_out_samplerate(gfp: GlobalFlags_) -> c_int;
  fn lame_set_analysis(gfp: GlobalFlags_, analysis: c_int) -> c_int;
  fn lame_get_analysis(gfp: GlobalFlags_) -> c_int;
  fn lame_set_bWriteVbrTag(gfp: GlobalFlags_, write_tag: c_int) -> c_int;
  fn lame_get_bWriteVbrTag(gfp: GlobalFlags_) -> c_int;
  fn lame_set_decode_only(gfp: GlobalFlags_, decode: c_int) -> c_int;
  fn lame_get_decode_only(gfp: GlobalFlags_) -> c_int;
  fn lame_set_quality(gfp: GlobalFlags_, quality: c_int) -> c_int;
  fn lame_get_quality(gfp: GlobalFlags_) -> c_int;
  fn lame_set_mode(gfp: GlobalFlags_, mode: MpegMode) -> c_int;
  fn lame_get_mode(gfp: GlobalFlags_) -> MpegMode;
  fn lame_set_brate(gfp: GlobalFlags_, rate: c_int) -> c_int;
  fn lame_get_brate(gfp: GlobalFlags_) -> c_int;
  fn lame_set_disable_reservoir(gfp: GlobalFlags_, disable: c_int) -> c_int;
  fn lame_get_disable_reservoir(gfp: GlobalFlags_) -> c_int;
  fn lame_init_params(gfp: GlobalFlags_) -> c_int;
  fn lame_close(gfp: GlobalFlags_) -> c_int;
  fn lame_encode_buffer_interleaved(gfp: GlobalFlags_,
                                    pcm: *c_short,
                                    num_samples:c_int,
                                    mp3buf: *c_uchar,
                                    mp3buf_size: c_int) -> c_int;
  fn lame_encode_flush_nogap(gfp: GlobalFlags_,
                             mp3buf: *c_uchar,
                             size: c_int) -> c_int;
}

pub struct LameContext {
  gfp: GlobalFlags_
}

impl LameContext {
  #[fixed_stack_segment]
  pub fn new() -> LameContext {
    LameContext { gfp: unsafe { lame_init() }}
  }
  #[fixed_stack_segment]
  pub fn set_in_samplerate(&self, rate: int) {
    unsafe { lame_set_in_samplerate(self.gfp, rate as c_int) };
  }
  #[fixed_stack_segment]
  pub fn set_out_samplerate(&self, rate: int) {
    unsafe { lame_set_out_samplerate(self.gfp, rate as c_int) };
  }
  #[fixed_stack_segment]
  pub fn get_out_samplerate(&self) -> int {
    unsafe { lame_get_out_samplerate(self.gfp) as int }
  }
  #[fixed_stack_segment]
  pub fn set_num_channels(&self, channels: int) {
    unsafe { lame_set_num_channels(self.gfp, channels as c_int) };
  }
  #[fixed_stack_segment]
  pub fn set_quality(&self, quality: int) {
    unsafe { lame_set_quality(self.gfp, quality as c_int) };
  }
  #[fixed_stack_segment]
  pub fn set_bitrate(&self, rate: int) {
    unsafe { lame_set_brate(self.gfp, rate as c_int) };
  }
  #[fixed_stack_segment]
  pub fn set_disable_reservoir(&self, disable: bool) {
    unsafe { lame_set_disable_reservoir(self.gfp, disable as c_int) };
  }
  #[fixed_stack_segment]
  pub fn init_params(&self) {
    unsafe { lame_init_params(self.gfp) };
  }
  #[fixed_stack_segment]
  pub fn encode_buffer_interleaved(&self, pcm: &[u8]) -> ~[u8] {
    if pcm.len() % 4 != 0 {
      return ~[];
    }
    let num_samples = pcm.len() / 4;
    let mp3buf_size = (1.25 * num_samples as float + 7200.0) as uint;
    unsafe {
      let mut mp3buf: ~[u8] = vec::with_capacity(mp3buf_size);
      let length = lame_encode_buffer_interleaved(self.gfp,
                                    vec::raw::to_ptr(pcm) as *c_short,
                                    num_samples as c_int,
                                    vec::raw::to_mut_ptr(mp3buf) as *c_uchar,
                                    mp3buf_size as c_int);
      if length < 0 {
        return ~[];
      }
      vec::raw::set_len(&mut mp3buf, length as uint);
      mp3buf
    }
  }
}

impl Drop for LameContext {
  #[fixed_stack_segment]
  fn drop(&mut self) {
    unsafe { lame_close(self.gfp) };
  }
}