3 #include "library/filesystem.hpp"
4 #include "library/minmax.hpp"
5 #include "library/workthread.hpp"
6 #include "library/serialization.hpp"
7 #include "library/string.hpp"
8 #include "library/ogg.hpp"
9 #include "library/opus-ogg.hpp"
10 #include "library/opus.hpp"
11 #include "core/audioapi.hpp"
12 #include "core/command.hpp"
13 #include "core/dispatch.hpp"
14 #include "core/framerate.hpp"
15 #include "core/inthread.hpp"
16 #include "core/keymapper.hpp"
17 #include "core/settings.hpp"
18 #include "core/misc.hpp"
28 //Farther than this, packets can be fastskipped.
29 #define OPUS_CONVERGE_MAX 5760
30 //Maximum size of PCM output for one packet.
31 #define OPUS_MAX_OUT 5760
33 #define OUTPUT_BLOCK 1440
35 #define OPUS_SAMPLERATE 48000
37 #define OPUS_BLOCK_SIZE 960
38 //Threshold for decoding additional block
39 #define BLOCK_THRESHOLD 1200
40 //Maximum output block size.
41 #define OUTPUT_SIZE (BLOCK_THRESHOLD + OUTPUT_BLOCK)
42 //Amount of microseconds per interation.
43 #define ITERATION_TIME 15000
44 //Opus bitrate to use.
45 #define OPUS_BITRATE 48000
46 //Opus min bitrate to use.
47 #define OPUS_MIN_BITRATE 8000
48 //Opus max bitrate to use.
49 #define OPUS_MAX_BITRATE 255000
50 //Ogg Opus granule rate.
51 #define OGGOPUS_GRANULERATE 48000
52 //Record buffer size threshold divider.
53 #define REC_THRESHOLD_DIV 40
54 //Playback buffer size threshold divider.
55 #define PLAY_THRESHOLD_DIV 30
56 //Special granule position: None.
57 #define GRANULEPOS_NONE 0xFFFFFFFFFFFFFFFFULL
61 class opus_playback_stream
;
63 class stream_collection
;
65 settingvar::variable
<settingvar::model_int
<OPUS_MIN_BITRATE
,OPUS_MAX_BITRATE
>> opus_bitrate(lsnes_vset
,
66 "opus-bitrate", "commentary‣Bitrate", OPUS_BITRATE
);
67 settingvar::variable
<settingvar::model_int
<OPUS_MIN_BITRATE
,OPUS_MAX_BITRATE
>> opus_max_bitrate(lsnes_vset
,
68 "opus-max-bitrate", "commentary‣Max bitrate", OPUS_MAX_BITRATE
);
70 //Recording active flag.
71 volatile bool active_flag
= false;
72 //Last seen frame number.
73 uint64_t last_frame_number
= 0;
76 //Mutex protecting current_time and time_jump.
77 threads::lock time_mutex
;
79 uint64_t current_time
;
80 //Time jump flag. Set if time jump is detected.
81 //If time jump is detected, all current playing streams are stopped, stream locks are cleared and
82 //apropriate streams are restarted. If time jump is false, all unlocked streams coming into range
85 //Lock protecting active_playback_streams.
86 threads::lock active_playback_streams_lock
;
87 //List of streams currently playing.
88 std::list
<opus_playback_stream
*> active_playback_streams
;
89 //The collection of streams.
90 stream_collection
* current_collection
;
91 //Lock protecting current collection.
92 threads::lock current_collection_lock
;
94 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
96 struct bitrate_tracker
98 bitrate_tracker() throw();
100 double get_min() throw();
101 double get_avg() throw();
102 double get_max() throw();
103 double get_length() throw();
104 uint64_t get_bytes() throw();
105 uint64_t get_blocks() throw();
106 void submit(uint32_t bytes
, uint32_t samples
) throw();
115 bitrate_tracker::bitrate_tracker() throw()
120 void bitrate_tracker::reset() throw()
125 minrate
= std::numeric_limits
<uint32_t>::max();
129 double bitrate_tracker::get_min() throw()
131 return blocks
? minrate
/ 1000.0 : 0.0;
134 double bitrate_tracker::get_avg() throw()
136 return samples
? bytes
/ (125.0 * samples
/ OPUS_SAMPLERATE
) : 0.0;
139 double bitrate_tracker::get_max() throw()
141 return blocks
? maxrate
/ 1000.0 : 0.0;
144 double bitrate_tracker::get_length() throw()
146 return 1.0 * samples
/ OPUS_SAMPLERATE
;
149 uint64_t bitrate_tracker::get_bytes() throw()
154 uint64_t bitrate_tracker::get_blocks() throw()
159 void bitrate_tracker::submit(uint32_t _bytes
, uint32_t _samples
) throw()
164 uint32_t irate
= _bytes
* 8 * OPUS_SAMPLERATE
/ OPUS_BLOCK_SIZE
;
165 minrate
= min(minrate
, irate
);
166 maxrate
= max(maxrate
, irate
);
169 std::ostream
& operator<<(std::ostream
& s
, bitrate_tracker
& t
)
171 s
<< t
.get_bytes() << " bytes for " << t
.get_length() << "s (" << t
.get_blocks() << " blocks)"
172 << std::endl
<< "Bitrate (kbps): min: " << t
.get_min() << " avg: " << t
.get_avg() << " max:"
173 << t
.get_max() << std::endl
;
177 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
178 //Information about individual opus packet in stream.
179 struct opus_packetinfo
181 //Length is in units of 1/400th of a second.
182 opus_packetinfo(uint16_t datasize
, uint8_t length
, uint64_t offset
)
184 descriptor
= (offset
& 0xFFFFFFFFFFULL
) | (static_cast<uint64_t>(length
) << 40) |
185 (static_cast<uint64_t>(datasize
) << 48);
187 //Get the data size of the packet.
188 uint16_t size() { return descriptor
>> 48; }
189 //Calculate the length of packet in samples.
190 uint16_t length() { return 120 * ((descriptor
>> 40) & 0xFF); }
191 //Calculate the true offset.
192 uint64_t offset() { return descriptor
& 0xFFFFFFFFFFULL
; }
195 std::vector
<unsigned char> packet(filesystem::ref from_sys
);
200 std::vector
<unsigned char> opus_packetinfo::packet(filesystem::ref from_sys
)
202 std::vector
<unsigned char> ret
;
203 uint64_t off
= offset();
204 uint32_t sz
= size();
205 uint32_t cluster
= off
/ CLUSTER_SIZE
;
206 uint32_t coff
= off
% CLUSTER_SIZE
;
208 size_t r
= from_sys
.read_data(cluster
, coff
, &ret
[0], sz
);
210 throw std::runtime_error("Incomplete read");
214 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
215 //Information about opus stream.
218 //Create new empty stream with specified base time.
219 opus_stream(uint64_t base
, filesystem::ref filesys
);
220 //Read stream with specified base time and specified start clusters.
222 opus_stream(uint64_t base
, filesystem::ref filesys
, uint32_t ctrl_cluster
, uint32_t data_cluster
);
223 //Import a stream with specified base time.
225 opus_stream(uint64_t base
, filesystem::ref filesys
, std::ifstream
& data
,
226 external_stream_format extfmt
);
227 //Delete this stream (also puts a ref)
228 void delete_stream() { deleting
= true; put_ref(); }
231 void export_stream(std::ofstream
& data
, external_stream_format extfmt
);
232 //Get length of specified packet in samples.
233 uint16_t packet_length(uint32_t seqno
)
235 return (seqno
< packets
.size()) ? packets
[seqno
].length() : 0;
237 //Get data of specified packet.
239 std::vector
<unsigned char> packet(uint32_t seqno
)
241 return (seqno
< packets
.size()) ? packets
[seqno
].packet(fs
) : std::vector
<unsigned char>();
243 //Get base time in samples for stream.
244 uint64_t timebase() { return s_timebase
; }
245 //Set base time in samples for stream.
246 void timebase(uint64_t ts
) { s_timebase
= ts
; }
247 //Get length of stream in samples.
250 if(pregap_length
+ postgap_length
> total_len
)
253 return total_len
- pregap_length
- postgap_length
;
255 //Set the pregap length.
256 void set_pregap(uint32_t p
) { pregap_length
= p
; }
257 //Get the pregap length.
258 uint32_t get_pregap() { return pregap_length
; }
259 //Set the postgap length.
260 void set_potsgap(uint32_t p
) { postgap_length
= p
; }
261 //Get the postgap length.
262 uint32_t get_postgap() { return postgap_length
; }
264 void set_gain(int16_t g
) { gain
= g
; }
266 int16_t get_gain() { return gain
; }
268 float get_gain_linear() { return pow(10, gain
/ 20); }
269 //Get number of packets in stream.
270 uint32_t blocks() { return packets
.size(); }
271 //Is this stream locked?
272 bool islocked() { return locked
; }
274 void lock() { locked
= true; }
276 void unlock() { locked
= false; }
277 //Increment reference count.
278 void get_ref() { threads::alock
m(reflock
); refcount
++; }
279 //Decrement reference count, destroying object if it hits zero.
280 void put_ref() { threads::alock
m(reflock
); refcount
--; if(!refcount
) destroy(); }
281 //Add new packet into stream.
282 //Not safe to call simultaneously with packet_length() or packet().
284 void write(uint8_t len
, const unsigned char* payload
, size_t payload_len
);
285 //Write stream trailer.
286 void write_trailier();
288 std::pair
<uint32_t, uint32_t> get_clusters() { return std::make_pair(ctrl_cluster
, data_cluster
); }
290 void export_stream_sox(std::ofstream
& data
);
291 void export_stream_oggopus(std::ofstream
& data
);
292 void import_stream_sox(std::ifstream
& data
);
293 void import_stream_oggopus(std::ifstream
& data
);
295 opus_stream(const opus_stream
&);
296 opus_stream
& operator=(const opus_stream
&);
299 std::vector
<opus_packetinfo
> packets
;
302 uint32_t next_cluster
;
303 uint32_t next_offset
;
304 uint32_t next_mcluster
;
305 uint32_t next_moffset
;
306 uint32_t ctrl_cluster
;
307 uint32_t data_cluster
;
308 uint32_t pregap_length
;
309 uint32_t postgap_length
;
312 threads::lock reflock
;
317 opus_stream::opus_stream(uint64_t base
, filesystem::ref filesys
)
336 opus_stream::opus_stream(uint64_t base
, filesystem::ref filesys
, uint32_t _ctrl_cluster
,
337 uint32_t _data_cluster
)
345 next_cluster
= data_cluster
= _data_cluster
;
346 next_mcluster
= ctrl_cluster
= _ctrl_cluster
;
352 //Read the data buffers.
353 char buf
[CLUSTER_SIZE
];
354 uint32_t last_cluster_seen
= next_mcluster
;
355 uint64_t total_size
= 0;
356 uint64_t total_frames
= 0;
357 bool trailers
= false;
358 bool saved_pointer_valid
= false;
359 uint32_t saved_next_mcluster
= 0;
360 uint32_t saved_next_moffset
= 0;
362 last_cluster_seen
= next_mcluster
;
363 size_t r
= fs
.read_data(next_mcluster
, next_moffset
, buf
, CLUSTER_SIZE
);
365 //The stream ends here.
368 //Find the first unused entry if any.
369 for(unsigned i
= 0; i
< CLUSTER_SIZE
; i
+= 4)
370 if(!buf
[i
+ 3] || trailers
) {
371 //This entry is unused. If the next entry is also unused, that is the end.
372 //Otherwise, there might be stream trailers.
373 if(trailers
&& !buf
[i
+ 3]) {
374 goto out_parsing
; //Ends for real.
377 //Set the trailer flag and continue parsing.
378 //The saved offset must be placed here.
379 saved_next_mcluster
= last_cluster_seen
;
380 saved_next_moffset
= i
;
381 saved_pointer_valid
= true;
385 //This is a trailer entry.
386 if(buf
[i
+ 3] == 2) {
388 pregap_length
= serialization::u32b(buf
+ i
) >> 8;
389 } else if(buf
[i
+ 3] == 3) {
391 postgap_length
= serialization::u32b(buf
+ i
) >> 8;
392 } else if(buf
[i
+ 3] == 4) {
394 gain
= serialization::s16b(buf
+ i
);
397 uint16_t psize
= serialization::u16b(buf
+ i
);
398 uint8_t plen
= serialization::u8b(buf
+ i
+ 2);
400 total_len
+= 120 * plen
;
401 opus_packetinfo
p(psize
, plen
, 1ULL * next_cluster
* CLUSTER_SIZE
+
403 size_t r2
= fs
.skip_data(next_cluster
, next_offset
, psize
);
405 throw std::runtime_error("Incomplete data stream");
406 packets
.push_back(p
);
411 //If saved pointer is valid, restore to that.
412 if(saved_pointer_valid
) {
413 next_mcluster
= saved_next_mcluster
;
414 next_moffset
= saved_next_moffset
;
418 opus_stream::opus_stream(uint64_t base
, filesystem::ref filesys
, std::ifstream
& data
,
419 external_stream_format extfmt
)
436 if(extfmt
== EXTFMT_OGGOPUS
)
437 import_stream_oggopus(data
);
438 else if(extfmt
== EXTFMT_SOX
)
439 import_stream_sox(data
);
442 void opus_stream::import_stream_oggopus(std::ifstream
& data
)
444 ogg::stream_reader_iostreams
reader(data
);
445 reader
.set_errors_to(messages
);
446 struct opus::ogg_header h
;
447 struct opus::ogg_tags t
;
449 ogg::demuxer
d(messages
);
452 uint64_t datalen
= 0;
453 uint64_t last_datalen
= 0;
454 uint64_t last_granulepos
= 0;
458 if(!d
.wants_packet_out()) {
459 if(!reader
.get_page(page
))
466 case 0: //Not locked.
469 throw std::runtime_error("Multistream OggOpus streams are not "
471 state
= 1; //Expecting comment.
472 pregap_length
= h
.preskip
;
475 case 1: //Expecting comment.
477 state
= 2; //Data page.
479 throw std::runtime_error("Empty OggOpus stream");
483 const std::vector
<uint8_t>& pkt
= p
.get_vector();
484 uint8_t tcnt
= opus::packet_tick_count(&pkt
[0], pkt
.size());
486 write(tcnt
, &pkt
[0], pkt
.size());
487 datalen
+= tcnt
* 120;
489 if(p
.get_last_page()) {
490 uint64_t samples
= p
.get_granulepos() - last_granulepos
;
491 if(samples
> p
.get_granulepos())
493 uint64_t rsamples
= datalen
- last_datalen
;
494 if((samples
> rsamples
&& state
== 3) || (samples
<
495 rsamples
&& !p
.get_on_eos_page()))
496 messages
<< "Warning: Granulepos says there are "
497 << samples
<< " samples, found " << rsamples
499 last_datalen
= datalen
;
500 last_granulepos
= p
.get_granulepos();
501 if(p
.get_on_eos_page()) {
502 if(samples
< rsamples
)
503 postgap_length
= rsamples
- samples
;
514 throw std::runtime_error("No OggOpus stream found");
516 throw std::runtime_error("Oggopus stream missing required tags pages");
517 if(state
== 2 || state
== 3)
518 messages
<< "Warning: Incomplete Oggopus stream." << std::endl
;
519 if(datalen
<= pregap_length
)
520 throw std::runtime_error("Stream too short (entiere pregap not present)");
523 if(ctrl_cluster
) fs
.free_cluster_chain(ctrl_cluster
);
524 if(data_cluster
) fs
.free_cluster_chain(data_cluster
);
529 void opus_stream::import_stream_sox(std::ifstream
& data
)
531 bitrate_tracker brtrack
;
532 unsigned char tmpi
[65536];
533 float tmp
[OPUS_MAX_OUT
];
535 data
.read(header
, 32);
537 throw std::runtime_error("Can't read .sox header");
538 if(serialization::u32l(header
+ 0) != 0x586F532EULL
)
539 throw std::runtime_error("Bad .sox header magic");
540 if(serialization::u8b(header
+ 4) > 28)
541 data
.read(header
+ 32, serialization::u8b(header
+ 4) - 28);
543 throw std::runtime_error("Can't read .sox header");
544 if(serialization::u64l(header
+ 16) != 4676829883349860352ULL)
545 throw std::runtime_error("Bad .sox sampling rate");
546 if(serialization::u32l(header
+ 24) != 1)
547 throw std::runtime_error("Only mono streams are supported");
548 uint64_t samples
= serialization::u64l(header
+ 8);
549 opus::encoder
enc(opus::samplerate::r48k
, false, opus::application::voice
);
550 enc
.ctl(opus::bitrate(opus_bitrate
.get()));
551 int32_t pregap
= enc
.ctl(opus::lookahead
);
552 pregap_length
= pregap
;
553 for(uint64_t i
= 0; i
< samples
+ pregap
; i
+= OPUS_BLOCK_SIZE
) {
554 size_t bs
= OPUS_BLOCK_SIZE
;
555 if(i
+ bs
> samples
+ pregap
)
556 bs
= samples
+ pregap
- i
;
557 //We have to read zero bytes after the end of stream.
558 size_t readable
= bs
;
559 if(readable
+ i
> samples
)
560 readable
= max(samples
, i
) - i
;
562 data
.read(reinterpret_cast<char*>(tmpi
), 4 * readable
);
564 memset(tmpi
+ 4 * readable
, 0, 4 * (bs
- readable
));
566 if(ctrl_cluster
) fs
.free_cluster_chain(ctrl_cluster
);
567 if(data_cluster
) fs
.free_cluster_chain(data_cluster
);
568 throw std::runtime_error("Can't read .sox data");
570 for(size_t j
= 0; j
< bs
; j
++)
571 tmp
[j
] = static_cast<float>(serialization::s32l(tmpi
+ 4 * j
)) / 268435456;
572 if(bs
< OPUS_BLOCK_SIZE
)
573 postgap_length
= OPUS_BLOCK_SIZE
- bs
;
574 for(size_t j
= bs
; j
< OPUS_BLOCK_SIZE
; j
++)
577 const size_t opus_out_max2
= opus_max_bitrate
.get() * OPUS_BLOCK_SIZE
/ 384000;
578 size_t r
= enc
.encode(tmp
, OPUS_BLOCK_SIZE
, tmpi
, opus_out_max2
);
579 write(OPUS_BLOCK_SIZE
/ 120, tmpi
, r
);
580 brtrack
.submit(r
, bs
);
581 } catch(std::exception
& e
) {
582 if(ctrl_cluster
) fs
.free_cluster_chain(ctrl_cluster
);
583 if(data_cluster
) fs
.free_cluster_chain(data_cluster
);
584 (stringfmt() << "Error encoding opus packet: " << e
.what()).throwex();
587 messages
<< "Imported stream: " << brtrack
;
591 if(ctrl_cluster
) fs
.free_cluster_chain(ctrl_cluster
);
592 if(data_cluster
) fs
.free_cluster_chain(data_cluster
);
597 void opus_stream::destroy()
600 //We catch the errors and print em, because otherwise put_ref could throw, which would
603 fs
.free_cluster_chain(ctrl_cluster
);
604 } catch(std::exception
& e
) {
605 messages
<< "Failed to delete stream control file: " << e
.what();
608 fs
.free_cluster_chain(data_cluster
);
609 } catch(std::exception
& e
) {
610 messages
<< "Failed to delete stream data file: " << e
.what();
616 void opus_stream::export_stream_oggopus(std::ofstream
& data
)
619 throw std::runtime_error("Empty oggopus stream is not valid");
620 opus::ogg_header header
;
622 ogg::stream_writer_iostreams
writer(data
);
623 unsigned stream_id
= 1;
624 uint64_t true_granule
= 0;
629 header
.preskip
= pregap_length
;
630 header
.rate
= OPUS_SAMPLERATE
;
632 header
.map_family
= 0;
635 header
.chanmap
[0] = 0;
636 memset(header
.chanmap
+ 1, 255, 254);
637 tags
.vendor
= "unknown";
638 tags
.comments
.push_back((stringfmt() << "ENCODER=lsnes rr" + lsnes_version
).str());
639 tags
.comments
.push_back((stringfmt() << "LSNES_STREAM_TS=" << s_timebase
).str());
641 struct ogg::page hpage
= header
.serialize();
642 hpage
.set_stream(stream_id
);
643 writer
.put_page(hpage
);
644 seq
= tags
.serialize([&writer
](const ogg::page
& p
) { writer
.put_page(p
); }, stream_id
);
646 struct ogg::page ppage
;
647 ogg::muxer
mux(stream_id
, seq
);
648 for(size_t i
= 0; i
< packets
.size(); i
++) {
649 std::vector
<unsigned char> p
;
652 } catch(std::exception
& e
) {
653 (stringfmt() << "Error reading opus packet: " << e
.what()).throwex();
656 (stringfmt() << "Empty Opus packet is not valid").throwex();
657 uint32_t samples
= static_cast<uint32_t>(opus::packet_tick_count(&p
[0], p
.size())) * 120;
658 if(i
+ 1 < packets
.size())
659 true_granule
+= samples
;
661 true_granule
= max(true_granule
, true_granule
+ samples
- postgap_length
);
662 if(!mux
.wants_packet_in() || !mux
.packet_fits(p
.size()))
663 while(mux
.has_page_out()) {
665 writer
.put_page(ppage
);
667 mux
.packet_in(p
, true_granule
);
670 while(mux
.has_page_out()) {
672 writer
.put_page(ppage
);
676 void opus_stream::export_stream_sox(std::ofstream
& data
)
678 opus::decoder
dec(opus::samplerate::r48k
, false);
679 std::vector
<unsigned char> p
;
680 float tmp
[OPUS_MAX_OUT
];
682 serialization::u64l(header
, 0x1C586F532EULL
); //Magic and header size.
683 serialization::u64l(header
+ 16, 4676829883349860352ULL); //Sampling rate.
684 serialization::u32l(header
+ 24, 1);
686 uint32_t lookahead_thrown
= 0;
687 data
.write(header
, 32);
689 throw std::runtime_error("Error writing PCM data.");
690 float lgain
= get_gain_linear();
691 for(size_t i
= 0; i
< packets
.size(); i
++) {
692 char blank
[4] = {0, 0, 0, 0};
694 uint32_t pregap_throw
= 0;
695 uint32_t postgap_throw
= 0;
696 std::vector
<unsigned char> p
= packet(i
);
697 uint32_t len
= packet_length(i
);
698 dec
.decode(&p
[0], p
.size(), tmp
, OPUS_MAX_OUT
);
699 bool is_last
= (i
== packets
.size() - 1);
700 if(lookahead_thrown
< pregap_length
) {
701 //We haven't yet thrown the full pregap. Throw some.
702 uint32_t maxthrow
= pregap_length
- lookahead_thrown
;
703 pregap_throw
= min(len
, maxthrow
);
704 lookahead_thrown
+= pregap_length
;
707 postgap_throw
= min(len
- pregap_throw
, postgap_length
);
708 tlen
+= (len
- pregap_throw
- postgap_throw
);
709 for(uint32_t j
= pregap_throw
; j
< len
- postgap_throw
; j
++) {
710 int32_t s
= (int32_t)(tmp
[j
] * lgain
* 268435456.0);
711 serialization::s32l(blank
, s
);
712 data
.write(blank
, 4);
714 throw std::runtime_error("Error writing PCM data.");
716 } catch(std::exception
& e
) {
717 (stringfmt() << "Error decoding opus packet: " << e
.what()).throwex();
720 data
.seekp(0, std::ios_base::beg
);
721 serialization::u64l(header
+ 8, tlen
);
722 data
.write(header
, 32);
724 throw std::runtime_error("Error writing PCM data.");
728 void opus_stream::export_stream(std::ofstream
& data
, external_stream_format extfmt
)
730 if(extfmt
== EXTFMT_OGGOPUS
)
731 export_stream_oggopus(data
);
732 else if(extfmt
== EXTFMT_SOX
)
733 export_stream_sox(data
);
736 void opus_stream::write(uint8_t len
, const unsigned char* payload
, size_t payload_len
)
740 uint32_t used_cluster
, used_offset
;
741 uint32_t used_mcluster
, used_moffset
;
743 next_cluster
= data_cluster
= fs
.allocate_cluster();
745 next_mcluster
= ctrl_cluster
= fs
.allocate_cluster();
746 serialization::u16b(descriptor
, payload_len
);
747 serialization::u8b(descriptor
+ 2, len
);
748 serialization::u8b(descriptor
+ 3, 1);
749 fs
.write_data(next_cluster
, next_offset
, payload
, payload_len
, used_cluster
, used_offset
);
750 fs
.write_data(next_mcluster
, next_moffset
, descriptor
, 4, used_mcluster
, used_moffset
);
751 uint64_t off
= static_cast<uint64_t>(used_cluster
) * CLUSTER_SIZE
+ used_offset
;
752 opus_packetinfo
p(payload_len
, len
, off
);
753 total_len
+= p
.length();
754 packets
.push_back(p
);
755 } catch(std::exception
& e
) {
756 (stringfmt() << "Can't write opus packet: " << e
.what()).throwex();
760 void opus_stream::write_trailier()
764 uint32_t used_mcluster
, used_moffset
;
765 //The allocation must be done for real.
767 next_mcluster
= ctrl_cluster
= fs
.allocate_cluster();
768 //But the write must not update the pointers..
769 uint32_t tmp_mcluster
= next_mcluster
;
770 uint32_t tmp_moffset
= next_moffset
;
771 serialization::u32b(descriptor
, 0);
772 serialization::u32b(descriptor
+ 4, (pregap_length
<< 8) | 0x02);
773 serialization::u32b(descriptor
+ 8, (postgap_length
<< 8) | 0x03);
774 serialization::s16b(descriptor
+ 12, gain
);
775 serialization::u16b(descriptor
+ 14, 0x0004);
776 fs
.write_data(tmp_mcluster
, tmp_moffset
, descriptor
, 16, used_mcluster
, used_moffset
);
777 } catch(std::exception
& e
) {
778 (stringfmt() << "Can't write stream trailer: " << e
.what()).throwex();
783 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
784 //Playing opus stream.
785 struct opus_playback_stream
787 //Create a new playing stream from given opus stream.
788 opus_playback_stream(opus_stream
& data
);
789 //Destroy playing opus stream.
790 ~opus_playback_stream();
791 //Read samples from stream.
793 void read(float* data
, size_t samples
);
794 //Skip samples from stream.
796 void skip(uint64_t samples
);
797 //Has the stream already ended?
800 opus_playback_stream(const opus_playback_stream
&);
801 opus_playback_stream
& operator=(const opus_playback_stream
&);
804 float output
[OPUS_MAX_OUT
];
805 unsigned output_left
;
806 uint32_t pregap_thrown
;
808 opus::decoder
* decoder
;
814 opus_playback_stream::opus_playback_stream(opus_stream
& data
)
822 postgap_thrown
= false;
823 blocks
= stream
.blocks();
824 decoder
= new opus::decoder(opus::samplerate::r48k
, false);
826 throw std::bad_alloc();
829 opus_playback_stream::~opus_playback_stream()
831 //No, we don't unlock the stream.
836 bool opus_playback_stream::eof()
838 return (next_block
>= blocks
&& !output_left
);
841 void opus_playback_stream::decode_block()
843 if(next_block
>= blocks
)
845 if(output_left
>= OPUS_MAX_OUT
)
847 unsigned plen
= stream
.packet_length(next_block
);
848 if(plen
+ output_left
> OPUS_MAX_OUT
)
850 std::vector
<unsigned char> pdata
= stream
.packet(next_block
);
852 size_t c
= decoder
->decode(&pdata
[0], pdata
.size(), output
+ output_left
,
853 OPUS_MAX_OUT
- output_left
);
854 output_left
= min(output_left
+ c
, static_cast<size_t>(OPUS_MAX_OUT
));
856 //Bad packet, insert silence.
857 for(unsigned i
= 0; i
< plen
; i
++)
858 output
[output_left
++] = 0;
860 //Throw the pregap away if needed.
861 if(pregap_thrown
< stream
.get_pregap()) {
862 uint32_t throw_amt
= min(stream
.get_pregap() - pregap_thrown
, (uint32_t)output_left
);
863 if(throw_amt
&& throw_amt
< output_left
)
864 memmove(output
, output
+ throw_amt
, (output_left
- throw_amt
) * sizeof(float));
865 output_left
-= throw_amt
;
866 pregap_thrown
+= throw_amt
;
871 void opus_playback_stream::read(float* data
, size_t samples
)
873 float lgain
= stream
.get_gain_linear();
876 if(next_block
>= blocks
&& !postgap_thrown
) {
877 //This is the final packet. Throw away postgap samples at the end.
878 uint32_t thrown
= min(stream
.get_postgap(), (uint32_t)output_left
);
879 output_left
-= thrown
;
880 postgap_thrown
= true;
882 if(next_block
>= blocks
&& !output_left
) {
883 //Zerofill remainder.
884 for(size_t i
= 0; i
< samples
; i
++)
888 unsigned maxcopy
= min(static_cast<unsigned>(samples
), output_left
);
890 memcpy(data
, output
, maxcopy
* sizeof(float));
891 for(size_t i
= 0; i
< maxcopy
; i
++)
894 if(maxcopy
< output_left
&& maxcopy
)
895 memmove(output
, output
+ maxcopy
, (output_left
- maxcopy
) * sizeof(float));
896 output_left
-= maxcopy
;
902 void opus_playback_stream::skip(uint64_t samples
)
904 //Adjust for preskip and declare all preskip already thrown away.
905 pregap_thrown
= stream
.get_pregap();
906 samples
+= pregap_thrown
;
907 postgap_thrown
= false;
908 //First, skip inside decoded samples.
909 if(samples
< output_left
) {
910 //Skipping less than amount in output buffer. Just discard from output buffer and try
911 //to decode a new block.
912 memmove(output
, output
+ samples
, (output_left
- samples
) * sizeof(float));
913 output_left
-= samples
;
917 //Skipping at least the amount of samples in output buffer. First, blank the output buffer
918 //and count those towards samples discarded.
919 samples
-= output_left
;
922 //While number of samples is so great that adequate convergence period can be ensured without
923 //decoding this packet, just skip the samples from the packet.
924 while(samples
> OPUS_CONVERGE_MAX
) {
925 samples
-= stream
.packet_length(next_block
++);
927 if(next_block
>= blocks
)
930 //Okay, we are near the point. Start decoding packets.
934 if(next_block
>= blocks
&& !output_left
)
936 //Skip as many samples as possible.
937 unsigned maxskip
= min(static_cast<unsigned>(samples
), output_left
);
938 if(maxskip
< output_left
)
939 memmove(output
, output
+ maxskip
, (output_left
- maxskip
) * sizeof(float));
940 output_left
-= maxskip
;
943 //Just to be nice, decode a extra block.
948 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
949 //Collection of streams.
950 struct stream_collection
953 //Create a new collection.
955 stream_collection(filesystem::ref filesys
);
956 //Destroy a collection. All streams are destroyed but not deleted.
957 ~stream_collection();
958 //Get list of streams active at given point.
959 std::list
<uint64_t> streams_at(uint64_t point
);
960 //Add a stream into collection.
962 uint64_t add_stream(opus_stream
& stream
);
963 //Get the filesystem this collection is for.
964 filesystem::ref
get_filesystem() { return fs
; }
965 //Unlock all streams in collection.
967 //Get stream with given index (NULL if not found).
968 opus_stream
* get_stream(uint64_t index
)
970 threads::alock
m(mlock
);
971 if(streams
.count(index
)) {
972 streams
[index
]->get_ref();
973 return streams
[index
];
979 void delete_stream(uint64_t index
);
980 //Alter stream timebase.
982 void alter_stream_timebase(uint64_t index
, uint64_t newts
);
984 void alter_stream_gain(uint64_t index
, uint16_t newgain
);
985 //Enumerate all valid stream indices, in time order.
986 std::list
<uint64_t> all_streams();
987 //Export the entiere superstream.
989 void export_superstream(std::ofstream
& out
);
993 unsigned next_stream
;
995 std::set
<uint64_t> free_indices
;
996 std::map
<uint64_t, uint64_t> entries
;
997 std::multimap
<uint64_t, uint64_t> streams_by_time
;
998 //FIXME: Something more efficient.
999 std::map
<uint64_t, opus_stream
*> streams
;
1002 stream_collection::stream_collection(filesystem::ref filesys
)
1007 //The stream index table is in cluster 2.
1008 uint32_t next_cluster
= 2;
1009 uint32_t next_offset
= 0;
1014 size_t r
= fs
.read_data(next_cluster
, next_offset
, buffer
, 16);
1017 uint64_t timebase
= serialization::u64b(buffer
);
1018 uint32_t ctrl_cluster
= serialization::u32b(buffer
+ 8);
1019 uint32_t data_cluster
= serialization::u32b(buffer
+ 12);
1021 opus_stream
* x
= new opus_stream(timebase
, fs
, ctrl_cluster
, data_cluster
);
1022 entries
[next_index
] = i
;
1023 streams_by_time
.insert(std::make_pair(timebase
, next_index
));
1024 streams
[next_index
++] = x
;
1026 free_indices
.insert(i
);
1029 } catch(std::exception
& e
) {
1030 for(auto i
: streams
)
1031 i
.second
->put_ref();
1032 (stringfmt() << "Failed to parse LSVS: " << e
.what()).throwex();
1036 stream_collection::~stream_collection()
1038 threads::alock
m(mlock
);
1039 for(auto i
: streams
)
1040 i
.second
->put_ref();
1044 std::list
<uint64_t> stream_collection::streams_at(uint64_t point
)
1046 threads::alock
m(mlock
);
1047 std::list
<uint64_t> s
;
1048 for(auto i
: streams
) {
1049 uint64_t start
= i
.second
->timebase();
1050 uint64_t end
= start
+ i
.second
->length();
1051 if(point
>= start
&& point
< end
) {
1052 i
.second
->get_ref();
1053 s
.push_back(i
.first
);
1059 uint64_t stream_collection::add_stream(opus_stream
& stream
)
1063 threads::alock
m(mlock
);
1064 //Lock the added stream so it doesn't start playing back immediately.
1067 streams
[idx
] = &stream
;
1069 serialization::u64b(buffer
, stream
.timebase());
1070 auto r
= stream
.get_clusters();
1071 serialization::u32b(buffer
+ 8, r
.first
);
1072 serialization::u32b(buffer
+ 12, r
.second
);
1073 uint64_t entry_number
= 0;
1074 if(free_indices
.empty())
1075 entry_number
= next_stream
++;
1077 entry_number
= *free_indices
.begin();
1078 free_indices
.erase(entry_number
);
1080 uint32_t write_cluster
= 2;
1081 uint32_t write_offset
= 0;
1082 uint32_t dummy1
, dummy2
;
1083 fs
.skip_data(write_cluster
, write_offset
, 16 * entry_number
);
1084 fs
.write_data(write_cluster
, write_offset
, buffer
, 16, dummy1
, dummy2
);
1085 streams_by_time
.insert(std::make_pair(stream
.timebase(), idx
));
1086 entries
[idx
] = entry_number
;
1088 } catch(std::exception
& e
) {
1089 (stringfmt() << "Failed to add stream: " << e
.what()).throwex();
1094 void stream_collection::unlock_all()
1096 threads::alock
m(mlock
);
1097 for(auto i
: streams
)
1101 void stream_collection::delete_stream(uint64_t index
)
1103 threads::alock
m(mlock
);
1104 if(!entries
.count(index
))
1106 uint64_t entry_number
= entries
[index
];
1107 uint32_t write_cluster
= 2;
1108 uint32_t write_offset
= 0;
1109 uint32_t dummy1
, dummy2
;
1110 char buffer
[16] = {0};
1111 fs
.skip_data(write_cluster
, write_offset
, 16 * entry_number
);
1112 fs
.write_data(write_cluster
, write_offset
, buffer
, 16, dummy1
, dummy2
);
1113 auto itr
= streams_by_time
.lower_bound(streams
[index
]->timebase());
1114 auto itr2
= streams_by_time
.upper_bound(streams
[index
]->timebase());
1115 for(auto x
= itr
; x
!= itr2
; x
++)
1116 if(x
->second
== index
) {
1117 streams_by_time
.erase(x
);
1120 streams
[index
]->delete_stream();
1121 streams
.erase(index
);
1124 void stream_collection::alter_stream_timebase(uint64_t index
, uint64_t newts
)
1127 threads::alock
m(mlock
);
1128 if(!streams
.count(index
))
1130 if(entries
.count(index
)) {
1132 uint32_t write_cluster
= 2;
1133 uint32_t write_offset
= 0;
1134 uint32_t dummy1
, dummy2
;
1135 serialization::u64b(buffer
, newts
);
1136 fs
.skip_data(write_cluster
, write_offset
, 16 * entries
[index
]);
1137 fs
.write_data(write_cluster
, write_offset
, buffer
, 8, dummy1
, dummy2
);
1139 auto itr
= streams_by_time
.lower_bound(streams
[index
]->timebase());
1140 auto itr2
= streams_by_time
.upper_bound(streams
[index
]->timebase());
1141 for(auto x
= itr
; x
!= itr2
; x
++)
1142 if(x
->second
== index
) {
1143 streams_by_time
.erase(x
);
1146 streams
[index
]->timebase(newts
);
1147 streams_by_time
.insert(std::make_pair(newts
, index
));
1148 } catch(std::exception
& e
) {
1149 (stringfmt() << "Failed to alter stream timebase: " << e
.what()).throwex();
1153 void stream_collection::alter_stream_gain(uint64_t index
, uint16_t newgain
)
1156 threads::alock
m(mlock
);
1157 if(!streams
.count(index
))
1159 streams
[index
]->set_gain(newgain
);
1160 streams
[index
]->write_trailier();
1161 } catch(std::exception
& e
) {
1162 (stringfmt() << "Failed to alter stream gain: " << e
.what()).throwex();
1166 std::list
<uint64_t> stream_collection::all_streams()
1168 threads::alock
m(mlock
);
1169 std::list
<uint64_t> s
;
1170 for(auto i
: streams_by_time
)
1171 s
.push_back(i
.second
);
1175 void stream_collection::export_superstream(std::ofstream
& out
)
1177 std::list
<uint64_t> slist
= all_streams();
1178 //Find the total length of superstream.
1180 for(auto i
: slist
) {
1181 opus_stream
* s
= get_stream(i
);
1183 len
= max(len
, s
->timebase() + s
->length());
1188 serialization::u64l(header
, 0x1C586F532EULL
); //Magic and header size.
1189 serialization::u64l(header
+ 8, len
);
1190 serialization::u64l(header
+ 16, 4676829883349860352ULL); //Sampling rate.
1191 serialization::u64l(header
+ 24, 1);
1192 out
.write(header
, 32);
1194 throw std::runtime_error("Error writing PCM output");
1196 //Find the first valid stream.
1197 auto next_i
= slist
.begin();
1198 opus_stream
* next_stream
= NULL
;
1199 while(next_i
!= slist
.end()) {
1200 next_stream
= get_stream(*next_i
);
1206 next_ts
= next_stream
? next_stream
->timebase() : len
;
1208 std::list
<opus_playback_stream
*> active
;
1210 for(uint64_t s
= 0; s
< len
;) {
1212 active
.push_back(new opus_playback_stream(*next_stream
));
1213 next_stream
->put_ref();
1215 while(next_i
!= slist
.end()) {
1216 next_stream
= get_stream(*next_i
);
1220 uint64_t next_ts
= next_stream
->timebase();
1223 //Okay, this starts too...
1224 active
.push_back(new opus_playback_stream(*next_stream
));
1225 next_stream
->put_ref();
1228 next_ts
= next_stream
? next_stream
->timebase() : len
;
1230 uint64_t maxsamples
= min(next_ts
- s
, static_cast<uint64_t>(OUTPUT_BLOCK
));
1231 maxsamples
= min(maxsamples
, len
- s
);
1232 char outbuf
[4 * OUTPUT_BLOCK
];
1233 float buf1
[OUTPUT_BLOCK
];
1234 float buf2
[OUTPUT_BLOCK
];
1235 for(size_t t
= 0; t
< maxsamples
; t
++)
1237 for(auto t
: active
) {
1238 t
->read(buf2
, maxsamples
);
1239 for(size_t u
= 0; u
< maxsamples
; u
++)
1242 for(auto t
= active
.begin(); t
!= active
.end();) {
1247 active
.erase(todel
);
1251 for(size_t t
= 0; t
< maxsamples
; t
++)
1252 serialization::s32l(outbuf
+ 4 * t
, buf1
[t
] * 268435456);
1253 out
.write(outbuf
, 4 * maxsamples
);
1255 throw std::runtime_error("Failed to write PCM");
1258 } catch(std::exception
& e
) {
1259 (stringfmt() << "Failed to export PCM: " << e
.what()).throwex();
1261 for(auto t
= active
.begin(); t
!= active
.end();) {
1266 active
.erase(todelete
);
1272 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1273 void start_management_stream(opus_stream
& s
)
1275 opus_playback_stream
* p
= new opus_playback_stream(s
);
1276 threads::alock
m(active_playback_streams_lock
);
1277 active_playback_streams
.push_back(p
);
1280 void advance_time(uint64_t newtime
)
1282 threads::alock
m2(current_collection_lock
);
1283 if(!current_collection
) {
1285 threads::alock
m(active_playback_streams_lock
);
1286 for(auto i
: active_playback_streams
)
1288 active_playback_streams
.clear();
1291 std::list
<uint64_t> sactive
= current_collection
->streams_at(newtime
);
1292 for(auto j
: sactive
) {
1293 opus_stream
* i
= current_collection
->get_stream(j
);
1296 //Don't play locked streams in order to avoid double playing.
1297 threads::alock
m(active_playback_streams_lock
);
1300 active_playback_streams
.push_back(new opus_playback_stream(*i
));
1301 } catch(std::exception
& e
) {
1302 messages
<< "Can't start stream: " << e
.what() << std::endl
;
1308 void jump_time(uint64_t newtime
)
1310 threads::alock
m2(current_collection_lock
);
1311 if(!current_collection
) {
1313 threads::alock
m(active_playback_streams_lock
);
1314 for(auto i
: active_playback_streams
)
1316 active_playback_streams
.clear();
1319 //Close all currently playing streams.
1321 threads::alock
m(active_playback_streams_lock
);
1322 for(auto i
: active_playback_streams
)
1324 active_playback_streams
.clear();
1326 //Unlock all streams, so they will play.
1327 current_collection
->unlock_all();
1328 //Reopen all streams that should be open (with seeking)
1329 std::list
<uint64_t> sactive
= current_collection
->streams_at(newtime
);
1330 for(auto j
: sactive
) {
1331 opus_stream
* i
= current_collection
->get_stream(j
);
1334 //No need to check for locks, because we just busted all of those.
1335 uint64_t p
= newtime
- i
->timebase();
1336 opus_playback_stream
* s
;
1338 s
= new opus_playback_stream(*i
);
1339 } catch(std::exception
& e
) {
1340 messages
<< "Can't start stream: " << e
.what() << std::endl
;
1347 threads::alock
m(active_playback_streams_lock
);
1348 active_playback_streams
.push_back(s
);
1353 void do_resample(audioapi_resampler
& r
, float* srcbuf
, size_t& srcuse
, float* dstbuf
, size_t& dstuse
,
1354 size_t dstmax
, double ratio
)
1356 if(srcuse
== 0 || dstuse
>= dstmax
)
1359 size_t in_u
= srcuse
;
1360 float* out
= dstbuf
+ dstuse
;
1361 size_t out_u
= dstmax
- dstuse
;
1362 r
.resample(in
, in_u
, out
, out_u
, ratio
, false);
1363 size_t offset
= in
- srcbuf
;
1365 memmove(srcbuf
, srcbuf
+ offset
, sizeof(float) * (srcuse
- offset
));
1367 dstuse
= dstmax
- out_u
;
1370 //Drain the input buffer.
1373 while(audioapi_voice_r_status() > 0) {
1375 unsigned size
= min(audioapi_voice_r_status(), 256u);
1376 audioapi_record_voice(buf
, size
);
1380 //Read the input buffer.
1381 void read_input(float* buf
, size_t& use
, size_t maxuse
)
1383 size_t rleft
= audioapi_voice_r_status();
1384 unsigned toread
= min(rleft
, max(maxuse
, use
) - use
);
1386 audioapi_record_voice(buf
+ use
, toread
);
1391 //Compress Opus block.
1392 void compress_opus_block(opus::encoder
& e
, float* buf
, size_t& use
, opus_stream
& active_stream
,
1393 bitrate_tracker
& brtrack
)
1395 const size_t opus_out_max
= 1276;
1396 unsigned char opus_output
[opus_out_max
];
1407 return; //No valid data to compress.
1408 const size_t opus_out_max2
= opus_max_bitrate
.get() * cblock
/ 384000;
1410 size_t c
= e
.encode(buf
, cblock
, opus_output
, opus_out_max2
);
1411 //Successfully compressed a block.
1412 size_t opus_output_len
= c
;
1413 brtrack
.submit(c
, cblock
);
1415 active_stream
.write(cblock
/ 120, opus_output
, opus_output_len
);
1416 } catch(std::exception
& e
) {
1417 messages
<< "Error writing data: " << e
.what() << std::endl
;
1419 } catch(std::exception
& e
) {
1420 messages
<< "Opus encoder error: " << e
.what() << std::endl
;
1427 uint64_t sampletime
;
1430 threads::alock
m(time_mutex
);
1431 sampletime
= current_time
;
1432 jumping
= time_jump
;
1436 jump_time(sampletime
);
1438 advance_time(sampletime
);
1441 void decompress_active_streams(float* out
, size_t& use
)
1444 use
+= OUTPUT_BLOCK
;
1445 for(unsigned i
= 0; i
< OUTPUT_BLOCK
; i
++)
1447 //Do it this way to minimize the amount of time playback streams lock
1449 std::list
<opus_playback_stream
*> stmp
;
1451 threads::alock
m(active_playback_streams_lock
);
1452 stmp
= active_playback_streams
;
1454 std::set
<opus_playback_stream
*> toerase
;
1455 for(auto i
: stmp
) {
1456 float tmp
[OUTPUT_BLOCK
];
1458 i
->read(tmp
, OUTPUT_BLOCK
);
1459 } catch(std::exception
& e
) {
1460 messages
<< "Failed to decompress: " << e
.what() << std::endl
;
1461 for(unsigned j
= 0; j
< OUTPUT_BLOCK
; j
++)
1464 for(unsigned j
= 0; j
< OUTPUT_BLOCK
; j
++)
1465 out
[j
+ base
] += tmp
[j
];
1470 threads::alock
m(active_playback_streams_lock
);
1471 for(auto i
= active_playback_streams
.begin(); i
!= active_playback_streams
.end();) {
1472 if(toerase
.count(*i
)) {
1476 active_playback_streams
.erase(toerase
);
1483 void handle_tangent_positive_edge(opus::encoder
& e
, opus_stream
*& active_stream
, bitrate_tracker
& brtrack
)
1485 threads::alock
m2(current_collection_lock
);
1486 if(!current_collection
)
1490 e
.ctl(opus::bitrate(opus_bitrate
.get()));
1494 threads::alock
m(time_mutex
);
1495 ctime
= current_time
;
1497 active_stream
= NULL
;
1498 active_stream
= new opus_stream(ctime
, current_collection
->get_filesystem());
1499 int32_t pregap
= e
.ctl(opus::lookahead
);
1500 active_stream
->set_pregap(pregap
);
1501 } catch(std::exception
& e
) {
1502 messages
<< "Can't start stream: " << e
.what() << std::endl
;
1505 messages
<< "Tangent enaged." << std::endl
;
1508 void handle_tangent_negative_edge(opus_stream
*& active_stream
, bitrate_tracker
& brtrack
)
1510 threads::alock
m2(current_collection_lock
);
1511 messages
<< "Tangent disenaged: " << brtrack
;
1513 active_stream
->write_trailier();
1514 } catch(std::exception
& e
) {
1515 messages
<< e
.what() << std::endl
;
1517 if(current_collection
) {
1519 current_collection
->add_stream(*active_stream
);
1520 } catch(std::exception
& e
) {
1521 messages
<< "Can't add stream: " << e
.what() << std::endl
;
1522 active_stream
->put_ref();
1524 notify_voice_stream_change();
1526 active_stream
->put_ref();
1527 active_stream
= NULL
;
1530 class inthread_th
: public workthread::worker
1544 threads::alock
h(lmut
);
1556 } catch(std::bad_alloc
& e
) {
1558 } catch(std::exception
& e
) {
1559 messages
<< "AIEEE... Fatal exception in voice thread: " << e
.what() << std::endl
;
1565 //Wait for libopus to load...
1566 size_t cbh
= opus::add_callback([this]() {
1567 threads::alock
h(this->lmut
);
1568 this->lcond
.notify_all();
1571 threads::alock
h(lmut
);
1572 if(opus::libopus_loaded() || quit
)
1576 opus::cancel_callback(cbh
);
1580 opus::encoder
oenc(opus::samplerate::r48k
, false, opus::application::voice
);
1581 oenc
.ctl(opus::bitrate(opus_bitrate
.get()));
1582 audioapi_resampler rin
;
1583 audioapi_resampler rout
;
1584 const unsigned buf_max
= 6144; //These buffers better be large.
1585 size_t buf_in_use
= 0;
1586 size_t buf_inr_use
= 0;
1587 size_t buf_outr_use
= 0;
1588 size_t buf_out_use
= 0;
1589 float buf_in
[buf_max
];
1590 float buf_inr
[OPUS_BLOCK_SIZE
];
1591 float buf_outr
[OUTPUT_SIZE
];
1592 float buf_out
[buf_max
];
1593 bitrate_tracker brtrack
;
1594 opus_stream
* active_stream
= NULL
;
1598 if(clear_workflag(workthread::quit_request
) & workthread::quit_request
) {
1599 if(!active_flag
&& active_stream
)
1600 handle_tangent_negative_edge(active_stream
, brtrack
);
1603 uint64_t ticks
= get_utime();
1604 //Handle tangent edgets.
1605 if(active_flag
&& !active_stream
) {
1609 handle_tangent_positive_edge(oenc
, active_stream
, brtrack
);
1611 else if((!active_flag
|| quit
) && active_stream
)
1612 handle_tangent_negative_edge(active_stream
, brtrack
);
1616 //Read input, up to 25ms.
1617 unsigned rate_in
= audioapi_voice_rate().first
;
1618 unsigned rate_out
= audioapi_voice_rate().second
;
1619 size_t dbuf_max
= min(buf_max
, rate_in
/ REC_THRESHOLD_DIV
);
1620 read_input(buf_in
, buf_in_use
, dbuf_max
);
1622 //Resample up to full opus block.
1623 do_resample(rin
, buf_in
, buf_in_use
, buf_inr
, buf_inr_use
, OPUS_BLOCK_SIZE
,
1624 1.0 * OPUS_SAMPLERATE
/ rate_in
);
1626 //If we have full opus block and recording is enabled, compress it.
1627 if(buf_inr_use
>= OPUS_BLOCK_SIZE
&& active_stream
)
1628 compress_opus_block(oenc
, buf_inr
, buf_inr_use
, *active_stream
, brtrack
);
1630 //Update time, starting/ending streams.
1633 //Decompress active streams.
1634 if(buf_outr_use
< BLOCK_THRESHOLD
)
1635 decompress_active_streams(buf_outr
, buf_outr_use
);
1637 //Resample to output rate.
1638 do_resample(rout
, buf_outr
, buf_outr_use
, buf_out
, buf_out_use
, buf_max
,
1639 1.0 * rate_out
/ OPUS_SAMPLERATE
);
1642 if(buf_out_use
> 0 && audioapi_voice_p_status2() < rate_out
/ PLAY_THRESHOLD_DIV
) {
1643 audioapi_play_voice(buf_out
, buf_out_use
);
1647 //Sleep a bit to save CPU use.
1648 uint64_t ticks_spent
= get_utime() - ticks
;
1649 if(ticks_spent
< ITERATION_TIME
)
1650 usleep(ITERATION_TIME
- ticks_spent
);
1652 threads::alock
h(current_collection_lock
);
1653 delete current_collection
;
1654 current_collection
= NULL
;
1660 volatile bool quit_ack
;
1665 //The tangent function.
1666 command::fnptr
<> ptangent(lsnes_cmd
, "+tangent", "Voice tangent",
1667 "Syntax: +tangent\nVoice tangent.\n",
1668 []() throw(std::bad_alloc
, std::runtime_error
) {
1671 command::fnptr
<> ntangent(lsnes_cmd
, "-tangent", "Voice tangent",
1672 "Syntax: -tangent\nVoice tangent.\n",
1673 []() throw(std::bad_alloc
, std::runtime_error
) {
1674 active_flag
= false;
1676 keyboard::invbind
itangent(lsnes_mapper
, "+tangent", "Movie‣Voice tangent");
1677 inthread_th
* int_task
;
1680 //Rate is not sampling rate!
1681 void voice_frame_number(uint64_t newframe
, double rate
)
1683 if(rate
== last_rate
&& last_frame_number
== newframe
)
1685 threads::alock
m(time_mutex
);
1686 current_time
= newframe
/ rate
* OPUS_SAMPLERATE
;
1687 if(fabs(rate
- last_rate
) > 1e-6 || last_frame_number
+ 1 != newframe
)
1689 last_frame_number
= newframe
;
1693 void voicethread_task()
1695 int_task
= new inthread_th
;
1698 void voicethread_kill()
1705 uint64_t voicesub_parse_timebase(const std::string
& n
)
1708 if(x
.length() > 0 && x
[x
.length() - 1] == 's') {
1709 x
= x
.substr(0, x
.length() - 1);
1710 return 48000 * parse_value
<double>(x
);
1712 return parse_value
<uint64_t>(x
);
1715 bool voicesub_collection_loaded()
1717 threads::alock
m2(current_collection_lock
);
1718 return (current_collection
!= NULL
);
1721 std::list
<playback_stream_info
> voicesub_get_stream_info()
1723 threads::alock
m2(current_collection_lock
);
1724 std::list
<playback_stream_info
> in
;
1725 if(!current_collection
)
1727 for(auto i
: current_collection
->all_streams()) {
1728 opus_stream
* s
= current_collection
->get_stream(i
);
1729 playback_stream_info pi
;
1733 pi
.base
= s
->timebase();
1734 pi
.length
= s
->length();
1744 void voicesub_play_stream(uint64_t id
)
1746 threads::alock
m2(current_collection_lock
);
1747 if(!current_collection
)
1748 throw std::runtime_error("No collection loaded");
1749 opus_stream
* s
= current_collection
->get_stream(id
);
1753 start_management_stream(*s
);
1761 void voicesub_export_stream(uint64_t id
, const std::string
& filename
, external_stream_format fmt
)
1763 threads::alock
m2(current_collection_lock
);
1764 if(!current_collection
)
1765 throw std::runtime_error("No collection loaded");
1766 opus_stream
* st
= current_collection
->get_stream(id
);
1769 std::ofstream
s(filename
, std::ios_base::out
| std::ios_base::binary
);
1772 throw std::runtime_error("Can't open output file");
1775 st
->export_stream(s
, fmt
);
1776 } catch(std::exception
& e
) {
1778 (stringfmt() << "Export failed: " << e
.what()).throwex();
1783 uint64_t voicesub_import_stream(uint64_t ts
, const std::string
& filename
, external_stream_format fmt
)
1785 threads::alock
m2(current_collection_lock
);
1786 if(!current_collection
)
1787 throw std::runtime_error("No collection loaded");
1789 std::ifstream
s(filename
, std::ios_base::in
| std::ios_base::binary
);
1791 throw std::runtime_error("Can't open input file");
1792 opus_stream
* st
= new opus_stream(ts
, current_collection
->get_filesystem(), s
, fmt
);
1795 id
= current_collection
->add_stream(*st
);
1797 st
->delete_stream();
1800 st
->unlock(); //Not locked.
1801 notify_voice_stream_change();
1805 void voicesub_delete_stream(uint64_t id
)
1807 threads::alock
m2(current_collection_lock
);
1808 if(!current_collection
)
1809 throw std::runtime_error("No collection loaded");
1810 current_collection
->delete_stream(id
);
1811 notify_voice_stream_change();
1814 void voicesub_export_superstream(const std::string
& filename
)
1816 threads::alock
m2(current_collection_lock
);
1817 if(!current_collection
)
1818 throw std::runtime_error("No collection loaded");
1819 std::ofstream
s(filename
, std::ios_base::out
| std::ios_base::binary
);
1821 throw std::runtime_error("Can't open output file");
1822 current_collection
->export_superstream(s
);
1825 void voicesub_load_collection(const std::string
& filename
)
1827 threads::alock
m2(current_collection_lock
);
1828 filesystem::ref newfs
;
1829 stream_collection
* newc
;
1830 newfs
= filesystem::ref(filename
);
1831 newc
= new stream_collection(newfs
);
1832 if(current_collection
)
1833 delete current_collection
;
1834 current_collection
= newc
;
1835 notify_voice_stream_change();
1838 void voicesub_unload_collection()
1840 threads::alock
m2(current_collection_lock
);
1841 if(current_collection
)
1842 delete current_collection
;
1843 current_collection
= NULL
;
1844 notify_voice_stream_change();
1847 void voicesub_alter_timebase(uint64_t id
, uint64_t ts
)
1849 threads::alock
m2(current_collection_lock
);
1850 if(!current_collection
)
1851 throw std::runtime_error("No collection loaded");
1852 current_collection
->alter_stream_timebase(id
, ts
);
1853 notify_voice_stream_change();
1856 float voicesub_get_gain(uint64_t id
)
1858 threads::alock
m2(current_collection_lock
);
1859 if(!current_collection
)
1860 throw std::runtime_error("No collection loaded");
1861 return current_collection
->get_stream(id
)->get_gain() / 256.0;
1864 void voicesub_set_gain(uint64_t id
, float gain
)
1866 threads::alock
m2(current_collection_lock
);
1867 if(!current_collection
)
1868 throw std::runtime_error("No collection loaded");
1869 int64_t _gain
= gain
* 256;
1870 if(_gain
< -32768 || _gain
> 32767)
1871 throw std::runtime_error("Gain out of range (+-128dB)");
1872 current_collection
->alter_stream_gain(id
, _gain
);
1873 notify_voice_stream_change();
1876 double voicesub_ts_seconds(uint64_t ts
)
1878 return ts
/ 48000.0;