comparison foosdk/sdk/foobar2000/foo_sample/input_raw.cpp @ 1:20d02a178406 default tip

*: check in everything else yay
author Paper <paper@tflc.us>
date Mon, 05 Jan 2026 02:15:46 -0500
parents
children
comparison
equal deleted inserted replaced
0:e9bb126753e7 1:20d02a178406
1 #include "stdafx.h"
2
3 enum {
4 raw_bits_per_sample = 16,
5 raw_channels = 2,
6 raw_sample_rate = 44100,
7
8 raw_bytes_per_sample = raw_bits_per_sample / 8,
9 raw_total_sample_width = raw_bytes_per_sample * raw_channels,
10 };
11
12 // Note that input class does *not* implement virtual methods or derive from interface classes.
13 // Our methods get called over input framework templates. See input_singletrack_impl for descriptions of what each method does.
14 // input_stubs just provides stub implementations of mundane methods that are irrelevant for most implementations.
15 class input_raw : public input_stubs {
16 public:
17 void open(service_ptr_t<file> p_filehint,const char * p_path,t_input_open_reason p_reason,abort_callback & p_abort) {
18 if (p_reason == input_open_info_write) throw exception_tagging_unsupported();//our input does not support retagging.
19 m_file = p_filehint;//p_filehint may be null, hence next line
20 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).
21 }
22
23 void get_info(file_info & p_info,abort_callback & p_abort) {
24 t_filesize size = m_file->get_size(p_abort);
25 //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
26 if (size != filesize_invalid) {
27 //file size is known, let's set length
28 p_info.set_length(audio_math::samples_to_time( size / raw_total_sample_width, raw_sample_rate));
29 }
30 //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.
31 p_info.info_set_int("samplerate",raw_sample_rate);
32 p_info.info_set_int("channels",raw_channels);
33 p_info.info_set_int("bitspersample",raw_bits_per_sample);
34
35 // Indicate whether this is a fixedpoint or floatingpoint stream, when using bps >= 32
36 // As 32bit fixedpoint can't be decoded losslessly by fb2k, does not fit in float32 audio_sample.
37 if ( raw_bits_per_sample >= 32 ) p_info.info_set("bitspersample_extra", "fixed-point");
38
39 p_info.info_set("encoding","lossless");
40 p_info.info_set_bitrate((raw_bits_per_sample * raw_channels * raw_sample_rate + 500 /* rounding for bps to kbps*/ ) / 1000 /* bps to kbps */);
41
42 }
43 t_filestats2 get_stats2(unsigned f, abort_callback& a) {return m_file->get_stats2_(f, a);}
44 t_filestats get_file_stats(abort_callback & p_abort) {return m_file->get_stats(p_abort);}
45
46 void decode_initialize(unsigned p_flags,abort_callback & p_abort) {
47 m_file->reopen(p_abort);//equivalent to seek to zero, except it also works on nonseekable streams
48 }
49 bool decode_run(audio_chunk & p_chunk,abort_callback & p_abort) {
50 enum {
51 deltaread = 1024,
52 };
53
54 const size_t deltaReadBytes = deltaread * raw_total_sample_width;
55 // Prepare buffer
56 m_buffer.set_size(deltaReadBytes);
57 // Read bytes
58 size_t got = m_file->read(m_buffer.get_ptr(), deltaReadBytes,p_abort) / raw_total_sample_width;
59
60 // EOF?
61 if (got == 0) return false;
62
63 // This converts the data that we've read to the audio_chunk's internal format, audio_sample (float 32-bit).
64 // audio_sample is the audio data format that all fb2k code works with.
65 p_chunk.set_data_fixedpoint(m_buffer.get_ptr(), got * raw_total_sample_width,raw_sample_rate,raw_channels,raw_bits_per_sample,audio_chunk::g_guess_channel_config(raw_channels));
66
67 //processed successfully, no EOF
68 return true;
69 }
70 void decode_seek(double p_seconds,abort_callback & p_abort) {
71 m_file->ensure_seekable();//throw exceptions if someone called decode_seek() despite of our input having reported itself as nonseekable.
72 // IMPORTANT: convert time to sample offset with proper rounding! audio_math::time_to_samples does this properly for you.
73 t_filesize target = audio_math::time_to_samples(p_seconds,raw_sample_rate) * raw_total_sample_width;
74
75 // 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).
76 t_filesize max = m_file->get_size_ex(p_abort);
77 if (target > max) target = max;//clip seek-past-eof attempts to legal range (next decode_run() call will just signal EOF).
78
79 m_file->seek(target,p_abort);
80 }
81 bool decode_can_seek() {return m_file->can_seek();}
82 bool decode_get_dynamic_info(file_info & p_out, double & p_timestamp_delta) {return false;} // deals with dynamic information such as VBR bitrates
83 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
84 void decode_on_idle(abort_callback & p_abort) {m_file->on_idle(p_abort);}
85
86 // Note that open() already rejects requests to open for tag writing, so these two should never get called.
87 void retag(const file_info & p_info,abort_callback & p_abort) {throw exception_tagging_unsupported();}
88 void remove_tags(abort_callback&) { throw exception_tagging_unsupported(); }
89
90 static bool g_is_our_content_type(const char * p_content_type) {return false;} // match against supported mime types here
91 static bool g_is_our_path(const char * p_path,const char * p_extension) {return stricmp_utf8(p_extension,"raw") == 0;}
92 static const char * g_get_name() { return "foo_sample raw input"; }
93 static const GUID g_get_guid() {
94 // GUID of the decoder. Replace with your own when reusing code.
95 static const GUID I_am_foo_sample_and_this_is_my_decoder_GUID = { 0xd9c01c8d, 0x69c5, 0x4eec,{ 0xa2, 0x1c, 0x1d, 0x14, 0xef, 0x65, 0xbf, 0x8b } };
96 return I_am_foo_sample_and_this_is_my_decoder_GUID;
97 }
98 public:
99 service_ptr_t<file> m_file;
100 pfc::array_t<t_uint8> m_buffer;
101 };
102
103 static input_singletrack_factory_t<input_raw> g_input_raw_factory;
104
105 // Declare .RAW as a supported file type to make it show in "open file" dialog etc.
106 DECLARE_FILE_TYPE("Raw files","*.RAW");