📄 sbc-test.c
字号:
#include <stdlib.h>#include <stdio.h>#include <fcntl.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include "sbc_internal.h"/* Doesn't work with size%2!=0 */void swapper(u_int8_t* memory, off_t size){ int i; u_int8_t a; for(i=0; i<size; i+=2) { a = memory[i]; memory[i] = memory[i+1]; memory[i+1] = a; }}int main(int argc, char** argv) { char *testfile = "testing/sbc_test_01.sbc"; struct sbc_decoder_state state; struct sbc_encoder_state estate; int fh; struct stat filestat; u_int8_t *stream; off_t streamlength; off_t pos = 0; int frames = 0; state.subbands = 0; if(argc==2) testfile = argv[1]; if(stat(testfile, &filestat)!=0) { perror("Can't stat"); return 1; } streamlength = filestat.st_size; stream = malloc(streamlength); if(!stream) { perror("Can't malloc"); return 1; } fh = open(testfile, O_RDONLY); if(fh < 0) { perror("Can't open"); return 1; } if(read(fh, stream, streamlength)!=streamlength) { perror("Some reading error"); return 1; } close(fh); fh = open("bla_.au", O_WRONLY|O_CREAT|O_TRUNC, 0644); if(fh < 0) { perror("Can't open in write mode"); return -1; } do { struct sbc_frame frame; int framelen; framelen = sbc_unpack_frame(stream+pos, &frame, streamlength-pos); if(framelen > 0) { if(state.subbands == 0) { u_int8_t sample_rate1 = ((u_int32_t)(frame.sampling_frequency * 1000) & 0xff000000) >> 24; u_int8_t sample_rate2 = ((u_int32_t)(frame.sampling_frequency * 1000) & 0x00ff0000) >> 16; u_int8_t sample_rate3 = ((u_int32_t)(frame.sampling_frequency * 1000) & 0x0000ff00) >> 8; u_int8_t sample_rate4 = ((u_int32_t)(frame.sampling_frequency * 1000) & 0x000000ff); sbc_decoder_init(&state, &frame); sbc_encoder_init(&estate, &frame); write(fh, ".snd", 4); /* magic word */ write(fh, "\0\0\0\x18", 4); /* offset to data */ write(fh, "\xff\xff\xff\xff", 4); /* length unspecified */ write(fh, "\0\0\0\3", 4); /* encoding: 16 bit linear pcm */ write(fh, &sample_rate1, 1); write(fh, &sample_rate2, 1); write(fh, &sample_rate3, 1); write(fh, &sample_rate4, 1); /* sampling rate */ write(fh, "\0\0\0", 3); write(fh, &frame.channels, 1); /* number of channels */ } fprintf(stderr, "Got a frame: "); fprintf(stderr, "%.1f kHz, ", frame.sampling_frequency); fprintf(stderr, "%i blocks, ", frame.blocks); switch(frame.channel_mode) { case MONO: fprintf(stderr, "mono, "); break; case DUAL_CHANNEL: fprintf(stderr, "dual channel, "); break; case STEREO: fprintf(stderr, "stereo, "); break; case JOINT_STEREO: fprintf(stderr, "joint stereo, "); break; } switch(frame.allocation_method) { case LOUDNESS: fprintf(stderr, "loudness, "); break; case SNR: fprintf(stderr, "SNR, "); break; } fprintf(stderr, "%i subbands, ", frame.subbands); fprintf(stderr, "bitpool: %u\n", frame.bitpool); { int ch,i,samples; samples = sbc_synthesize_audio(&state, &frame); for(i=0; i<samples; i++) { for(ch=0; ch<frame.channels; ch++) { u_int16_t s = (u_int16_t)(frame.pcm_sample[ch][i]); u_int8_t s1 = (s & 0xff00) >> 8; u_int8_t s2 = (s & 0x00ff); write(fh, &s1, sizeof(s1)); write(fh, &s2, sizeof(s2)); } } sbc_analyze_audio(&estate, &frame); } pos += framelen; frames++; } else { fprintf(stderr, "Attention: %s at 0x%X\n", (framelen==-1?"stream too short":(framelen==-2?"sync byte incorrect":(framelen==-3?"CRC incorrect":"huh?"))), (unsigned int)pos); pos++; } } while(pos < streamlength); fprintf(stderr, "Got %i frames\n", frames); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -