当前位置: 首页>>代码示例>>C++>>正文


C++ BMediaTrack::ReadFrames方法代码示例

本文整理汇总了C++中BMediaTrack::ReadFrames方法的典型用法代码示例。如果您正苦于以下问题:C++ BMediaTrack::ReadFrames方法的具体用法?C++ BMediaTrack::ReadFrames怎么用?C++ BMediaTrack::ReadFrames使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BMediaTrack的用法示例。


在下文中一共展示了BMediaTrack::ReadFrames方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: AudioTrack

AudioTrack*
TrackIO::ImportAudio(BMediaFile* mediaFile, const char* name)
{
	TrackIndex* index = new TrackIndex();

	media_format format;

	MediaFormatBuilder::BuildAudioBlockRawFormat(&format);

	// TODO check original file format to ask 
	// if the user want it to be resampled or not.
	// NOTE atm we are considering only the first track, may and probably will
	// change in future, or at least the user should be able to select which track
	// to import (eventually all and as separated track).
	BMediaTrack* track = mediaFile->TrackAt(0);
	if (track->DecodedFormat(&format) != B_OK) {
		// Critical error show a popup
		return NULL;
	}

	uint32 channels = format.u.raw_audio.channel_count;
	for (uint32 i = 0; i < channels; i++) {
		MediaBlockMap* channel = new MediaBlockMap();
		index->AddChannel(channel);
	}

	WindowsManager::ProgressUpdate(10.0f, "Loading audio data");

	int64 frames = 0;

	int64 bufferFrames = (format.u.raw_audio.buffer_size
		/ (format.u.raw_audio.format 
		& media_raw_audio_format::B_AUDIO_SIZE_MASK));

	float buffer[bufferFrames];

	memset(&buffer, 0, format.u.raw_audio.buffer_size);
	while (track->ReadFrames(buffer, &frames) == B_OK) {
		_BuildBlocks((float*)buffer, frames*channels, index,
			channels);
		memset(&buffer, 0, format.u.raw_audio.buffer_size);
	}

	WindowsManager::ProgressUpdate(50.0f, "Flushing data and preview");

	mediaFile->ReleaseTrack(track);

	// This is done to optimize writes, the data is cached in a buffer
	// and then written to the BPositionIO.
	for (uint32 j = 0; j < index->CountChannels(); j++)
		index->ChannelAt(j)->Writer()->Flush();

	return new AudioTrack(name, index);
}
开发者ID:Barrett17,项目名称:Faber,代码行数:54,代码来源:TrackIO.cpp

示例2: system_time

status_t
MainView::LoadWave(const entry_ref& ref)
{
	bigtime_t startLoadWave = system_time();

	fDestCursor = 0;
	fSourceCursor = 0;
	fAverage = 0.0f;
	fAverageCursor = 0;
	fDownsamplingWidth = 0;
	fDownsampleCount = 0;

	// instantiate a BMediaFile object, and make sure there was no error.
	/*BEntry entry("./song.wav");
	entry_ref ref;
	entry.GetRef(&ref);*/
	BMediaFile* mediaFile = new BMediaFile(&ref);
	status_t err = mediaFile->InitCheck();
	if (err != B_OK) {
		printf("cannot contruct BMediaFile object -- %s\n", strerror(err));
		return err;
	}

	for (int32 i = 0; i < mediaFile->CountTracks(); i++) {
		BMediaTrack* track = mediaFile->TrackAt(i);
		if (track == NULL) {
			printf("cannot contruct BMediaTrack object\n");
			return B_ERROR;
		}

		media_format format;
		format.type = B_MEDIA_RAW_AUDIO;
		format.u.raw_audio.format = media_raw_audio_format::B_AUDIO_FLOAT;
		err = track->DecodedFormat(&format);
		if (err != B_OK) {
			printf("BMediaTrack::DecodedFormat error -- %s\n", strerror(err));
			return err;
		}

		if (format.type == B_MEDIA_RAW_AUDIO) {

			// get the actual sourceWindow.left obtained by seeking
			bigtime_t start = system_time();
			int64 actualSeekFrame = fSourceWindow.left;
			err = track->SeekToFrame(&actualSeekFrame);
			if (err != B_OK) {
				printf("BMediaTrack::SeekToFrame(%lli) error -- %s\n", fSourceWindow.left, strerror(err));
				return err;
			}
			printf("BMediaTrack::SeekToFrame(%lli) seekedto=%lli in %llims\n", fSourceWindow.left, actualSeekFrame, (system_time() - start) / 1000);
			fSourceWindow.left = actualSeekFrame;

			frame_range destWindow(0, (uint64) fWaveFrame.Width());
			int64 totalSourceFrameCount = track->CountFrames();
			fDownsamplingWidth = fSourceWindow.Width() / destWindow.Width();

			printf("Source window left=%lli right=%lli width=%lli (total=%lli)\n", fSourceWindow.left, fSourceWindow.right, fSourceWindow.Width(), totalSourceFrameCount);
			printf("Dest window left=%lli right=%lli width=%lli\n", destWindow.left, destWindow.right, destWindow.Width());
			printf("Downsampling width=%lli\n", fDownsamplingWidth);

			delete [] fWave;
			fWave = NULL;
			fWave = new(std::nothrow) float[fSourceWindow.Width() / fDownsamplingWidth]; // ATT: on en prend plus que la largeur de dest car downsampling width entier
			if (fWave == NULL)
				return B_NO_MEMORY;

			char* buffer = new(std::nothrow) char[format.u.raw_audio.buffer_size];
			if (buffer == NULL)
				return B_NO_MEMORY;

			int64 readFrameCount = 0;
			media_header mediaHeader;
			fSourceCursor = fSourceWindow.left;
			for(int64 j = 0; j < fSourceWindow.Width(); j += readFrameCount) {

				err = track->ReadFrames(buffer, &readFrameCount, &mediaHeader);
				if (err) {
					printf("BMediaTrack::ReadFrames error -- %s\n", strerror(err));
					delete [] buffer;
					break;
					// TODO fatal error?
				}

				if (fSourceCursor + readFrameCount >= fSourceWindow.right) {
					readFrameCount = fSourceWindow.right - fSourceCursor;
					printf("yes readFrameCount = %lli\n", readFrameCount);
				}

				_ProcessAudio(buffer, &format, readFrameCount);
			}
			printf("Source cursor %li (read %li)\n", fSourceCursor, fSourceCursor - fSourceWindow.left);
			printf("Dest cursor %li\n", fDestCursor);

			delete [] buffer;
		}

		mediaFile->ReleaseTrack(track);
	}
	delete mediaFile;

//.........这里部分代码省略.........
开发者ID:Barrett17,项目名称:waveview,代码行数:101,代码来源:MainView.cpp

示例3: if

int32
MediaView::MediaPlayer(
	void	*arg)
{
	MediaView*		view = (MediaView *)arg;
	BWindow*		window = view->Window();
	BBitmap*		bitmap = view->fBitmap;
	BMediaTrack*	videoTrack = view->fVideoTrack;
	BMediaTrack*	audioTrack = view->fAudioTrack;
	BMediaTrack*	counterTrack = (videoTrack != NULL) ? videoTrack : audioTrack;
	AudioOutput*	audioOutput = view->fAudioOutput;
	void*			adBuffer = view->fAudioDumpingBuffer;
	int64			numFrames = counterTrack->CountFrames();
	int64			numFramesToSkip = 0;
	int64			numSkippedFrames = 0;
	bool			scrubbing = false;
	bool			seekNeeded = false;
	int64			dummy;
	media_header	mh;
	bigtime_t		vStartTime, aStartTime, seekTime, snoozeTime, startTime;
	bigtime_t		curScrubbing, lastScrubbing, lastTime;

	curScrubbing = lastScrubbing = system_time();
	seekTime = 0LL;

	// Main processing loop (handle stop->start->stop)
	while (acquire_sem(view->fPlaySem) == B_OK) {
		release_sem(view->fPlaySem);
		
		// as we are doing stop->start, restart audio if needed.
		if (audioTrack != NULL)
			audioOutput->Play();
		startTime = system_time()-counterTrack->CurrentTime();		

		// This will loop until the end of the stream
		while ((counterTrack->CurrentFrame() < numFrames) || scrubbing) {
		
			// We are in scrub mode
			if (acquire_sem(view->fScrubSem) == B_OK) {
				curScrubbing = system_time();

				// We are entering scrub mode
				if (!scrubbing) {
					if (audioTrack != NULL)
						audioOutput->Stop();
					scrubbing = true;
				}
				// Do a seek.
				seekNeeded = true;
				seekTime = view->fScrubTime;
			}
			// We are not scrubbing
			else if (scrubbing) {
				if (audioTrack != NULL)
					audioOutput->Play();
				scrubbing = false;
			}
			
			// Handle seeking
			if (seekNeeded) {
				if (videoTrack) {
					// Seek the seekTime as close as possible
					vStartTime = seekTime;
					videoTrack->SeekToTime(&vStartTime);
					
					// Read frames until we get less than 50ms ahead.
					lastTime = vStartTime;
					do {
						bitmap->LockBits();
						status_t err = videoTrack->ReadFrames((char*)bitmap->Bits(), &dummy, &mh);
						bitmap->UnlockBits();
						if (err != B_OK) break;
						vStartTime = mh.start_time;
						if ((dummy == 0) || (vStartTime <= lastTime))
							break;
						lastTime = vStartTime;
					} while (seekTime - vStartTime > 50000);
				}
				
				if (audioTrack) {
					// Seek the extractor as close as possible
					aStartTime = seekTime;
					audioOutput->SeekToTime(&aStartTime);
					
					// Read frames until we get less than 50ms ahead.
					lastTime = aStartTime;
					while (seekTime - aStartTime > 50000) {
						if (audioTrack->ReadFrames((char *)adBuffer, &dummy, &mh) != B_OK)
							break;
						aStartTime = mh.start_time;
						if ((dummy == 0) || (aStartTime <= lastTime))
							break;
						lastTime = aStartTime;
					}
				}
				else startTime = system_time() - vStartTime;
				
				// Set the current time
				view->fCurTime = seekTime;	
			
//.........这里部分代码省略.........
开发者ID:HaikuArchives,项目名称:Resourcer,代码行数:101,代码来源:MediaView.cpp

示例4: entry


//.........这里部分代码省略.........
	gsFormat.format = format.u.raw_audio.format;
	gsFormat.byte_order = format.u.raw_audio.byte_order;

	BPushGameSound pushGameSound(framesPerBufferPart, &gsFormat,
		bufferPartCount);
	if (pushGameSound.InitCheck() != B_OK) {
		printf("trouble initializing push game sound: %s\n",
			strerror(pushGameSound.InitCheck()));
		return 1;
	}

	uint8 *buffer;
	size_t bufferSize;
	if (pushGameSound.LockForCyclic((void **)&buffer, &bufferSize)
			!= BPushGameSound::lock_ok) {
		printf("cannot lock buffer\n");
		return 1;
	}
	memset(buffer, 0, bufferSize);

	if (pushGameSound.StartPlaying() != B_OK) {
		printf("cannot start playback\n");
		return 1;
	}

	printf("playing, press [esc] to exit...\n");

	uint8 decoded[format.u.raw_audio.buffer_size * 2];
	size_t bufferPartSize = framesPerBufferPart
		* format.u.raw_audio.channel_count
		* (format.u.raw_audio.format
			& media_raw_audio_format::B_AUDIO_SIZE_MASK);
	size_t decodedSize = 0;
	size_t partPos = 0;
	size_t pos = 0; /*pushGameSound.CurrentPosition();*/
	key_info keyInfo;

	while (true) {
		// fill buffer part with data from decoded buffer
		while (partPos < bufferPartSize && decodedSize) {
			size_t size = min_c(bufferPartSize - partPos, decodedSize);

			memcpy(buffer + pos + partPos, decoded, size);
			partPos += size;

			decodedSize -= size;
			memmove(decoded, decoded + size, decodedSize);
		}

		// if there are too little data to fill next buffer part
		// read next decoded frames
		if (partPos < bufferPartSize) {
			int64 frameCount;
			if (mediaTrack->ReadFrames(decoded + decodedSize, &frameCount)
					!= B_OK)
				break;
			if (frameCount == 0)
				break;

			decodedSize += frameCount * format.u.raw_audio.channel_count
				* (format.u.raw_audio.format
					& media_raw_audio_format::B_AUDIO_SIZE_MASK);

			printf("\rtime: %.2f",
				(double)mediaTrack->CurrentTime() / 1000000LL);
			fflush(stdout);

			continue;
		}

		// this buffer part is done
		partPos = 0;
		pos += bufferPartSize;
		if (bufferSize <= pos)
			pos = 0;

		// playback sync - wait for the buffer part we're about to fill to be
		// played
		while (pushGameSound.CurrentPosition() >= pos + bufferPartSize
			|| pushGameSound.CurrentPosition() < pos)
			snooze(1000 * framesPerBufferPart / gsFormat.frame_rate);

		// check escape key state
		if (get_key_info(&keyInfo) != B_OK) {
			printf("\nkeyboard state read error\n");
			break;
		}
		if ((keyInfo.key_states[0] & 0x40) != 0)
			break;
	}

	pushGameSound.StopPlaying();

	mediaFile.ReleaseTrack(mediaTrack);
	mediaFile.CloseFile();

	printf("\nfinished.\n");

	return 0;
}
开发者ID:SummerSnail2014,项目名称:haiku,代码行数:101,代码来源:push_game_sound_test.cpp

示例5: sizeof


//.........这里部分代码省略.........
	outFile->CommitHeader();
	// this is where you would call outFile->AddCopyright(...)

	int64 framesRead;
	media_header mh;
	int32 lastPercent, currPercent;
	float completePercent;
	BString status;

	int64 start;
	int64 end;
	int32 stat = 0;

	// read video from source and write to destination, if necessary
	if (outVidTrack != NULL) {
		lastPercent = -1;
		videoFrameCount = inVidTrack->CountFrames();
		if (endDuration == 0 || endDuration < startDuration) {
			start = 0;
			end = videoFrameCount;
		} else {
			inVidTrack->SeekToTime(&endDuration, stat);
			end = inVidTrack->CurrentFrame();
			inVidTrack->SeekToTime(&startDuration, stat);
			start = inVidTrack->CurrentFrame();
			if (end > videoFrameCount)
				end =  videoFrameCount;
			if (start > end)
				start = 0;
		}

		framesRead = 0;
		for (int64 i = start; (i <= end) && !fCancel; i += framesRead) {
			if ((ret = inVidTrack->ReadFrames(videoBuffer, &framesRead,
					&mh)) != B_OK) {
				fprintf(stderr, "Error reading video frame %" B_PRId64 ": %s\n", i,
						strerror(ret));
				snprintf(status.LockBuffer(128), 128,
						B_TRANSLATE("Error read video frame %" B_PRId64), i);
				status.UnlockBuffer();
				SetStatusMessage(status.String());

				break;
			}

			if ((ret = outVidTrack->WriteFrames(videoBuffer, framesRead,
					mh.u.encoded_video.field_flags)) != B_OK) {
				fprintf(stderr, "Error writing video frame %" B_PRId64 ": %s\n", i,
						strerror(ret));
				snprintf(status.LockBuffer(128), 128,
						B_TRANSLATE("Error writing video frame %" B_PRId64), i);
				status.UnlockBuffer();
				SetStatusMessage(status.String());

				break;
			}
			completePercent = (float)(i - start) / (float)(end - start) * 100;
			currPercent = (int32)completePercent;
			if (currPercent > lastPercent) {
				lastPercent = currPercent;
				snprintf(status.LockBuffer(128), 128,
					B_TRANSLATE("Writing video track: %" B_PRId32 "%% complete"),
					currPercent);
				status.UnlockBuffer();
				SetStatusMessage(status.String());
开发者ID:Barrett17,项目名称:haiku-contacts-kit-old,代码行数:66,代码来源:MediaConverterApp.cpp


注:本文中的BMediaTrack::ReadFrames方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。