Skip to content

Commit 8d2dc23

Browse files
committed
Add spectrum in album view
1 parent 6854c02 commit 8d2dc23

10 files changed

Lines changed: 583 additions & 69 deletions

File tree

src/PlayerEngine.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,13 @@
5757
@property (nonatomic) double current,
5858
total;
5959
@end
60+
61+
62+
enum{
63+
FFT_SAMPLE_SIZE = 2048
64+
};
65+
66+
@interface FFTSampleBlock : NSObject
67+
@property (nonatomic,unsafe_unretained) Float32 *pSampleL,*pSampleR;
68+
@end
69+

src/PlayerEngine.mm

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
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+

src/PlayerMessage.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ typedef enum : NSUInteger {
7474
EventID_to_set_font_size,
7575

7676
EventID_to_show_hide_table_header,
77+
78+
/// @param: FFTSampleBlock
79+
EventID_to_draw_spectrum,
80+
7781
} EventID;
7882

7983

src/PlayerMessage.mm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@
6363
"to_play_item",
6464
"to_love_item",
6565
"to_set_font_size",
66-
"to_show_hide_table_header"
66+
"to_show_hide_table_header",
67+
"to_draw_spectrum"
6768
};
6869

6970
NSNotificationCenter *sCenter ;

uPlayer.xcodeproj/project.pbxproj

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
1E154DF61AAEFA8A0000A3B1 /* keycode.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1E154DF51AAEFA8A0000A3B1 /* keycode.mm */; };
1919
1E169A891A78E701003D3D01 /* windowController.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1E169A881A78E701003D3D01 /* windowController.mm */; };
2020
1E169A8D1A78FF88003D3D01 /* PlayerMessage.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1E169A8C1A78FF88003D3D01 /* PlayerMessage.mm */; };
21-
1E1719841BCE36B90048C3EB /* AlbumViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 1E1719801BCE36B90048C3EB /* AlbumViewController.m */; };
21+
1E1719841BCE36B90048C3EB /* AlbumViewController.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1E1719801BCE36B90048C3EB /* AlbumViewController.mm */; };
2222
1E1719851BCE36B90048C3EB /* cd_bg.png in Resources */ = {isa = PBXBuildFile; fileRef = 1E1719811BCE36B90048C3EB /* cd_bg.png */; };
2323
1E1719861BCE36B90048C3EB /* cd_icon.png in Resources */ = {isa = PBXBuildFile; fileRef = 1E1719821BCE36B90048C3EB /* cd_icon.png */; };
2424
1E1719871BCE36B90048C3EB /* cd_icon_r.png in Resources */ = {isa = PBXBuildFile; fileRef = 1E1719831BCE36B90048C3EB /* cd_icon_r.png */; };
@@ -151,7 +151,7 @@
151151
1E169A8C1A78FF88003D3D01 /* PlayerMessage.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = PlayerMessage.mm; sourceTree = "<group>"; };
152152
1E16D53B1A8B0F8A00C844ED /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/Main.strings"; sourceTree = "<group>"; };
153153
1E17197F1BCE36B90048C3EB /* AlbumViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AlbumViewController.h; path = AlbumView/AlbumViewController.h; sourceTree = "<group>"; };
154-
1E1719801BCE36B90048C3EB /* AlbumViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AlbumViewController.m; path = AlbumView/AlbumViewController.m; sourceTree = "<group>"; };
154+
1E1719801BCE36B90048C3EB /* AlbumViewController.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = AlbumViewController.mm; path = AlbumView/AlbumViewController.mm; sourceTree = "<group>"; };
155155
1E1719811BCE36B90048C3EB /* cd_bg.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = cd_bg.png; sourceTree = "<group>"; };
156156
1E1719821BCE36B90048C3EB /* cd_icon.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = cd_icon.png; sourceTree = "<group>"; };
157157
1E1719831BCE36B90048C3EB /* cd_icon_r.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = cd_icon_r.png; sourceTree = "<group>"; };
@@ -222,6 +222,7 @@
222222
1ED32D971CA90FC000D0C87D /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
223223
1ED6B0A31AEA043F00DBEEE4 /* id3Info.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = id3Info.h; path = ../src/id3Info.h; sourceTree = "<group>"; };
224224
1ED6B0A41AEA043F00DBEEE4 /* id3Info.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = id3Info.mm; path = ../src/id3Info.mm; sourceTree = "<group>"; };
225+
1ED953BD1DD01A630007A2DB /* FFT.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FFT.h; sourceTree = "<group>"; };
225226
1EDAAD461AB6F17E009DB36A /* Base */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = Base; path = Base.lproj/Localizable.strings; sourceTree = "<group>"; };
226227
1EDAAD481AB6F180009DB36A /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/Localizable.strings"; sourceTree = "<group>"; };
227228
1EDE8FA21A773D810086677B /* Smine.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Smine.app; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -475,7 +476,8 @@
475476
1E5F426B1A887BE800911BE8 /* PlaylistViewController.h */,
476477
1E5F426C1A887BE800911BE8 /* PlaylistViewController.mm */,
477478
1E17197F1BCE36B90048C3EB /* AlbumViewController.h */,
478-
1E1719801BCE36B90048C3EB /* AlbumViewController.m */,
479+
1E1719801BCE36B90048C3EB /* AlbumViewController.mm */,
480+
1ED953BD1DD01A630007A2DB /* FFT.h */,
479481
1E17197E1BCE36B90048C3EB /* AlbumView */,
480482
1EFD42CE1A91E847009D65B5 /* ThreadJob.h */,
481483
1EFD42CC1A91E826009D65B5 /* ThreadJob.mm */,
@@ -751,7 +753,7 @@
751753
1EDE8FAB1A773D810086677B /* main.m in Sources */,
752754
1E4A13C61BA94C730008CBEE /* stringConv.cpp in Sources */,
753755
1E0B2BFD1A7755C400B4FE14 /* PlayerImpl.mm in Sources */,
754-
1E1719841BCE36B90048C3EB /* AlbumViewController.m in Sources */,
756+
1E1719841BCE36B90048C3EB /* AlbumViewController.mm in Sources */,
755757
1ED32D961CA9059C00D0C87D /* PlayerDocument+ScreenSaver.m in Sources */,
756758
1E5F9EC51A7A1979000360D7 /* PlayerEngine.mm in Sources */,
757759
1E4A13C51BA94C730008CBEE /* lrcFch_ting.cpp in Sources */,

uPlayer/AlbumView/AlbumViewController.m

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)