📄 player.cpp
字号:
//// Copyright 2004 Alan Post//// This file is part of voudp.//// voudp is free software; you can redistribute it and/or modify it under the// terms of the GNU General Public License as published by the Free Software// Foundation; either version 2 of the License, or (at your option) any later// version.//// voudp is distributed in the hope that it will be useful, but WITHOUT ANY// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR// A PARTICULAR PURPOSE. See the GNU General Public License for more details.//// You should have received a copy of the GNU General Public License along with// voudp; if not, write to the Free Software Foundation, Inc., 59 Temple Place,// Suite 330, Boston, MA 02111-1307 USA//#include "player.h"#include "receiver.h"#include "die.h"#include <sys/audioio.h>#include <sys/ioctl.h>#include <algorithm>//// XXX we didn't actually write block zero -- we get one block of silence at// startup time.//Player::Player( u_int8_t *dma_buf_, const int &afd_, const size_t &blocksize_, // in bytes const size_t &buffer_size_ ) // in bytes: dma_buf( dma_buf_ ), afd( afd_ ), blocksize( blocksize_ ), blocks_in_buffer( buffer_size_ / blocksize_ ), samples_per_block( blocksize_ / sizeof( hw_sample_t )), last_written_block( 0 ), leftover_offset( samples_per_frame ){}static void convert( hw_sample_t *hw, float *samples, u_int n ){ for ( u_int i = 0; i < n; i++ ) { float sample = samples[i]; if ( sample < -32766 ) sample = -32766; if ( sample > 32766 ) sample = 32766; int16_t hw_sample = (int16_t) sample; for ( size_t j = 0; j < hw_channels; j++ ) hw[i].s[j] = hw_sample; }}voidPlayer::play_if_needed( Receiver &receiver ){ audio_offset_t poff; if ( ioctl( afd, AUDIO_GETOOFFS, &poff ) < 0 ) die( "failed to get play offset" ); u_int playing_block = poff.samples / blocksize; if ( last_written_block > playing_block ) return; hw_sample_t *block = (hw_sample_t*) ( dma_buf + blocksize * (( playing_block + 1 ) % blocks_in_buffer )); u_int block_offset = 0; // units in samples while ( block_offset < samples_per_block ) { u_int to_write = samples_per_block - block_offset; u_int leftovers = samples_per_frame - leftover_offset; if ( leftovers == 0 ) { receiver.decode_frame( frame_acc ); leftover_offset = 0; continue; } u_int writing = min( to_write, leftovers ); convert( block + block_offset, frame_acc + leftover_offset, writing ); leftover_offset += writing; block_offset += writing; } last_written_block = playing_block + 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -