Skip to content

Fast PCM Encoding #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ project.xcworkspace/

# temporary files -------------------------------

*~
*~
.idea
39 changes: 29 additions & 10 deletions audiostream/inc/ni/media/audio/ostream.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,14 @@
auto operator<<( const Range& rng ) -> std::enable_if_t<boost::has_range_iterator<Range>::value, ostream&>;

using std::ostream::bad;
using std::ostream::clear;
using std::ostream::eof;
using std::ostream::fail;
using std::ostream::good;
using std::ostream::rdbuf;
using std::ostream::rdstate;
using std::ostream::setstate;


using std::ostream::tellp;
auto frame_tellp() -> pos_type;
auto sample_tellp() -> pos_type;

// - stream_info

virtual auto info() const -> const info_type&;

protected:
Expand Down Expand Up @@ -113,9 +106,35 @@
template <class Range>
auto ostream::operator<<( const Range& rng ) -> std::enable_if_t<boost::has_range_iterator<Range>::value, ostream&>
{
// TODO fast pcm conversion
for ( auto val : rng )
*this << val;
using Value = typename boost::range_value<Range>::type;

if ( fail() )
return *this;

Check warning on line 112 in audiostream/inc/ni/media/audio/ostream.h

View check run for this annotation

Codecov / codecov/patch

audiostream/inc/ni/media/audio/ostream.h#L112

Added line #L112 was not covered by tests

if ( m_streambuf->overflow() == streambuf::traits_type::eof() )
{
setstate( rdstate() | failbit );
return *this;

Check warning on line 117 in audiostream/inc/ni/media/audio/ostream.h

View check run for this annotation

Codecov / codecov/patch

audiostream/inc/ni/media/audio/ostream.h#L116-L117

Added lines #L116 - L117 were not covered by tests
}

auto in_beg = std::begin( rng );
auto in_end = std::end( rng );

auto in_iter = in_beg;
do
{
m_streambuf->sync();
assert( std::distance( m_streambuf->pptr(), m_streambuf->epptr() ) % m_info->bytes_per_sample() == 0 );

const auto out_beg = pcm::make_iterator<Value>( m_streambuf->pptr(), m_info->format() );
const auto out_end = pcm::make_iterator<Value>( m_streambuf->epptr(), m_info->format() );

auto result = pcm::copy( in_iter, in_end, out_beg, out_end );

auto count = static_cast<int>( std::distance( in_iter, result.first ) * m_info->bytes_per_sample() );
in_iter = result.first;
m_streambuf->pbump( count );
} while ( in_iter < in_end && m_streambuf->overflow() != streambuf::traits_type::eof() );

return *this;
}
Expand Down