📄 soundstream.c
字号:
stream->isFinished = TRUE; SWFSoundStream_rewind(stream); }}static void fillStreamBlock_mp3(SWFSoundStream stream, SWFSoundStreamBlock block){ int delay, wanted; /* see how many frames we can put in this block, see how big they are */ block->delay = stream->delay; delay = stream->delay + stream->samplesPerFrame; wanted = delay; block->length = getMP3Samples(stream->source.mp3.input, stream->flags, &delay); block->numSamples = delay; if(block->length <= 0) { stream->isFinished = TRUE; SWFSoundStream_rewind(stream); } stream->delay = wanted - delay;}SWFBlockSWFSoundStream_getStreamBlock(SWFSoundStream stream){ SWFSoundStreamBlock block; if ( stream->isFinished ) return NULL; block = (SWFSoundStreamBlock) malloc(sizeof(struct SWFSoundStreamBlock_s)); SWFBlockInit((SWFBlock)block); BLOCK(block)->complete = completeSWFSoundStream; BLOCK(block)->writeBlock = writeSWFSoundStreamToMethod; BLOCK(block)->dtor = NULL; BLOCK(block)->type = SWF_SOUNDSTREAMBLOCK; block->stream = stream; block->length = 0; block->numSamples = 0; if(stream->streamSource == STREAM_MP3) fillStreamBlock_mp3(stream, block); else if(stream->streamSource == STREAM_FLV) fillStreamBlock_flv(stream, block); if(block->length == 0) { free(block); return NULL; } return (SWFBlock)block;}/* * returns the duration of a stream in ms * * This function returns the duration of a given stream in ms */unsigned int SWFSoundStream_getDuration(SWFSoundStream stream){ if(stream->streamSource == STREAM_MP3) return getMP3Duration(stream->source.mp3.input); else if(stream->streamSource == STREAM_FLV) return FLVStream_getDuration(stream->source.flv.stream, FLV_AUDIOTAG); else return 0;}/* * DEPRECATED! * returns the number of movie frames for a given sound stream * * This function returns the number of movie frames necessary to * play the full sound stream. * Works only if the sound stream object was added to a movie and the stream * source is a mp3 file. * * Use SWFSoundStream_getDuration() instead. */int SWFSoundStream_getFrames(SWFSoundStream stream){ int n, frameSize; if(stream->streamSource == STREAM_FLV || stream->samplesPerFrame == 0) { SWF_warn("SWFSoundStream_getFrames works only if stream was assigned to a movie\n"); return -1; } if ( stream->sampleRate > 32000 ) frameSize = 1152; else frameSize = 576; n = 0; while(nextMP3Frame(stream->source.mp3.input) > 0) n++; SWFSoundStream_rewind(stream); return n * frameSize / stream->samplesPerFrame;} static intgetStreamFlag_mp3File(SWFSoundStream stream, float frameRate, float skip){ SWFInput input = stream->source.mp3.input; int start; byte flags; start = getMP3Flags(input, &flags); if(start < 0) return -1; stream->source.mp3.start = start; stream->sampleRate = SWFSound_getSampleRate(flags); stream->flags = flags; // XXX: fixme stream->samplesPerFrame = (int)floor(stream->sampleRate / frameRate); skipMP3(stream, skip); return flags;}static intgetStreamFlag_flv(SWFSoundStream stream, float frameRate, float skip){ int flags = 0, ret; FLVTag tag, *tag_p = NULL; unsigned int skip_msec; while((ret = FLVStream_nextTag(stream->source.flv.stream, &tag, tag_p)) == 0) { if(tag.tagType == FLV_AUDIOTAG) break; tag_p = &tag; } if(ret < 0) return -1; switch(tag.hdr.audio.samplingRate >> 2) { case 1: stream->sampleRate = 11025; break; case 2: stream->sampleRate = 22050; break; case 3: stream->sampleRate = 44100; break; default: SWF_warn("getStreamFlag_flv: unsupported sampleRate\n"); } stream->samplesPerFrame = (int)floor(stream->sampleRate / frameRate); flags = tag.hdr.audio.samplingRate | tag.hdr.audio.sampleSize; flags |= tag.hdr.audio.channel | tag.hdr.audio.format; stream->flags = flags; // XXX: fixme skip_msec = round(skip * 1000); if(FLVStream_setStreamOffset(stream->source.flv.stream, skip_msec) < 0) return -1; return flags;}SWFBlockSWFSoundStream_getStreamHead(SWFSoundStream stream, float frameRate, float skip){ int flags = 0; SWFOutput out = newSizedSWFOutput(4); SWFOutputBlock block = newSWFOutputBlock(out, SWF_SOUNDSTREAMHEAD2); if(stream->streamSource == STREAM_MP3) flags = getStreamFlag_mp3File(stream, frameRate, skip); else if(stream->streamSource == STREAM_FLV) flags = getStreamFlag_flv(stream, frameRate, skip); stream->flags = flags; stream->frameRate = frameRate; if(flags < 0) { destroySWFOutputBlock(block); return NULL; } SWFOutput_writeUInt8(out, flags & 0x0f); SWFOutput_writeUInt8(out, flags); SWFOutput_writeUInt16(out, stream->samplesPerFrame); if(((flags & 0xf0) >> 4) == 2) // MP3 only SWFOutput_writeUInt16(out, stream->initialDelay); return (SWFBlock)block;}void SWFSoundStream_setInitialMp3Delay(SWFSoundStream stream, int delay){ stream->initialDelay = delay;}intSWFSoundStream_getFlags(SWFSoundStream stream){ if(stream->streamSource == STREAM_MP3) return getStreamFlag_mp3File(stream, 1.0f, 0); else if(stream->streamSource == STREAM_FLV) return getStreamFlag_flv(stream, 1.0f, 0); return 0;}int SWFSoundStream_getLength(SWFSoundStream stream, SWFSoundStreamBlock streamblock){ int source = stream->streamSource; struct SWFSoundStreamBlock_s block; if (streamblock == NULL) streamblock = █ streamblock->stream = stream; streamblock->length = 0; streamblock->numSamples = 0; stream->delay = INT_MAX - stream->samplesPerFrame - 1; if(source == STREAM_MP3) { fillStreamBlock_mp3(stream, streamblock); } else if(source == STREAM_FLV) { fillStreamBlock_flv(stream, streamblock); } return streamblock->length;}voidwriteSWFSoundWithSoundStreamToMethod(SWFSoundStream stream, SWFByteOutputMethod method, void *data){ int source = stream->streamSource; struct SWFSoundStreamBlock_s streamblock; // need to get the sample count && and rewind SWFSoundStream_getLength(stream, &streamblock); SWFSoundStream_rewind(stream); methodWriteUInt32(streamblock.numSamples, method, data); methodWriteUInt16(stream->initialDelay, method, data); if(source == STREAM_MP3) write_mp3(&streamblock, method, data); else if(source == STREAM_FLV) write_flv(&streamblock, method, data);}/* XXX - kill this somehow.. */voidSWFSoundStream_rewind(SWFSoundStream stream){ if(stream->streamSource == STREAM_MP3) SWFInput_seek(stream->source.mp3.input, stream->source.mp3.start, SEEK_SET); else if(stream->streamSource == STREAM_FLV) stream->source.flv.tagOffset = -1;}SWFSoundStreamnewSWFSoundStream_fromInput(SWFInput input){ FLVStream *flvStream; SWFSoundStream stream = (SWFSoundStream)malloc(sizeof(struct SWFSoundStream_s)); /* test if input stream is a FLV file first. */ flvStream = FLVStream_fromInput(input); /* if input is not a valid FLV file a MP3 file is assumed. */ if(flvStream == NULL) { SWFInput_seek(input, 0, SEEK_SET); stream->streamSource = STREAM_MP3; stream->source.mp3.input = input; stream->source.mp3.start = 0; } else { stream->streamSource = STREAM_FLV; stream->source.flv.stream = flvStream; stream->source.flv.tagOffset = -1; } stream->initialDelay = SWFSOUND_INITIAL_DELAY; stream->delay = SWFSOUND_INITIAL_DELAY; stream->isFinished = FALSE; stream->samplesPerFrame = 0; stream->sampleRate = 0; stream->freeInput = FALSE; stream->flags = -1; return stream;}voiddestroySWFSoundStream(SWFSoundStream stream){ if (stream->freeInput) { if(stream->streamSource == STREAM_MP3) destroySWFInput(stream->source.mp3.input); else if(stream->streamSource == STREAM_FLV) destroyFLVStream(stream->source.flv.stream); else SWF_warn("destroySWFSoundStream: unknown stream\n"); } free(stream);}SWFSoundStreamnewSWFSoundStream(FILE* file){ SWFSoundStream s = newSWFSoundStream_fromInput(newSWFInput_file(file)); s->freeInput = TRUE; return s;}SWFSoundStreamnewSWFSoundStreamFromFileno(int fd){ FILE *fp = fdopen(fd, "r"); return newSWFSoundStream(fp);}/* * Local variables: * tab-width: 2 * c-basic-offset: 2 * End: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -