foobar2000 SDK  2015-01-14
input_raw.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 
3 enum {
6  raw_sample_rate = 44100,
7 
10 };
11 
12 // No inheritance. Our methods get called over input framework templates. See input_singletrack_impl for descriptions of what each method does.
13 class input_raw {
14 public:
15  void open(service_ptr_t<file> p_filehint,const char * p_path,t_input_open_reason p_reason,abort_callback & p_abort) {
16  if (p_reason == input_open_info_write) throw exception_io_unsupported_format();//our input does not support retagging.
17  m_file = p_filehint;//p_filehint may be null, hence next line
18  input_open_file_helper(m_file,p_path,p_reason,p_abort);//if m_file is null, opens file with appropriate privileges for our operation (read/write for writing tags, read-only otherwise).
19  }
20 
21  void get_info(file_info & p_info,abort_callback & p_abort) {
22  t_filesize size = m_file->get_size(p_abort);
23  //note that the file size is not always known, for an example, live streams and alike have no defined size and filesize_invalid is returned
24  if (size != filesize_invalid) {
25  //file size is known, let's set length
27  }
28  //note that the values below should be based on contents of the file itself, NOT on user-configurable variables for an example. To report info that changes independently from file contents, use get_dynamic_info/get_dynamic_info_track instead.
29  p_info.info_set_int("samplerate",raw_sample_rate);
30  p_info.info_set_int("channels",raw_channels);
31  p_info.info_set_int("bitspersample",raw_bits_per_sample);
32  p_info.info_set("encoding","lossless");
33  p_info.info_set_bitrate((raw_bits_per_sample * raw_channels * raw_sample_rate + 500 /* rounding for bps to kbps*/ ) / 1000 /* bps to kbps */);
34 
35  }
36  t_filestats get_file_stats(abort_callback & p_abort) {return m_file->get_stats(p_abort);}
37 
38  void decode_initialize(unsigned p_flags,abort_callback & p_abort) {
39  m_file->reopen(p_abort);//equivalent to seek to zero, except it also works on nonseekable streams
40  }
41  bool decode_run(audio_chunk & p_chunk,abort_callback & p_abort) {
42  enum {
43  deltaread = 1024,
44  };
46  t_size deltaread_done = m_file->read(m_buffer.get_ptr(),deltaread * raw_total_sample_width,p_abort) / raw_total_sample_width;
47  if (deltaread_done == 0) return false;//EOF
48 
50 
51  //processed successfully, no EOF
52  return true;
53  }
54  void decode_seek(double p_seconds,abort_callback & p_abort) {
55  m_file->ensure_seekable();//throw exceptions if someone called decode_seek() despite of our input having reported itself as nonseekable.
56  // IMPORTANT: convert time to sample offset with proper rounding! audio_math::time_to_samples does this properly for you.
58 
59  // get_size_ex fails (throws exceptions) if size is not known (where get_size would return filesize_invalid). Should never fail on seekable streams (if it does it's not our problem anymore).
60  t_filesize max = m_file->get_size_ex(p_abort);
61  if (target > max) target = max;//clip seek-past-eof attempts to legal range (next decode_run() call will just signal EOF).
62 
63  m_file->seek(target,p_abort);
64  }
65  bool decode_can_seek() {return m_file->can_seek();}
66  bool decode_get_dynamic_info(file_info & p_out, double & p_timestamp_delta) {return false;} // deals with dynamic information such as VBR bitrates
67  bool decode_get_dynamic_info_track(file_info & p_out, double & p_timestamp_delta) {return false;} // deals with dynamic information such as track changes in live streams
68  void decode_on_idle(abort_callback & p_abort) {m_file->on_idle(p_abort);}
69 
70  void retag(const file_info & p_info,abort_callback & p_abort) {throw exception_io_unsupported_format();}
71 
72  static bool g_is_our_content_type(const char * p_content_type) {return false;} // match against supported mime types here
73  static bool g_is_our_path(const char * p_path,const char * p_extension) {return stricmp_utf8(p_extension,"raw") == 0;}
74 public:
77 };
78 
80 
81 // Declare .RAW as a supported file type to make it show in "open file" dialog etc.
82 DECLARE_FILE_TYPE("Raw files","*.RAW");
void info_set_bitrate(t_int64 val)
Definition: file_info.h:206
static bool g_is_our_path(const char *p_path, const char *p_extension)
Definition: input_raw.cpp:73
const t_item * get_ptr() const
Definition: array.h:213
Non-multitrack-enabled input factory (helper) - hides multitrack management functions from input impl...
Definition: input_impl.h:334
Interface to container of a chunk of audio data. See audio_chunk_impl for an implementation.
Definition: audio_chunk.h:5
static const t_filesize filesize_invalid
Invalid/unknown file size constant. Also see: t_filesize.
Definition: filesystem.h:16
bool decode_get_dynamic_info(file_info &p_out, double &p_timestamp_delta)
Definition: input_raw.cpp:66
t_input_open_reason
Definition: input_impl.h:1
int SHARED_EXPORT stricmp_utf8(const char *p1, const char *p2)
service_ptr_t< file > m_file
Definition: input_raw.cpp:75
void open(service_ptr_t< file > p_filehint, const char *p_path, t_input_open_reason p_reason, abort_callback &p_abort)
Definition: input_raw.cpp:15
bool decode_run(audio_chunk &p_chunk, abort_callback &p_abort)
Definition: input_raw.cpp:41
bool decode_get_dynamic_info_track(file_info &p_out, double &p_timestamp_delta)
Definition: input_raw.cpp:67
void decode_on_idle(abort_callback &p_abort)
Definition: input_raw.cpp:68
void info_set_int(const char *name, t_int64 value)
Definition: file_info.cpp:221
double samples_to_time(t_uint64 p_samples, t_uint32 p_sample_rate)
Definition: audio_sample.h:37
Main interface class for information about some playable object.
Definition: file_info.h:73
void input_open_file_helper(service_ptr_t< file > &p_file, const char *p_path, t_input_open_reason p_reason, abort_callback &p_abort)
Helper function for input implementation use; ensures that file is open with relevant access mode...
Definition: input.cpp:231
pfc::array_t< t_uint8 > m_buffer
Definition: input_raw.cpp:76
size_t t_size
Definition: int_types.h:48
void decode_initialize(unsigned p_flags, abort_callback &p_abort)
Definition: input_raw.cpp:38
void set_size(t_size p_size)
Definition: array.h:104
bool decode_can_seek()
Definition: input_raw.cpp:65
void get_info(file_info &p_info, abort_callback &p_abort)
Definition: input_raw.cpp:21
t_size info_set(const char *p_name, const char *p_value)
Definition: file_info.h:167
void set_data_fixedpoint(const void *ptr, t_size bytes, unsigned srate, unsigned nch, unsigned bps, unsigned channel_config)
Helper, sets chunk data to contents of specified buffer, using default win32/wav conventions for sign...
Definition: audio_chunk.h:156
t_filestats get_file_stats(abort_callback &p_abort)
Definition: input_raw.cpp:36
void decode_seek(double p_seconds, abort_callback &p_abort)
Definition: input_raw.cpp:54
DECLARE_FILE_TYPE("Raw files","*.RAW")
static unsigned g_guess_channel_config(unsigned count)
Helper function; guesses default channel map for the specified channel count. Returns 0 on failure...
t_uint64 t_filesize
Type used for file size related variables.
Definition: filesystem.h:8
static bool g_is_our_content_type(const char *p_content_type)
Definition: input_raw.cpp:72
t_uint64 time_to_samples(double p_time, t_uint32 p_sample_rate)
Definition: audio_sample.h:33
virtual void set_length(double p_length)=0
Sets audio duration, in seconds. Note that the reported duration should not be assumed to be the exa...
static input_singletrack_factory_t< input_raw > g_input_raw_factory
Definition: input_raw.cpp:79
void retag(const file_info &p_info, abort_callback &p_abort)
Definition: input_raw.cpp:70