1414#import " PlayerTypeDefines.h"
1515#import " UPlayer.h"
1616
17- const NSTimeInterval timeInterval = 1.0 ;
17+
18+
19+ const NSTimeInterval timeInterval = 0.2 ;
1820
1921@interface PlayerEngine ()
2022<AVAudioPlayerDelegate>
@@ -26,8 +28,27 @@ @interface PlayerEngine ()
2628
2729@property (nonatomic ,strong ) AVAudioPlayer *player;
2830@property (nonatomic ,strong ) NSTimer *timer;
31+
32+
33+
34+
35+ @property (nonatomic ,strong ) AVAudioFormat *audioFormat;
36+ @property (nonatomic ,strong ) AVAudioPCMBuffer* pcmBuffer;
37+ @property (nonatomic ,strong ) FFTSampleBlock* sampleBlock;
38+ @end
39+
40+ @implementation FFTSampleBlock
41+
42+ +(int )getSampleLength
43+ {
44+ return 2048 ;
45+ }
46+
2947@end
3048
49+ void getSampleBlockInBufferAtTime (AVAudioPCMBuffer* pcmBuffer, AVAudioFormat *audioFormat, NSTimeInterval time,FFTSampleBlock* sampleBlock);
50+
51+
3152@implementation PlayerEngine
3253
3354
@@ -41,6 +62,9 @@ -(instancetype)init
4162 _progressInfo = [ProgressInfo new ];
4263
4364
65+
66+
67+
4468 addObserverForEvent (self, @selector (playNext ), EventID_track_stopped_playnext);
4569
4670 addObserverForEvent (self, @selector (actionPlayNext ), EventID_to_play_next);
@@ -73,6 +97,18 @@ -(void)timerComing
7397 _progressInfo.current += timeInterval;
7498
7599 postEvent (EventID_track_progress_changed, _progressInfo);
100+
101+
102+ if (self.sampleBlock == nil ) {
103+ self.sampleBlock = [[FFTSampleBlock alloc ]init];
104+ }
105+
106+
107+
108+ getSampleBlockInBufferAtTime (self.pcmBuffer , self.audioFormat , _progressInfo.current , self.sampleBlock );
109+ postEvent (EventID_to_draw_spectrum, self.sampleBlock );
110+
111+
76112 }
77113 }
78114
@@ -299,9 +335,30 @@ - (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *
299335}
300336
301337
338+
339+
340+
302341// if time is -1,it will be ignored
303342-(BOOL )playURL : (NSURL *)url initPaused : (bool )initPaused time : (NSTimeInterval )time
304343{
344+ NSURL * fileURL = url;
345+ AVAudioFile *audioFile = [[AVAudioFile alloc ] initForReading: fileURL error: nil ];
346+ self.audioFormat = audioFile.processingFormat ;
347+ uint32 audioFrameCount = (uint32)audioFile.length ;
348+ self.pcmBuffer = [[AVAudioPCMBuffer alloc ] initWithPCMFormat: self .audioFormat frameCapacity: audioFrameCount];
349+
350+ NSError *errorRead = nil ;
351+ [audioFile readIntoBuffer: self .pcmBuffer error: &errorRead ];
352+ if (errorRead) {
353+ NSLog (@" readIntoBuffer error: %@ " ,errorRead);
354+ }
355+
356+
357+
358+
359+
360+
361+
305362 NSError *error;
306363 self.player = [[AVAudioPlayer alloc ]initWithContentsOfURL:url error: &error];
307364 if (error)
@@ -403,3 +460,61 @@ @implementation ProgressInfo
403460
404461
405462@end
463+
464+
465+
466+ void getSampleBlockInBufferAtTime (AVAudioPCMBuffer* pcmBuffer, AVAudioFormat *audioFormat, NSTimeInterval time,FFTSampleBlock* sampleBlock)
467+ {
468+ //
469+ // printf("%lu,%lu,%lu\n",sizeof(Float32),sizeof(int32_t),sizeof(int16_t));
470+ //
471+ // printf("interleaved, %d\n",audioFormat.interleaved);
472+ //
473+ // printf("audioFrameCount: %d\n",pcmBuffer.frameLength);
474+ //
475+ // printf("\n,channels,%d, sample rate: %f,\n",audioFormat.channelCount,audioFormat.sampleRate);
476+
477+
478+
479+ if ( pcmBuffer.floatChannelData )
480+ {
481+ // printf("floatChannelData,sizeof float: %lu\n",sizeof(float));
482+
483+ Float32 *leftChannelBuffer = pcmBuffer.floatChannelData [0 ];
484+ Float32 *rightChannelBuffer = pcmBuffer.floatChannelData [ 1 ];
485+
486+ for ( int i = 0 ; i < FFT_SAMPLE_SIZE ; i+=1 )
487+ {
488+ sampleBlock.pSampleL = leftChannelBuffer +((int )(time * audioFormat.sampleRate ) - FFT_SAMPLE_SIZE/2 + i );
489+ sampleBlock.pSampleR = rightChannelBuffer +((int )(time * audioFormat.sampleRate ) - FFT_SAMPLE_SIZE/2 + i );
490+ }
491+
492+ }
493+ else if (pcmBuffer.int16ChannelData )
494+ {
495+ printf (" int16ChannelData,sizeof int16: %lu\n " ,sizeof (int16_t ));
496+
497+ // result.pSample = (Float32*)pcmBuffer.int16ChannelData [ (int)(time * audioFormat.sampleRate) - [FFTSampleBlock getSampleLength]/2 ];
498+
499+ }
500+ else if ( pcmBuffer.int32ChannelData )
501+ {
502+ printf (" int32ChannelData,int32: %lu\n " ,sizeof (int32_t ));
503+
504+ // result.pSample = (Float32*)pcmBuffer.int32ChannelData [ (int)(time * audioFormat.sampleRate) - [FFTSampleBlock getSampleLength]/2 ];
505+
506+ }
507+ else {
508+ // can not be here.
509+ assert (false );
510+ }
511+
512+ typedef enum {
513+ mono=1 , // 单声道
514+ stereo, // 联合立体声
515+ mode_3d //
516+ } channel_mode;
517+
518+
519+ }
520+
0 commit comments