├── .gitignore ├── Demo ├── Demo.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata └── Demo │ ├── AppDelegate.h │ ├── AppDelegate.m │ ├── Base.lproj │ └── MainMenu.xib │ ├── Demo-Info.plist │ ├── Demo-Prefix.pch │ └── main.m ├── HybridWaveformView.h ├── HybridWaveformView.m ├── LICENSE ├── LiveWaveformView.h ├── LiveWaveformView.m ├── Preview.png ├── README.md ├── WaveformView.h ├── WaveformView.m └── WaveformViewShared.h /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | .DS_Store 3 | */build/* 4 | *.pbxuser 5 | !default.pbxuser 6 | *.mode1v3 7 | !default.mode1v3 8 | *.mode2v3 9 | !default.mode2v3 10 | *.perspectivev3 11 | !default.perspectivev3 12 | xcuserdata 13 | profile 14 | *.moved-aside 15 | DerivedData 16 | .idea/ 17 | *.hmap 18 | *.xccheckout 19 | 20 | #CocoaPods 21 | Pods 22 | -------------------------------------------------------------------------------- /Demo/Demo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 966D05B5188D819000830842 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 966D05B4188D819000830842 /* Cocoa.framework */; }; 11 | 966D05C1188D819000830842 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 966D05C0188D819000830842 /* main.m */; }; 12 | 966D05C8188D819000830842 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 966D05C7188D819000830842 /* AppDelegate.m */; }; 13 | 966D05CB188D819000830842 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 966D05C9188D819000830842 /* MainMenu.xib */; }; 14 | 966D05EA188D81D600830842 /* LiveWaveformView.m in Sources */ = {isa = PBXBuildFile; fileRef = 966D05E9188D81D600830842 /* LiveWaveformView.m */; }; 15 | 966D05EC188D833F00830842 /* AVFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 966D05EB188D833F00830842 /* AVFoundation.framework */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXFileReference section */ 19 | 966D05B1188D819000830842 /* Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Demo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 20 | 966D05B4188D819000830842 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; }; 21 | 966D05B7188D819000830842 /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; }; 22 | 966D05B8188D819000830842 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = System/Library/Frameworks/CoreData.framework; sourceTree = SDKROOT; }; 23 | 966D05B9188D819000830842 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; 24 | 966D05BC188D819000830842 /* Demo-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "Demo-Info.plist"; sourceTree = ""; }; 25 | 966D05C0188D819000830842 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 26 | 966D05C2188D819000830842 /* Demo-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Demo-Prefix.pch"; sourceTree = ""; }; 27 | 966D05C6188D819000830842 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 28 | 966D05C7188D819000830842 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 29 | 966D05CA188D819000830842 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/MainMenu.xib; sourceTree = ""; }; 30 | 966D05E8188D81D600830842 /* LiveWaveformView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = LiveWaveformView.h; path = ../../LiveWaveformView.h; sourceTree = ""; }; 31 | 966D05E9188D81D600830842 /* LiveWaveformView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = LiveWaveformView.m; path = ../../LiveWaveformView.m; sourceTree = ""; }; 32 | 966D05EB188D833F00830842 /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; }; 33 | /* End PBXFileReference section */ 34 | 35 | /* Begin PBXFrameworksBuildPhase section */ 36 | 966D05AE188D819000830842 /* Frameworks */ = { 37 | isa = PBXFrameworksBuildPhase; 38 | buildActionMask = 2147483647; 39 | files = ( 40 | 966D05EC188D833F00830842 /* AVFoundation.framework in Frameworks */, 41 | 966D05B5188D819000830842 /* Cocoa.framework in Frameworks */, 42 | ); 43 | runOnlyForDeploymentPostprocessing = 0; 44 | }; 45 | /* End PBXFrameworksBuildPhase section */ 46 | 47 | /* Begin PBXGroup section */ 48 | 966D05A8188D819000830842 = { 49 | isa = PBXGroup; 50 | children = ( 51 | 966D05BA188D819000830842 /* Demo */, 52 | 966D05B3188D819000830842 /* Frameworks */, 53 | 966D05B2188D819000830842 /* Products */, 54 | ); 55 | sourceTree = ""; 56 | }; 57 | 966D05B2188D819000830842 /* Products */ = { 58 | isa = PBXGroup; 59 | children = ( 60 | 966D05B1188D819000830842 /* Demo.app */, 61 | ); 62 | name = Products; 63 | sourceTree = ""; 64 | }; 65 | 966D05B3188D819000830842 /* Frameworks */ = { 66 | isa = PBXGroup; 67 | children = ( 68 | 966D05EB188D833F00830842 /* AVFoundation.framework */, 69 | 966D05B4188D819000830842 /* Cocoa.framework */, 70 | 966D05B6188D819000830842 /* Other Frameworks */, 71 | ); 72 | name = Frameworks; 73 | sourceTree = ""; 74 | }; 75 | 966D05B6188D819000830842 /* Other Frameworks */ = { 76 | isa = PBXGroup; 77 | children = ( 78 | 966D05B7188D819000830842 /* AppKit.framework */, 79 | 966D05B8188D819000830842 /* CoreData.framework */, 80 | 966D05B9188D819000830842 /* Foundation.framework */, 81 | ); 82 | name = "Other Frameworks"; 83 | sourceTree = ""; 84 | }; 85 | 966D05BA188D819000830842 /* Demo */ = { 86 | isa = PBXGroup; 87 | children = ( 88 | 966D05E8188D81D600830842 /* LiveWaveformView.h */, 89 | 966D05E9188D81D600830842 /* LiveWaveformView.m */, 90 | 966D05C6188D819000830842 /* AppDelegate.h */, 91 | 966D05C7188D819000830842 /* AppDelegate.m */, 92 | 966D05C9188D819000830842 /* MainMenu.xib */, 93 | 966D05BB188D819000830842 /* Supporting Files */, 94 | ); 95 | path = Demo; 96 | sourceTree = ""; 97 | }; 98 | 966D05BB188D819000830842 /* Supporting Files */ = { 99 | isa = PBXGroup; 100 | children = ( 101 | 966D05BC188D819000830842 /* Demo-Info.plist */, 102 | 966D05C0188D819000830842 /* main.m */, 103 | 966D05C2188D819000830842 /* Demo-Prefix.pch */, 104 | ); 105 | name = "Supporting Files"; 106 | sourceTree = ""; 107 | }; 108 | /* End PBXGroup section */ 109 | 110 | /* Begin PBXNativeTarget section */ 111 | 966D05B0188D819000830842 /* Demo */ = { 112 | isa = PBXNativeTarget; 113 | buildConfigurationList = 966D05E2188D819000830842 /* Build configuration list for PBXNativeTarget "Demo" */; 114 | buildPhases = ( 115 | 966D05AD188D819000830842 /* Sources */, 116 | 966D05AE188D819000830842 /* Frameworks */, 117 | 966D05AF188D819000830842 /* Resources */, 118 | ); 119 | buildRules = ( 120 | ); 121 | dependencies = ( 122 | ); 123 | name = Demo; 124 | productName = Demo; 125 | productReference = 966D05B1188D819000830842 /* Demo.app */; 126 | productType = "com.apple.product-type.application"; 127 | }; 128 | /* End PBXNativeTarget section */ 129 | 130 | /* Begin PBXProject section */ 131 | 966D05A9188D819000830842 /* Project object */ = { 132 | isa = PBXProject; 133 | attributes = { 134 | LastUpgradeCheck = 0500; 135 | ORGANIZATIONNAME = "Seb Jachec"; 136 | }; 137 | buildConfigurationList = 966D05AC188D819000830842 /* Build configuration list for PBXProject "Demo" */; 138 | compatibilityVersion = "Xcode 3.2"; 139 | developmentRegion = English; 140 | hasScannedForEncodings = 0; 141 | knownRegions = ( 142 | en, 143 | Base, 144 | ); 145 | mainGroup = 966D05A8188D819000830842; 146 | productRefGroup = 966D05B2188D819000830842 /* Products */; 147 | projectDirPath = ""; 148 | projectRoot = ""; 149 | targets = ( 150 | 966D05B0188D819000830842 /* Demo */, 151 | ); 152 | }; 153 | /* End PBXProject section */ 154 | 155 | /* Begin PBXResourcesBuildPhase section */ 156 | 966D05AF188D819000830842 /* Resources */ = { 157 | isa = PBXResourcesBuildPhase; 158 | buildActionMask = 2147483647; 159 | files = ( 160 | 966D05CB188D819000830842 /* MainMenu.xib in Resources */, 161 | ); 162 | runOnlyForDeploymentPostprocessing = 0; 163 | }; 164 | /* End PBXResourcesBuildPhase section */ 165 | 166 | /* Begin PBXSourcesBuildPhase section */ 167 | 966D05AD188D819000830842 /* Sources */ = { 168 | isa = PBXSourcesBuildPhase; 169 | buildActionMask = 2147483647; 170 | files = ( 171 | 966D05EA188D81D600830842 /* LiveWaveformView.m in Sources */, 172 | 966D05C8188D819000830842 /* AppDelegate.m in Sources */, 173 | 966D05C1188D819000830842 /* main.m in Sources */, 174 | ); 175 | runOnlyForDeploymentPostprocessing = 0; 176 | }; 177 | /* End PBXSourcesBuildPhase section */ 178 | 179 | /* Begin PBXVariantGroup section */ 180 | 966D05C9188D819000830842 /* MainMenu.xib */ = { 181 | isa = PBXVariantGroup; 182 | children = ( 183 | 966D05CA188D819000830842 /* Base */, 184 | ); 185 | name = MainMenu.xib; 186 | sourceTree = ""; 187 | }; 188 | /* End PBXVariantGroup section */ 189 | 190 | /* Begin XCBuildConfiguration section */ 191 | 966D05E0188D819000830842 /* Debug */ = { 192 | isa = XCBuildConfiguration; 193 | buildSettings = { 194 | ALWAYS_SEARCH_USER_PATHS = NO; 195 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 196 | CLANG_CXX_LIBRARY = "libc++"; 197 | CLANG_ENABLE_OBJC_ARC = YES; 198 | CLANG_WARN_BOOL_CONVERSION = YES; 199 | CLANG_WARN_CONSTANT_CONVERSION = YES; 200 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 201 | CLANG_WARN_EMPTY_BODY = YES; 202 | CLANG_WARN_ENUM_CONVERSION = YES; 203 | CLANG_WARN_INT_CONVERSION = YES; 204 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 205 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 206 | COPY_PHASE_STRIP = NO; 207 | GCC_C_LANGUAGE_STANDARD = gnu99; 208 | GCC_DYNAMIC_NO_PIC = NO; 209 | GCC_ENABLE_OBJC_EXCEPTIONS = YES; 210 | GCC_OPTIMIZATION_LEVEL = 0; 211 | GCC_PREPROCESSOR_DEFINITIONS = ( 212 | "DEBUG=1", 213 | "$(inherited)", 214 | ); 215 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 216 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 217 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 218 | GCC_WARN_UNDECLARED_SELECTOR = YES; 219 | GCC_WARN_UNINITIALIZED_AUTOS = YES; 220 | GCC_WARN_UNUSED_FUNCTION = YES; 221 | GCC_WARN_UNUSED_VARIABLE = YES; 222 | MACOSX_DEPLOYMENT_TARGET = 10.9; 223 | ONLY_ACTIVE_ARCH = YES; 224 | SDKROOT = macosx; 225 | }; 226 | name = Debug; 227 | }; 228 | 966D05E1188D819000830842 /* Release */ = { 229 | isa = XCBuildConfiguration; 230 | buildSettings = { 231 | ALWAYS_SEARCH_USER_PATHS = NO; 232 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 233 | CLANG_CXX_LIBRARY = "libc++"; 234 | CLANG_ENABLE_OBJC_ARC = YES; 235 | CLANG_WARN_BOOL_CONVERSION = YES; 236 | CLANG_WARN_CONSTANT_CONVERSION = YES; 237 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 238 | CLANG_WARN_EMPTY_BODY = YES; 239 | CLANG_WARN_ENUM_CONVERSION = YES; 240 | CLANG_WARN_INT_CONVERSION = YES; 241 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 242 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 243 | COPY_PHASE_STRIP = YES; 244 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 245 | ENABLE_NS_ASSERTIONS = NO; 246 | GCC_C_LANGUAGE_STANDARD = gnu99; 247 | GCC_ENABLE_OBJC_EXCEPTIONS = YES; 248 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 249 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 250 | GCC_WARN_UNDECLARED_SELECTOR = YES; 251 | GCC_WARN_UNINITIALIZED_AUTOS = YES; 252 | GCC_WARN_UNUSED_FUNCTION = YES; 253 | GCC_WARN_UNUSED_VARIABLE = YES; 254 | MACOSX_DEPLOYMENT_TARGET = 10.9; 255 | SDKROOT = macosx; 256 | }; 257 | name = Release; 258 | }; 259 | 966D05E3188D819000830842 /* Debug */ = { 260 | isa = XCBuildConfiguration; 261 | buildSettings = { 262 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 263 | COMBINE_HIDPI_IMAGES = YES; 264 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 265 | GCC_PREFIX_HEADER = "Demo/Demo-Prefix.pch"; 266 | INFOPLIST_FILE = "Demo/Demo-Info.plist"; 267 | PRODUCT_NAME = "$(TARGET_NAME)"; 268 | WRAPPER_EXTENSION = app; 269 | }; 270 | name = Debug; 271 | }; 272 | 966D05E4188D819000830842 /* Release */ = { 273 | isa = XCBuildConfiguration; 274 | buildSettings = { 275 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 276 | COMBINE_HIDPI_IMAGES = YES; 277 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 278 | GCC_PREFIX_HEADER = "Demo/Demo-Prefix.pch"; 279 | INFOPLIST_FILE = "Demo/Demo-Info.plist"; 280 | PRODUCT_NAME = "$(TARGET_NAME)"; 281 | WRAPPER_EXTENSION = app; 282 | }; 283 | name = Release; 284 | }; 285 | /* End XCBuildConfiguration section */ 286 | 287 | /* Begin XCConfigurationList section */ 288 | 966D05AC188D819000830842 /* Build configuration list for PBXProject "Demo" */ = { 289 | isa = XCConfigurationList; 290 | buildConfigurations = ( 291 | 966D05E0188D819000830842 /* Debug */, 292 | 966D05E1188D819000830842 /* Release */, 293 | ); 294 | defaultConfigurationIsVisible = 0; 295 | defaultConfigurationName = Release; 296 | }; 297 | 966D05E2188D819000830842 /* Build configuration list for PBXNativeTarget "Demo" */ = { 298 | isa = XCConfigurationList; 299 | buildConfigurations = ( 300 | 966D05E3188D819000830842 /* Debug */, 301 | 966D05E4188D819000830842 /* Release */, 302 | ); 303 | defaultConfigurationIsVisible = 0; 304 | }; 305 | /* End XCConfigurationList section */ 306 | }; 307 | rootObject = 966D05A9188D819000830842 /* Project object */; 308 | } 309 | -------------------------------------------------------------------------------- /Demo/Demo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Demo/Demo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | 2 | // AppDelegate.h 3 | // Demo 4 | 5 | // Created by Seb Jachec on 20/01/2014. 6 | // Copyright (c) 2014 Seb Jachec. All rights reserved. 7 | 8 | #import 9 | 10 | @class LiveWaveformView; 11 | 12 | @interface AppDelegate : NSObject 13 | 14 | @property (strong) IBOutlet NSWindow *window; 15 | @property (strong) IBOutlet LiveWaveformView *waveformView; 16 | 17 | @end 18 | -------------------------------------------------------------------------------- /Demo/Demo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | 2 | // AppDelegate.m 3 | // Demo 4 | 5 | // Created by Seb Jachec on 20/01/2014. 6 | // Copyright (c) 2014 Seb Jachec. All rights reserved. 7 | 8 | #import "AppDelegate.h" 9 | #import "LiveWaveformView.h" 10 | 11 | #import 12 | 13 | @implementation AppDelegate 14 | 15 | - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { 16 | //Set colours 17 | _waveformView.backgroundColor = [NSColor colorWithCalibratedRed:0.21 green:0.49 blue:0.89 alpha:1.00]; 18 | _waveformView.foregroundColor = [NSColor colorWithCalibratedWhite:0.0 alpha:0.2]; 19 | _waveformView.inactiveColor = [NSColor colorWithCalibratedRed:0.07 green:0.11 blue:0.20 alpha:1.00]; 20 | 21 | 22 | //Setup an AVAudioRecorder with the save URL and recording settings 23 | NSURL *saveURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@%@.wav",NSTemporaryDirectory(), NSDate.date.description]]; 24 | NSLog(@"Saving to %@",saveURL.path); 25 | 26 | NSDictionary *settings = @{AVSampleRateKey:@(44100.0), 27 | AVFormatIDKey:@(kAudioFormatLinearPCM), 28 | AVNumberOfChannelsKey:@2.0}; 29 | 30 | AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:saveURL settings:settings error:nil]; 31 | 32 | //Attach the recorder to the waveform view 33 | _waveformView.recorder = recorder; 34 | //Alternatively: 35 | //[_waveformView attachToRecorder:recorder]; 36 | 37 | 38 | //Start recording 39 | [_waveformView recordForDuration:15.0 FinishBlock:^{ 40 | //Note: Following lines are not recommended, purely for demo purposes 41 | [NSWorkspace.sharedWorkspace activateFileViewerSelectingURLs:@[saveURL]]; 42 | [_window close]; 43 | NSLog(@"Finished recording!"); 44 | }]; 45 | } 46 | 47 | - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender { 48 | return YES; 49 | } 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /Demo/Demo/Base.lproj/MainMenu.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | Default 522 | 523 | 524 | 525 | 526 | 527 | 528 | Left to Right 529 | 530 | 531 | 532 | 533 | 534 | 535 | Right to Left 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | Default 547 | 548 | 549 | 550 | 551 | 552 | 553 | Left to Right 554 | 555 | 556 | 557 | 558 | 559 | 560 | Right to Left 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | -------------------------------------------------------------------------------- /Demo/Demo/Demo-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIconFile 10 | 11 | CFBundleIdentifier 12 | me.sebj.${PRODUCT_NAME:rfc1034identifier} 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | ${PRODUCT_NAME} 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1 25 | LSMinimumSystemVersion 26 | ${MACOSX_DEPLOYMENT_TARGET} 27 | NSHumanReadableCopyright 28 | Copyright © 2014 Seb Jachec. All rights reserved. 29 | NSMainNibFile 30 | MainMenu 31 | NSPrincipalClass 32 | NSApplication 33 | 34 | 35 | -------------------------------------------------------------------------------- /Demo/Demo/Demo-Prefix.pch: -------------------------------------------------------------------------------- 1 | // 2 | // Prefix header 3 | // 4 | // The contents of this file are implicitly included at the beginning of every source file. 5 | // 6 | 7 | #ifdef __OBJC__ 8 | #import 9 | #endif 10 | -------------------------------------------------------------------------------- /Demo/Demo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // Demo 4 | // 5 | // Created by Seb Jachec on 20/01/2014. 6 | // Copyright (c) 2014 Seb Jachec. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | int main(int argc, const char * argv[]) 12 | { 13 | return NSApplicationMain(argc, argv); 14 | } 15 | -------------------------------------------------------------------------------- /HybridWaveformView.h: -------------------------------------------------------------------------------- 1 | 2 | // HybridWaveformView.h 3 | 4 | // Created by Seb Jachec on 20/11/2013. 5 | // Copyright (c) 2013 Seb Jachec. All rights reserved. 6 | 7 | #import "LiveWaveformView.h" 8 | 9 | @class WaveformView; 10 | 11 | /** 12 | * Bridges WaveformView, which just takes saved files, and LiveWaveformView, which plots sound live. 13 | */ 14 | @interface HybridWaveformView : LiveWaveformView 15 | 16 | /** 17 | * Access the WaveformView (files only) to direct all trim, play, stop etc. commands to it. 18 | */ 19 | @property (strong, nullable) WaveformView *fileView; 20 | 21 | 22 | /** 23 | * Properties and selectors from the file waveform view 24 | * (This view takes over responsibility for some things, forwarding back) 25 | * See WaveformView.h 26 | */ 27 | 28 | IBInspectable @property BOOL trimEnabled; 29 | 30 | IBInspectable @property (strong, nullable) NSColor *trimHandleColor; 31 | IBInspectable @property (strong, nullable) NSColor *inactiveColor; 32 | 33 | @property (weak, nullable) id delegate; 34 | 35 | @end 36 | -------------------------------------------------------------------------------- /HybridWaveformView.m: -------------------------------------------------------------------------------- 1 | 2 | // HybridWaveformView.m 3 | 4 | // Created by Seb Jachec on 20/11/2013. 5 | // Copyright (c) 2013 Seb Jachec. All rights reserved. 6 | 7 | #import "HybridWaveformView.h" 8 | #import "WaveformView.h" 9 | 10 | @implementation HybridWaveformView 11 | 12 | @dynamic inactiveColor; 13 | 14 | - (instancetype)initWithFrame:(NSRect)frame { 15 | self = [super initWithFrame:frame]; 16 | if (self) { 17 | [self addTrackingRect:_bounds owner:self userData:nil assumeInside:NO]; 18 | } 19 | return self; 20 | } 21 | 22 | - (instancetype)initWithCoder:(NSCoder *)coder { 23 | self = [super initWithCoder:coder]; 24 | if (self) { 25 | [self addTrackingRect:_bounds owner:self userData:nil assumeInside:NO]; 26 | } 27 | return self; 28 | } 29 | 30 | - (void)mouseEntered:(NSEvent *)theEvent { 31 | if (_delegate && [_delegate respondsToSelector:@selector(mouseEntered:)]) { 32 | [_delegate mouseEntered:theEvent]; 33 | } 34 | } 35 | 36 | - (void)mouseExited:(NSEvent *)theEvent { 37 | if (_delegate && [_delegate respondsToSelector:@selector(mouseExited:)]) { 38 | [_delegate mouseExited:theEvent]; 39 | } 40 | } 41 | 42 | #if TARGET_INTERFACE_BUILDER 43 | - (void)drawRect:(NSRect)dirtyRect { 44 | [NSColor.whiteColor set]; 45 | NSRectFill(_bounds); 46 | } 47 | #endif 48 | 49 | //Overriding from LiveWaveformView superclass 50 | - (void)finishedRecording { 51 | [_fileView loadURL:self.recorder.url]; 52 | 53 | _fileView = [[WaveformView alloc] initWithFrame:_frame]; 54 | _fileView.foregroundColor = self.foregroundColor; 55 | _fileView.backgroundColor = self.backgroundColor; 56 | _fileView.trimEnabled = self.trimEnabled; 57 | _fileView.trimHandleColor = self.trimHandleColor; 58 | _fileView.inactiveColor = self.inactiveColor; 59 | _fileView.drawsCenterLine = self.drawsCenterLine; 60 | 61 | [self.superview addSubview:_fileView positioned:NSWindowBelow relativeTo:self]; 62 | [self.animator setAlphaValue:0]; 63 | 64 | [self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.251]; 65 | } 66 | 67 | @end 68 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Use as you please! 2 | 3 | Crediting "Seb Jachec" with a link to my Twitter profile (https://twitter.com/iamsebj) would be nice if this code is useful, but is not necessary. -------------------------------------------------------------------------------- /LiveWaveformView.h: -------------------------------------------------------------------------------- 1 | 2 | // LiveWaveformView.h 3 | 4 | // Created by Seb Jachec on 19/11/2013. 5 | // Copyright (c) 2013 Seb Jachec. All rights reserved. 6 | 7 | #import 8 | #import 9 | 10 | /** 11 | * Give it an AVAudioRecorder and watch it plot a waveform - live. Customise foreground and background colours. Redirects AVAudioRecorderDelegate methods back to "original" delegate. 12 | */ 13 | IB_DESIGNABLE 14 | @interface LiveWaveformView : NSView { 15 | NSMutableArray *samples; 16 | 17 | id originalDelegate; 18 | 19 | NSTimer *refreshTimer; 20 | 21 | void(^finishBlock)(); 22 | } 23 | 24 | /** 25 | * Width to plot each sample - default is 2.0. 26 | */ 27 | IBInspectable @property float sampleWidth; 28 | 29 | IBInspectable @property (strong, nullable) NSColor *foregroundColor; 30 | IBInspectable @property (strong, nullable) NSColor *backgroundColor; 31 | IBInspectable @property (strong, nullable) NSColor *inactiveColor; 32 | 33 | IBInspectable @property BOOL drawsCenterLine; 34 | 35 | /* 36 | * View takes control of the recorder. Recommended to use methods below. 37 | */ 38 | @property (strong, nullable, setter = attachToRecorder:) AVAudioRecorder *recorder; 39 | 40 | /** 41 | * The following methods take over from AVAudioRecorder: 42 | */ 43 | 44 | - (void)record; 45 | - (void)recordForDuration:(NSTimeInterval)aDuration; 46 | - (void)recordForDuration:(NSTimeInterval)aDuration FinishBlock:(nullable void (^)())aBlock; 47 | 48 | - (void)stop; 49 | 50 | @end 51 | -------------------------------------------------------------------------------- /LiveWaveformView.m: -------------------------------------------------------------------------------- 1 | 2 | // LiveWaveformView.m 3 | 4 | // Created by Seb Jachec on 19/11/2013. 5 | // Copyright (c) 2013 Seb Jachec. All rights reserved. 6 | 7 | #import "LiveWaveformView.h" 8 | #import "math.h" 9 | #import "WaveformViewShared.h" 10 | 11 | @implementation LiveWaveformView 12 | 13 | //Style inspired by Arduino's map function 14 | double map(double x, double in_min, double in_max, double out_min, double out_max) { 15 | //Math from http://stackoverflow.com/a/5732390/447697 16 | double slope = 1.0 * (out_max - out_min) / (in_max - in_min); 17 | return out_min + slope * (x - in_min); 18 | } 19 | 20 | - (instancetype)initWithFrame:(NSRect)frame { 21 | self = [super initWithFrame:frame]; 22 | if (self) [self setup]; 23 | return self; 24 | } 25 | 26 | - (instancetype)initWithCoder:(NSCoder *)coder { 27 | self = [super initWithCoder:coder]; 28 | if (self) [self setup]; 29 | return self; 30 | } 31 | 32 | - (void)setup { 33 | _sampleWidth = 2.0f; 34 | _backgroundColor = DefaultBackgroundColor; 35 | _foregroundColor = DefaultForegroundColor; 36 | _inactiveColor = DefaultInactiveColor; 37 | } 38 | 39 | #pragma mark Recording 40 | 41 | - (void)prepareToRecord { 42 | samples = [NSMutableArray new]; 43 | 44 | _recorder.meteringEnabled = YES; 45 | 46 | if (_recorder.delegate) originalDelegate = _recorder.delegate; 47 | 48 | _recorder.delegate = self; 49 | } 50 | 51 | - (void)record { 52 | [self recordForDuration:0.0 FinishBlock:nil]; 53 | } 54 | 55 | - (void)recordForDuration:(NSTimeInterval)aDuration { 56 | [self recordForDuration:aDuration FinishBlock:nil]; 57 | } 58 | 59 | - (void)recordForDuration:(NSTimeInterval)aDuration FinishBlock:(nullable void (^)())aBlock { 60 | [self prepareToRecord]; 61 | 62 | if (aDuration > 0.0) { 63 | [_recorder recordForDuration:aDuration]; 64 | 65 | } else { 66 | [_recorder record]; 67 | } 68 | 69 | // 40/s 70 | refreshTimer = [NSTimer scheduledTimerWithTimeInterval:0.025 target:self selector:@selector(refresh) userInfo:nil repeats:YES]; 71 | 72 | finishBlock = aBlock; 73 | } 74 | 75 | - (void)stop { 76 | [_recorder stop]; 77 | } 78 | 79 | #pragma mark Delegate Methods 80 | 81 | - (void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error { 82 | //Redirect to original delegate 83 | 84 | if (originalDelegate && [originalDelegate respondsToSelector:@selector(audioRecorderEncodeErrorDidOccur:error:)]) { 85 | SEL aSelector = NSSelectorFromString(@"audioRecorderEncodeErrorDidOccur:error:"); 86 | 87 | NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[originalDelegate methodSignatureForSelector:aSelector]]; 88 | [inv setSelector:aSelector]; 89 | [inv setTarget:originalDelegate]; 90 | 91 | [inv setArgument:&(_recorder) atIndex:2]; //Arguments 0 and 1 are self and _cmd, automatically set 92 | [inv setArgument:&(error) atIndex:3]; //Arguments 0 and 1 are self and _cmd, automatically set 93 | 94 | [inv invoke]; 95 | } 96 | } 97 | 98 | - (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag { 99 | //Redirect to original delegate 100 | 101 | if (originalDelegate && [originalDelegate respondsToSelector:@selector(audioRecorderDidFinishRecording:successfully:)]) { 102 | SEL aSelector = NSSelectorFromString(@"audioRecorderDidFinishRecording:successfully:"); 103 | 104 | NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[originalDelegate methodSignatureForSelector:aSelector]]; 105 | [inv setSelector:aSelector]; 106 | [inv setTarget:originalDelegate]; 107 | 108 | [inv setArgument:&(_recorder) atIndex:2]; //Arguments 0 and 1 are self and _cmd, automatically set 109 | [inv setArgument:&(flag) atIndex:3]; //Arguments 0 and 1 are self and _cmd, automatically set 110 | [inv invoke]; 111 | } 112 | 113 | [refreshTimer invalidate]; 114 | refreshTimer = nil; 115 | 116 | if (finishBlock) finishBlock(); 117 | 118 | [self finishedRecording]; 119 | } 120 | 121 | - (void)finishedRecording { 122 | //Can be overridden by subclasses 123 | } 124 | 125 | #pragma mark Other 126 | 127 | - (void)refresh { 128 | 129 | if (_recorder && _recorder.isRecording) { 130 | if (floor(samples.count*_sampleWidth) > ceil(_bounds.size.width)) [samples removeObjectAtIndex:0]; 131 | 132 | [_recorder updateMeters]; 133 | [samples addObject:@(round([_recorder averagePowerForChannel:0]))]; 134 | 135 | self.needsDisplay = YES; 136 | } 137 | } 138 | 139 | - (void)viewDidEndLiveResize { 140 | #ifdef DEBUG 141 | NSLog(@"LiveWaveformView: Resize ended"); 142 | #endif 143 | 144 | self.needsDisplay = YES; 145 | } 146 | 147 | - (void)drawRect:(NSRect)dirtyRect { 148 | [_backgroundColor? _backgroundColor : DefaultBackgroundColor set]; 149 | NSRectFill(_bounds); 150 | 151 | if (self.window && !(self.window.occlusionState & NSWindowOcclusionStateVisible)) { 152 | return; 153 | } 154 | 155 | if (samples.count>1) { 156 | [_foregroundColor? _foregroundColor : DefaultForegroundColor set]; 157 | 158 | for (u_int16_t i = 0; i= -60) { 166 | height = (map(sample, -60, 0, 0, 1))*_bounds.size.height; 167 | } 168 | 169 | NSRect rect = NSMakeRect(i*_sampleWidth, (_bounds.size.height-height)/2, _sampleWidth, height); 170 | NSRectFillUsingOperation(rect, NSCompositeSourceOver); 171 | } 172 | 173 | if (_drawsCenterLine) { 174 | //[_foregroundColor? _foregroundColor : DefaultForegroundColor setFill]; 175 | 176 | NSBezierPath *centerLine = [NSBezierPath bezierPathWithRect:NSMakeRect(0, round((_bounds.size.height/2)-1), _bounds.size.width, 2)]; 177 | [centerLine fill]; 178 | } 179 | 180 | float x = round(samples.count*_sampleWidth); 181 | NSRect darken = NSMakeRect(x, 0, _bounds.size.width-x, _bounds.size.height); 182 | [_inactiveColor? _inactiveColor : DefaultInactiveColor set]; 183 | NSRectFill(darken); 184 | } 185 | } 186 | 187 | @end 188 | -------------------------------------------------------------------------------- /Preview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sebj/WaveformView/0d040251781f13a8e1f37da5253019d803e40bf6/Preview.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | WaveformView 2 | ============ 3 | ![WaveformView Preview](https://github.com/sebj/WaveformView/blob/master/Preview.png?raw=true) 4 | 5 | My take on an NSView subclass that can display the waveform for an audio file, allowing customisability of colors, play/stop control and image generation (from the view). 6 | 7 | I'd recommend looking through the code and adapting/modifying to suit your needs. 8 | 9 | **Note** 10 | If you're looking for an extremely accurate high performance visualization of a sound file or live sound recording, there are most likely alternatives that would better suit you. 11 | 12 | ### Classes 13 | 14 | --- 15 | 16 | ```WaveformView``` is a general-purpose waveform view to visualize a .wav file. 17 | 18 | --- 19 | 20 | ```LiveWaveformView``` will show a live waveform for a given ```AVAudioRecorder```. 21 | 22 | --- 23 | 24 | ```HybridWaveformView``` is a hybrid/combination of both of these - it's a bit experimental and not perfect. It should display a rough "live" waveform, then switch to an accurate waveform once recording has stopped and the sound recording files has been saved and loaded. 25 | -------------------------------------------------------------------------------- /WaveformView.h: -------------------------------------------------------------------------------- 1 | 2 | // WaveformView.h 3 | 4 | // Created by Seb Jachec on 16/11/2013. 5 | // Copyright (c) 2013 Seb Jachec. All rights reserved. 6 | 7 | #import 8 | #import 9 | 10 | /** 11 | * Give it a .wav and watch it plot - customise all colours, and enable trimming handles 12 | */ 13 | IB_DESIGNABLE 14 | @interface WaveformView : NSView 15 | 16 | /** 17 | * Returns sound duration in seconds 18 | */ 19 | @property (readonly) double duration; 20 | 21 | IBInspectable @property BOOL trimEnabled; 22 | /** 23 | * Returns time range using trimming handle. If trimming is disabled, trimRange is kkCMTimeZero, kCMTimePositiveInfinity 24 | */ 25 | @property (readonly) CMTimeRange trimRange; 26 | 27 | @property (strong, nullable) IBInspectable NSColor *foregroundColor; 28 | @property (strong, nullable) IBInspectable NSColor *backgroundColor; 29 | @property (strong, nullable) IBInspectable NSColor *trimHandleColor; 30 | @property (strong, nullable) IBInspectable NSColor *inactiveColor; 31 | 32 | IBInspectable @property BOOL drawsCenterLine; 33 | 34 | - (BOOL)loadFileWithPath:(nullable NSString*)filePath; 35 | - (BOOL)loadURL:(nullable NSURL*)aURL; 36 | 37 | - (void)play; 38 | - (void)stop; 39 | 40 | /** 41 | * Returns an NSImage with the waveform, the same size as this view's bounds 42 | */ 43 | - (nonnull NSImage*)waveformImage; 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /WaveformView.m: -------------------------------------------------------------------------------- 1 | 2 | // WaveformView.m 3 | 4 | // Created by Seb Jachec on 16/11/2013. 5 | // Copyright (c) 2013 Seb Jachec. All rights reserved. 6 | 7 | #import 8 | #import "WaveformView.h" 9 | #import "WaveformViewShared.h" 10 | 11 | #pragma mark - Trim Slider 12 | 13 | #define TrimSliderKnobHeight 22 14 | #define TrimSliderKnobWidth 6 15 | 16 | #define DefaultTrimHandleColor NSColor.grayColor 17 | 18 | @interface TrimSliderCell : NSSliderCell 19 | @property (strong) NSColor *color; 20 | @end 21 | 22 | @implementation TrimSliderCell 23 | 24 | //Solution to out-of-sync slider: http://stackoverflow.com/a/8617184/447697 25 | - (NSRect)knobRectFlipped:(BOOL)flipped { 26 | CGFloat value = (self.doubleValue-_minValue)/(_maxValue-_minValue); 27 | NSRect defaultRect = [super knobRectFlipped:flipped]; 28 | NSRect myRect = NSMakeRect(0, 0, TrimSliderKnobWidth, TrimSliderKnobHeight); 29 | 30 | myRect.origin.x = round(value * (self.controlView.frame.size.width - TrimSliderKnobWidth)); 31 | myRect.origin.y = round(defaultRect.origin.y + defaultRect.size.height/2.0 - myRect.size.height/2.0); 32 | 33 | return myRect; 34 | } 35 | 36 | - (void)drawKnob:(NSRect)knobRect { 37 | [_color set]; 38 | NSRectFillUsingOperation(knobRect, NSCompositeSourceOver); 39 | } 40 | 41 | - (void)drawBarInside:(NSRect)frame flipped:(BOOL)flipped { } 42 | 43 | - (BOOL)isOpaque { 44 | return NO; 45 | } 46 | 47 | @end 48 | 49 | 50 | #define mark - WaveformView 51 | 52 | @interface WaveformView () { 53 | AVURLAsset *currentAsset; 54 | 55 | NSMutableArray *points; 56 | NSImage *cacheImage; 57 | 58 | float secondInPixels; 59 | NSSlider *trimSlider; 60 | 61 | AVAudioPlayer *player; 62 | NSTimer *stopTimer; 63 | } 64 | @end 65 | 66 | #define absX(x) ((x)<0?0-(x):(x)) 67 | #define minMaxX(x,mn,mx) ((x)<=(mn)?(mn):((x)>=(mx)?(mx):(x))) 68 | #define noiseFloor (-50.0) 69 | #define decibel(amplitude) (20.0 * log10(absX(amplitude)/32767.0)) 70 | 71 | #define Settings @{AVFormatIDKey:@(kAudioFormatLinearPCM), AVNumberOfChannelsKey:@2.0} 72 | 73 | @implementation WaveformView 74 | 75 | - (instancetype)initWithFrame:(NSRect)frame { 76 | self = [super initWithFrame:frame]; 77 | if (self) { 78 | [self setup]; 79 | } 80 | return self; 81 | } 82 | 83 | - (instancetype)initWithCoder:(NSCoder *)coder { 84 | self = [super initWithCoder:coder]; 85 | if (self) { 86 | [self setup]; 87 | } 88 | return self; 89 | } 90 | 91 | - (void)setup { 92 | [self addObserver:self forKeyPath:@"trimEnabled" options:0 context:NULL]; 93 | 94 | trimSlider = [[NSSlider alloc] initWithFrame:NSMakeRect(0, (_bounds.size.height-21)/2, 1, 21)]; 95 | trimSlider.cell = [TrimSliderCell new]; 96 | trimSlider.target = self; 97 | trimSlider.action = @selector(sliderChanged); 98 | 99 | _backgroundColor = DefaultBackgroundColor; 100 | _foregroundColor = DefaultForegroundColor; 101 | _inactiveColor = DefaultInactiveColor; 102 | } 103 | 104 | #pragma mark Load files 105 | 106 | - (BOOL)loadFileWithPath:(nullable NSString*)filePath { 107 | return [self loadURL:[NSURL fileURLWithPath:filePath]]; 108 | } 109 | 110 | - (BOOL)loadURL:(nullable NSURL*)aURL{ 111 | currentAsset = [AVURLAsset URLAssetWithURL:aURL options:nil]; 112 | return [self processWaveformForAsset:currentAsset]; 113 | } 114 | 115 | #pragma mark Player 116 | 117 | - (void)play { 118 | [self stop]; 119 | 120 | player = [[AVAudioPlayer alloc] initWithContentsOfURL:currentAsset.URL error:NULL]; 121 | 122 | [player play]; 123 | 124 | float durationToPlay = trimSlider.doubleValue; 125 | float remainder = durationToPlay-player.currentTime; 126 | 127 | [stopTimer invalidate]; 128 | stopTimer = nil; 129 | 130 | stopTimer = [NSTimer scheduledTimerWithTimeInterval:remainder target:player selector:@selector(stop) userInfo:NULL repeats:NO]; 131 | } 132 | 133 | - (void)stop { 134 | if (player && player.isPlaying) [player stop]; 135 | } 136 | 137 | #pragma mark React to Changes 138 | 139 | - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 140 | if (object == self && [keyPath isEqualToString:@"trimEnabled"]) 141 | [self updateTrimSlider]; 142 | } 143 | 144 | - (void)sliderChanged { 145 | //Constrain slider 146 | if (trimSlider.doubleValue <= 0.5) [trimSlider setDoubleValue:0.5]; 147 | 148 | self.needsDisplay = YES; 149 | _trimRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(trimSlider.doubleValue, 1)); 150 | } 151 | 152 | - (void)viewDidEndLiveResize { 153 | #ifdef DEBUG 154 | NSLog(@"Resize ended"); 155 | #endif 156 | 157 | cacheImage = nil; 158 | [self processWaveformForAsset:currentAsset]; 159 | } 160 | 161 | #pragma mark Drawing, Processing 162 | 163 | - (void)drawRect:(NSRect)dirtyRect { 164 | [_backgroundColor? _backgroundColor : DefaultBackgroundColor set]; 165 | NSRectFill(_bounds); 166 | 167 | #if TARGET_INTERFACE_BUILDER 168 | return; 169 | #endif 170 | 171 | if (self.window && !(self.window.occlusionState & NSWindowOcclusionStateVisible)) { 172 | return; 173 | } 174 | 175 | if (currentAsset) { 176 | if (points) { 177 | if (!cacheImage) cacheImage = [self waveformImage]; 178 | 179 | //Stretch image vertically 180 | if (!NSEqualSizes(_bounds.size, cacheImage.size)) { 181 | NSImage *image = [[NSImage alloc] initWithSize:_bounds.size]; 182 | [image lockFocus]; 183 | NSGraphicsContext *ctxt = NSGraphicsContext.currentContext; 184 | ctxt.shouldAntialias = NO; 185 | ctxt.imageInterpolation = NSImageInterpolationNone; 186 | 187 | [cacheImage drawInRect:NSMakeRect(0, 0, cacheImage.size.width, _bounds.size.height)]; 188 | 189 | ctxt.shouldAntialias = YES; 190 | ctxt.imageInterpolation = NSImageInterpolationDefault; 191 | [image unlockFocus]; 192 | 193 | cacheImage = image; 194 | } 195 | 196 | [cacheImage drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0]; 197 | 198 | //Visualise trim 199 | if (_trimEnabled) { 200 | double x = secondInPixels * trimSlider.doubleValue; 201 | NSRect shadeRect = NSMakeRect(x, 0, _bounds.size.width-x, _bounds.size.height); 202 | [_inactiveColor? _inactiveColor : DefaultInactiveColor set]; 203 | NSRectFill(shadeRect); 204 | } 205 | } 206 | } 207 | } 208 | 209 | - (nonnull NSImage*)waveformImage { 210 | NSImage *image = [[NSImage alloc] initWithSize:_bounds.size]; 211 | [image lockFocus]; 212 | [self drawWaveform]; 213 | [image unlockFocus]; 214 | 215 | return image; 216 | } 217 | 218 | - (void)drawWaveform { 219 | 220 | if (points) { 221 | NSBezierPath *path = [NSBezierPath bezierPath]; 222 | 223 | int i = 0; 224 | for (NSValue *val in points) { 225 | if ((i%2)==0) { 226 | [path moveToPoint:val.pointValue]; 227 | 228 | } else { 229 | [path lineToPoint:val.pointValue]; 230 | } 231 | i++; 232 | } 233 | [path closePath]; 234 | 235 | secondInPixels = fabs(((NSValue*)points.lastObject).pointValue.x/(float)_duration); 236 | [self updateTrimSlider]; 237 | 238 | [_foregroundColor? _foregroundColor : DefaultForegroundColor setStroke]; 239 | [_foregroundColor? _foregroundColor : DefaultForegroundColor setFill]; 240 | path.lineWidth = 1.5f; 241 | 242 | NSGraphicsContext *ctxt = NSGraphicsContext.currentContext; 243 | ctxt.shouldAntialias = NO; 244 | 245 | [path stroke]; 246 | 247 | if (_drawsCenterLine) { 248 | NSBezierPath *centerLine = [NSBezierPath bezierPathWithRect:NSMakeRect(0, round((_bounds.size.height/2)-1), _bounds.size.width, 2)]; 249 | [centerLine fill]; 250 | } 251 | 252 | ctxt.shouldAntialias = YES; 253 | } 254 | } 255 | 256 | - (void)updateTrimSlider { 257 | if (_trimEnabled) { 258 | float furthestX = ((NSValue*)points.lastObject).pointValue.x; 259 | [trimSlider setFrame:NSMakeRect(0, (_bounds.size.height-21)/2, furthestX, 21)]; 260 | trimSlider.maxValue = _duration; 261 | trimSlider.doubleValue = _duration; 262 | ((TrimSliderCell*)trimSlider.cell).color = _trimHandleColor? _trimHandleColor : DefaultTrimHandleColor; 263 | } 264 | 265 | if (!_trimEnabled && trimSlider.superview == self) { 266 | [trimSlider removeFromSuperview]; 267 | _trimRange = CMTimeRangeMake(kCMTimeZero, kCMTimePositiveInfinity); 268 | 269 | } else if (_trimEnabled && trimSlider.superview != self) { 270 | [self addSubview:trimSlider]; 271 | _trimRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(_duration, 1)); 272 | self.needsDisplay = YES; 273 | } 274 | 275 | } 276 | 277 | //From FDWaveformView by William Entriken - https://github.com/fulldecent/FDWaveformView/blob/master/FDWaveformView/FDWaveformView.m 278 | //Only takes left channel of sound only for quick rough plot of sound 279 | - (BOOL)processWaveformForAsset:(AVURLAsset *)songAsset { 280 | if (!currentAsset) return NO; 281 | 282 | #ifdef DEBUG 283 | NSLog(@"WaveformView: Started processing"); 284 | #endif 285 | 286 | NSError *error; 287 | 288 | AVAssetReader *reader = [[AVAssetReader alloc] initWithAsset:songAsset error:&error]; 289 | 290 | if (error) { 291 | NSLog(@"WaveformView AVAssetReader Error: %@",error); 292 | return NO; 293 | } 294 | 295 | AVAssetTrack *songTrack = (songAsset.tracks)[0]; 296 | _duration = CMTimeGetSeconds(songTrack.timeRange.duration); 297 | 298 | AVAssetReaderTrackOutput *output = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:songTrack outputSettings:Settings]; 299 | output.alwaysCopiesSampleData = NO; 300 | [reader addOutput:output]; 301 | 302 | UInt32 sampleRate, channelCount; 303 | 304 | NSArray *formatDesc = songTrack.formatDescriptions; 305 | for (unsigned int i = 0; i < formatDesc.count; ++i) { 306 | CMAudioFormatDescriptionRef item = (__bridge CMAudioFormatDescriptionRef)formatDesc[i]; 307 | const AudioStreamBasicDescription *fmtDesc = CMAudioFormatDescriptionGetStreamBasicDescription(item); 308 | if (fmtDesc) { 309 | sampleRate = fmtDesc->mSampleRate; 310 | channelCount = fmtDesc->mChannelsPerFrame; 311 | } 312 | } 313 | 314 | UInt32 bytesPerSample = 2*channelCount; 315 | Float32 normalizeMax = noiseFloor; 316 | NSMutableData *fullSongData = [NSMutableData new]; 317 | [reader startReading]; 318 | 319 | UInt64 totalBytes = 0; 320 | 321 | Float64 totalLeft = 0; 322 | Float32 sampleTally = 0; 323 | 324 | NSInteger samplesPerPixel = sampleRate/50; 325 | 326 | while (reader.status == AVAssetReaderStatusReading){ 327 | AVAssetReaderTrackOutput *trackOutput = (AVAssetReaderTrackOutput*)(reader.outputs)[0]; 328 | CMSampleBufferRef sampleBufferRef = [trackOutput copyNextSampleBuffer]; 329 | 330 | if (sampleBufferRef){ 331 | CMBlockBufferRef blockBufferRef = CMSampleBufferGetDataBuffer(sampleBufferRef); 332 | 333 | size_t bufferLength = CMBlockBufferGetDataLength(blockBufferRef); 334 | totalBytes += bufferLength; 335 | 336 | void *data = malloc(bufferLength); 337 | CMBlockBufferCopyDataBytes(blockBufferRef, 0, bufferLength, data); 338 | 339 | SInt16 *samples = (SInt16 *)data; 340 | unsigned long sampleCount = bufferLength/bytesPerSample; 341 | for (int i = 0; i < sampleCount; i++) { 342 | 343 | Float32 left = (Float32) *samples++; 344 | left = decibel(left); 345 | left = minMaxX(left,noiseFloor,0); 346 | 347 | totalLeft += left; 348 | 349 | sampleTally++; 350 | 351 | if (sampleTally > samplesPerPixel) { 352 | 353 | left = totalLeft/sampleTally; 354 | if (left > normalizeMax) normalizeMax = left; 355 | 356 | [fullSongData appendBytes:&left length:sizeof(left)]; 357 | 358 | totalLeft = 0; 359 | sampleTally = 0; 360 | } 361 | } 362 | 363 | CMSampleBufferInvalidate(sampleBufferRef); 364 | CFRelease(sampleBufferRef); 365 | free(data); 366 | } 367 | } 368 | 369 | if (reader.status == AVAssetReaderStatusCompleted){ 370 | Float32 *samples = (Float32*)fullSongData.bytes; 371 | NSInteger sampleCount = fullSongData.length/(sizeof(Float32)*2); 372 | 373 | //Actually just taking left channel values to plot 374 | 375 | float centerLeft = _bounds.size.height/2; 376 | float sampleAdjustmentFactor = fabs((_bounds.size.height/(normalizeMax-noiseFloor)/2)); 377 | 378 | points = [NSMutableArray new]; 379 | 380 | for (NSInteger intSample = 0; intSample < sampleCount; intSample++) { 381 | Float32 left = *samples++; 382 | float pixels = (left - noiseFloor) * sampleAdjustmentFactor; 383 | [points addObject:[NSValue valueWithPoint:NSMakePoint(intSample, centerLeft-pixels)]]; 384 | [points addObject:[NSValue valueWithPoint:NSMakePoint(intSample, centerLeft+pixels)]]; 385 | } 386 | 387 | #ifdef DEBUG 388 | NSLog(@"WaveformView: Finished processing"); 389 | #endif 390 | 391 | self.needsDisplay = YES; 392 | 393 | return YES; 394 | 395 | } else if (reader.status == AVAssetReaderStatusFailed || reader.status == AVAssetReaderStatusUnknown){ 396 | NSLog(@"WaveformView AVAssetReader%@",reader.error? [NSString stringWithFormat:@" Error: %@",reader.error] : @""); 397 | } 398 | return NO; 399 | } 400 | 401 | @end 402 | -------------------------------------------------------------------------------- /WaveformViewShared.h: -------------------------------------------------------------------------------- 1 | 2 | // WaveformViewShared.h 3 | 4 | // Created by Sebastian Jachec on 08/08/2015. 5 | // Copyright (c) 2015 Seb Jachec. All rights reserved. 6 | 7 | #define DefaultBackgroundColor NSColor.whiteColor 8 | #define DefaultForegroundColor NSColor.blackColor 9 | #define DefaultInactiveColor [NSColor colorWithCalibratedWhite:0.1 alpha:1.0] --------------------------------------------------------------------------------