int main(void) { int fd; short *value; int i = 0; if ( ( fd = open( "/dev/dsp", O_RDWR ) ) == -1 ) { perror( "open()" ); return 1; } if ( setup_dsp( fd ) != 0 ) { fprintf( stderr, "Setup /dev/dsp failed.\n" ); close( fd ); return 1; } while(read(fd, value, sizeof(short) > 0)){ printf("%d\n", *value); } close( fd ); return 0; }
int music::play_music() { MainWindow *mine=(MainWindow*)arg; mine->my_words(); int music_len; long rate; char* buf; long n; int min; min=ftell(music_fp); music_len = head.ChunkSize+8; mine->set_line(music_len,min); ///安装dsp 设置 采样率 声道个数 if(setup_dsp(dsp_fd,head.SampleRate,head.NumChannels) != 0) { perror("setup_dsp failed"); return -1; } ////生效setup_dsp设置 if(sync_dsp(dsp_fd) != 0) { perror("sync dsp failed"); return -1; } //-----设置采样率-------- //采样位数*采样率/8 = 字节数量 //+7 方式采样率不是8的整数倍的情况 // /8*2 = /4 都按照双声道处理 //size = (head.BitsPerSample*head.SampleRate+7)>>2; rate = head.ByteRate; //每次读取的字节 = 字节速率 buf = (char*)malloc(rate); //音乐数据缓存 if(buf != NULL) { do{ pthread_mutex_lock(&my_mutex); pthread_mutex_unlock(&my_mutex); mine->word_move(); n = fread(buf,1,rate,music_fp); //cout<<"n:"<<n<<endl; if(n > 0) { write(dsp_fd,buf,n); } if(feof(music_fp)) { perror("play over."); break; } mine->add_second(); mine->move_line(ftell(music_fp)); this->set_vol(); }while(1); } return 1; }