I have recorded a movie file using AVCaptureMovieFileOutput by setting maximum duration limit.
For e.g.: If I want to record 10 seconds video, I had set the max duration and other properties for the movie file like below...
Float64 TotalSeconds = 10;
int32_t preferredTimeScale = 30
CMTime maxDuration = CMTimeMakeWithSeconds(TotalSeconds, preferredTimeScale);
aMovieFileOutput.maxRecordedDuration = maxDuration;
aMovieFileOutput.minFreeDiskSpaceLimit = 1024 * 1024;
But recorded video is showing only 9 seconds (which I played using MPMoviePlayerController), why is that time difference.. How to record exactly 10 seconds. Am I doing anything wrong while setting maximum duration. Thanx.
Please be sure to have the good framerate for the output
#define CAPTURE_FRAMES_PER_SECOND 30
//SET THE CONNECTION PROPERTIES (output properties)
AVCaptureConnection* captureConnection = [self.movieFileOutput connectionWithMediaType:AVMediaTypeVideo];
if(captureConnection.supportsVideoMinFrameDuration) captureConnection.videoMinFrameDuration = CMTimeMake(1,CAPTURE_FRAMES_PER_SECOND);
if(captureConnection.supportsVideoMaxFrameDuration) captureConnection.videoMaxFrameDuration = CMTimeMake(1,CAPTURE_FRAMES_PER_SECOND);
CMTimeShow(captureConnection.videoMinFrameDuration);
CMTimeShow(captureConnection.videoMaxFrameDuration);
Related
I realize that there are many questions here concerning converting MIDI ticks to milliseconds (ex: How to convert midi timeline into the actual timeline that should be played, Midi Ticks to Actual PlayBack Seconds !!! ( Midi Music), Midi timestamp in seconds) and I have looked at them all, tried to implement the suggestions, but i am still not getting it.
(Did I mention I am a little "math phobic")
Can anyone help me work a practical example? I am using the Bass lib from un4seen. I have all the data I need - I just don't trust my calculations.
Bass Methods
Tick
// position of midi stream
uint64_t tick = BASS_ChannelGetPosition(midiFileStream, BASS_POS_MIDI_TICK)
PPQN
//The Pulses Per Quarter Note (or ticks per beat) value of a MIDI stream.
float ppqn;
BASS_ChannelGetAttribute(handle, BASS_ATTRIB_MIDI_PPQN, &ppqn);
Tempo
//tempo in microseconds per quarter note.
uint32_t tempo = BASS_MIDI_StreamGetEvent( midiFileStream, -1, MIDI_EVENT_TEMPO);
My Attempt at Calculating MS value for tick:
float currentMilliseconds = tick * tempo / (ppqn * 1000);
The value I get appears correct but I don't have any confidence in it since I am not quite understanding the formula.
printf("tick %llu\n",tick);
printf("ppqn %f\n",ppqn);
printf("tempo %u\n",tempo);
printf("currentMilliseconds %f \n", currentMilliseconds);
Example output:
tick 479
ppqn 24.000000
tempo 599999
currentMilliseconds 11974.980469
Update
My confusion continues but based on this blog post I think I have the code right – at least the output seems accurate. Conversely, the answer provided by #Strikeskids below yields different results. Maybe I have an order of operations problem in there?
float kMillisecondsPerQuarterNote = tempo / 1000.0f;
float kMillisecondsPerTick = kMillisecondsPerQuarterNote / ppqn;
float deltaTimeInMilliseconds = tick * kMillisecondsPerTick;
printf("deltaTimeInMilliseconds %f \n", deltaTimeInMilliseconds);
.
float currentMillis = tick * 60000.0f / ppqn / tempo;
printf("currentMillis %f \n", currentMillis);
Output:
deltaTimeInMilliseconds 11049.982422
currentMillis 1.841670
Tempo is in beats per minute. Because you want to be getting a time, you should have it in the denominator of your fraction.
currentTime = currentTick * (beats / tick) * (minutes / beat) * (millis / minute)
millis = tick * (1/ppqn) * (1/tempo) * (1000*60)
to use integer arithmetic efficiently do
currentMillis = tick * 60000 / ppqn / tempo
This works:
float kMillisecondsPerQuarterNote = tempo / 1000.0f;
float kMillisecondsPerTick = kMillisecondsPerQuarterNote / ppqn;
float deltaTimeInMilliseconds = tick * kMillisecondsPerTick;
printf("deltaTimeInMilliseconds %f \n", deltaTimeInMilliseconds);
i am using unity3d to record some input from the microphone using:
getAudioSource.clip = Microphone.Start(null, true,20, 44100);
The problem is that this records for a standard time period of 20 secs, which i don't want. I would like to record untill a finish a recording by:
Microphone.End(deviceName);
and the resulting clip should have the exact size i recorded, not a standard 20 secs. Right now, if i end the recording after 3 secs, the resulting audio is 20 secs in length. I would like it to have 3 secs.
How can this be done?
the size isn't changeable, it's fixed size.. but i did manage to trim the audio to the actual size.
UPDATE
AudioClip ac = getAudioSource.clip;
float lengthL = ac.length;
float samplesL = ac.samples;
float samplesPerSec = (float)samplesL/lengthL;
float[] samples = new float[(int)(samplesPerSec * timeSinceRecordStarted)];
ac.GetData(samples,0);
getAudioSource.clip = AudioClip.Create("RecordedSound",(int)(timeSinceRecordStarted*samplesPerSec),1,44100,false,false);
getAudioSource.clip.SetData(samples,0);
where timeSinceRecordStarted is the length in secs of the recording and getAudioSource initially holds the microphone audio clip.
in my app i need to submit the time to the game center and i need to show that in Elapsed Time - To the hundredth of a second format.
00:00:00.00
this is the format i want to show in leader board.
In my app im getting the time in following format
ss.SS
ss = seconds
SS = hundredth of a second
i converted the value to double before send it to the game center
double newScoreDouble = [newScore doubleValue];
But when i sending the double score to the game center it asking me to convert it to int64_t format. But when i convert it to that format it loses some part of the double value.
double intPart = 0;
double fractPart = modf(newScoreDouble, &intPart);
int isecs = (int)intPart;
int min = isecs / 60;
int sec = isecs % 60;
int hund = (int) (fractPart * 100);
int64_t time_to_send_through_game_center = min*6000 + (sec*100 + hund);
this is the way i convert double to int64_t
Can any one say how to send whole double value to the game center and display it in Elapsed Time - To the hundredth of a second format.
Thanks
I've done this before. When you're recording a score in the to the hundredth of a second format. You would multiply your seconds with a hundred before submitting.
So let's say the user scored 1minute 44 seconds 300 milliseconds : 1:44:30 = 104.3 seconds. Then you would set your value property of GKScore object equal to 104.3 * 100 = 10430 ,and submit it like that.
Give it a try :)
I am successfully making a CMTime with following code:
endPoint = CMTimeMake([mp currentPlaybackTime], 1);
The current position, which originally was a float value, of the movie in my MPMoviePlayerController is given back as a CMTime in seconds, which is not bad.
But how I can get this position in e.g. in milli seconds?
I played with the 'timescale' and set it to 10 and 100, but it didn't have an effect to the result.
Thank you in advance!
I’m not really sure what you want. Do you understand the logic behind CMTime?
1.0s = 1/1s = CMTimeMake(1, 1)
0.1s = 1/10s = CMTimeMake(1, 10)
0.2s = 1/5s = CMTimeMake(1, 5)
0.2s = 2/10s = CMTimeMake(2, 10)
…
In other words, CMTimeMake(a, b) is the time value a/b. Thus when you have a floating-point time value:
double time1 = 0.2;
// in ms, (0.2*1000)/1000 == 200/1000 == 0.2
CMTime time2 = CMTimeMake(time1*1000, 1000);
Maybe this is what you want?
I am trying to display minutes and seconds based on a number of seconds.
I have:
float seconds = 200;
float mins = seconds / 60.0;
float sec = mins % 60.0;
[timeIndexLabel setText:[NSString stringWithFormat:#"%.2f , %.2f", mins,seconds]];
But I get an error: invalid operands of types 'float' and 'double' to binary 'operator%'
And I don't understand why... Can someone throw me a bone!?
A lot of languages only define the % operator to work on integer operands. Try casting seconds and mins to int before you use % (or just declare them int in the first place). The constant values you use will also need to be int (use 60 instead of 60.0).
As others have pointed out, you should be using integers. However, noone seems to have spotted that the result will be incorrect. Go back and have another look at modulo arithmetic, and you'll realize you should be doing
int seconds = 200;
int mins = seconds / 60;
int sec = seconds % 60;
Note the last line, seconds % 60 rather than mins % 60 (which will return the remainder of the minutes divided by 60, which is the number of minutes to the hour, and completely unrelated to this calculation).
EDIT
doh, forgot the ints... :)
The 60.0 forces a conversion to double
try:
float seconds = 200;
float mins = seconds / 60;
float sec = mins % 60;
Use ints instead. At least in your example, seems like they're enough (it will also be faster and clearer).
Also, in this case you would get 3.3333... mins, and not 3 minutes as expected. You could use Math.ceil(x) if you need to work with floats.
Do like this:
float seconds = 200.5;
float mins = floor(seconds / 60.0);
float sec = seconds - mins * 60.0;