Skip to content

Commit 8046eb2

Browse files
GeraldGerald
Gerald
authored and
Gerald
committed
initial commit
0 parents  commit 8046eb2

File tree

22 files changed

+1552
-0
lines changed

22 files changed

+1552
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
__pycache__
2+
*.pyc
3+
*~

Pytorch-CoreML-Spectrogram.xcodeproj/project.pbxproj

Lines changed: 498 additions & 0 deletions
Large diffs are not rendered by default.

Pytorch-CoreML-Spectrogram.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>SchemeUserState</key>
6+
<dict>
7+
<key>Pytorch-CoreML-Spectrogram.xcscheme_^#shared#^_</key>
8+
<dict>
9+
<key>orderHint</key>
10+
<integer>0</integer>
11+
</dict>
12+
</dict>
13+
</dict>
14+
</plist>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// AppDelegate.swift
3+
// Pytorch-CoreML-Spectrogram
4+
//
5+
// Created by Gerald on 5/31/20.
6+
// Copyright © 2020 Gerald. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
@UIApplicationMain
12+
class AppDelegate: UIResponder, UIApplicationDelegate {
13+
14+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
15+
// Override point for customization after application launch.
16+
return true
17+
}
18+
19+
// MARK: UISceneSession Lifecycle
20+
21+
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
22+
// Called when a new scene session is being created.
23+
// Use this method to select a configuration to create the new scene with.
24+
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
25+
}
26+
27+
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
28+
// Called when the user discards a scene session.
29+
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
30+
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
31+
}
32+
33+
34+
}
35+
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "iphone",
5+
"scale" : "2x",
6+
"size" : "20x20"
7+
},
8+
{
9+
"idiom" : "iphone",
10+
"scale" : "3x",
11+
"size" : "20x20"
12+
},
13+
{
14+
"idiom" : "iphone",
15+
"scale" : "2x",
16+
"size" : "29x29"
17+
},
18+
{
19+
"idiom" : "iphone",
20+
"scale" : "3x",
21+
"size" : "29x29"
22+
},
23+
{
24+
"idiom" : "iphone",
25+
"scale" : "2x",
26+
"size" : "40x40"
27+
},
28+
{
29+
"idiom" : "iphone",
30+
"scale" : "3x",
31+
"size" : "40x40"
32+
},
33+
{
34+
"idiom" : "iphone",
35+
"scale" : "2x",
36+
"size" : "60x60"
37+
},
38+
{
39+
"idiom" : "iphone",
40+
"scale" : "3x",
41+
"size" : "60x60"
42+
},
43+
{
44+
"idiom" : "ipad",
45+
"scale" : "1x",
46+
"size" : "20x20"
47+
},
48+
{
49+
"idiom" : "ipad",
50+
"scale" : "2x",
51+
"size" : "20x20"
52+
},
53+
{
54+
"idiom" : "ipad",
55+
"scale" : "1x",
56+
"size" : "29x29"
57+
},
58+
{
59+
"idiom" : "ipad",
60+
"scale" : "2x",
61+
"size" : "29x29"
62+
},
63+
{
64+
"idiom" : "ipad",
65+
"scale" : "1x",
66+
"size" : "40x40"
67+
},
68+
{
69+
"idiom" : "ipad",
70+
"scale" : "2x",
71+
"size" : "40x40"
72+
},
73+
{
74+
"idiom" : "ipad",
75+
"scale" : "1x",
76+
"size" : "76x76"
77+
},
78+
{
79+
"idiom" : "ipad",
80+
"scale" : "2x",
81+
"size" : "76x76"
82+
},
83+
{
84+
"idiom" : "ipad",
85+
"scale" : "2x",
86+
"size" : "83.5x83.5"
87+
},
88+
{
89+
"idiom" : "ios-marketing",
90+
"scale" : "1x",
91+
"size" : "1024x1024"
92+
}
93+
],
94+
"info" : {
95+
"author" : "xcode",
96+
"version" : 1
97+
}
98+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"info" : {
3+
"author" : "xcode",
4+
"version" : 1
5+
}
6+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13122.16" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
3+
<dependencies>
4+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13104.12"/>
5+
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
6+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
7+
</dependencies>
8+
<scenes>
9+
<!--View Controller-->
10+
<scene sceneID="EHf-IW-A2E">
11+
<objects>
12+
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
13+
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
14+
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
15+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
16+
<color key="backgroundColor" xcode11CocoaTouchSystemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
17+
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
18+
</view>
19+
</viewController>
20+
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
21+
</objects>
22+
<point key="canvasLocation" x="53" y="375"/>
23+
</scene>
24+
</scenes>
25+
</document>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16096" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
3+
<device id="retina6_1" orientation="portrait" appearance="light"/>
4+
<dependencies>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="16087"/>
6+
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
7+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
8+
</dependencies>
9+
<scenes>
10+
<!--View Controller-->
11+
<scene sceneID="tne-QT-ifu">
12+
<objects>
13+
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="Pytorch_CoreML_Spectrogram" customModuleProvider="target" sceneMemberID="viewController">
14+
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
15+
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
16+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
17+
<subviews>
18+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="CoreML Melspectrogram Demo" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="wav-tK-own">
19+
<rect key="frame" x="31" y="50" width="350" height="29"/>
20+
<autoresizingMask key="autoresizingMask" flexibleMaxY="YES"/>
21+
<fontDescription key="fontDescription" type="boldSystem" pointSize="24"/>
22+
<color key="textColor" systemColor="systemBlueColor" red="0.0" green="0.47843137250000001" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
23+
<nil key="highlightedColor"/>
24+
</label>
25+
<view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="mfE-wk-zKI" customClass="DrawSpecView" customModule="Pytorch_CoreML_Spectrogram" customModuleProvider="target">
26+
<rect key="frame" x="0.0" y="144" width="415" height="221"/>
27+
<autoresizingMask key="autoresizingMask" flexibleMaxY="YES"/>
28+
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
29+
</view>
30+
</subviews>
31+
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
32+
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
33+
</view>
34+
<connections>
35+
<outlet property="drawSpecView" destination="mfE-wk-zKI" id="65K-5f-sB3"/>
36+
</connections>
37+
</viewController>
38+
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
39+
</objects>
40+
<point key="canvasLocation" x="137.68115942028987" y="101.78571428571428"/>
41+
</scene>
42+
</scenes>
43+
</document>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// ConvertSpectrogram.swift
3+
// CoreML_Audio_Analysis
4+
//
5+
// Created by Gerald on 5/30/20.
6+
// Copyright © 2020 Gerald. All rights reserved.
7+
//
8+
// from https://github.com/tucan9389/DepthPrediction-CoreML/blob/master/DepthPrediction-CoreML/heatmapProcessor.swift
9+
import CoreML
10+
11+
class SpectrogramConverter {
12+
func convertTo2DArray(from spectrogram: MLMultiArray) -> Array<Array<Float32>> {
13+
guard spectrogram.shape.count == 4 else {
14+
print("spectrogram's shape is invalid. \(spectrogram.shape)")
15+
return []
16+
}
17+
let spectrogram_w = spectrogram.shape[2].intValue
18+
let spectrogram_h = spectrogram.shape[3].intValue
19+
20+
var converted_spectrogram: Array<Array<Float32>> = Array(repeating: Array(repeating: 0.0, count: spectrogram_h), count: spectrogram_w)
21+
22+
var minimumValue: Float32 = Float32.greatestFiniteMagnitude
23+
var maximumValue: Float32 = -Float32.greatestFiniteMagnitude
24+
25+
for i in 0..<spectrogram_w {
26+
for j in 0..<spectrogram_h {
27+
let index = [ 0, 0, i as NSNumber, j as NSNumber ] // i*(spectrogram_h) + j
28+
let val = spectrogram[index].floatValue
29+
// guard val > 0 else { continue }
30+
converted_spectrogram[i][spectrogram_h-j-1] = val // origin at bottom
31+
32+
if minimumValue > val {
33+
minimumValue = val
34+
}
35+
if maximumValue < val {
36+
maximumValue = val
37+
}
38+
}
39+
}
40+
41+
maximumValue = max( -15.0, maximumValue ) // for improved contrast on device
42+
var minmaxGap = maximumValue - minimumValue
43+
44+
// print( "minmax \(minmaxGap) \(maximumValue) \(minimumValue)")
45+
46+
if ( minmaxGap == 0 ) {
47+
minmaxGap = 1.0
48+
}
49+
for i in 0..<spectrogram_h {
50+
for j in 0..<spectrogram_w {
51+
converted_spectrogram[j][i] = (converted_spectrogram[j][i] - minimumValue) / minmaxGap
52+
}
53+
}
54+
55+
return converted_spectrogram
56+
}
57+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//
2+
// DrawSpecView.swift
3+
// CoreML_Audio_Analysis
4+
//
5+
// Created by Gerald on 5/30/20.
6+
// Copyright © 2020 Gerald. All rights reserved.
7+
//
8+
// adapted from https://github.com/tucan9389/DepthPrediction-CoreML/blob/master/DepthPrediction-CoreML/DrawingHeatmapView.swift
9+
10+
import Foundation
11+
12+
import UIKit
13+
14+
class DrawSpecView: UIView {
15+
16+
var spectrogram: Array<Array<Float32>>? = nil {
17+
didSet {
18+
DispatchQueue.main.async {
19+
self.setNeedsDisplay()
20+
}
21+
}
22+
}
23+
24+
override func draw(_ rect: CGRect) {
25+
26+
if let ctx = UIGraphicsGetCurrentContext() {
27+
28+
ctx.clear(rect);
29+
30+
guard let spectrogram = self.spectrogram else { return }
31+
32+
let size = self.bounds.size
33+
let spectrogram_w = spectrogram.count
34+
let spectrogram_h = spectrogram.first?.count ?? 0
35+
let w = size.width / CGFloat(spectrogram_w)
36+
let h = size.height / CGFloat(spectrogram_h)
37+
38+
for j in 0..<spectrogram_h {
39+
for i in 0..<spectrogram_w {
40+
let value = spectrogram[i][j]
41+
var alpha: CGFloat = CGFloat(value)
42+
if alpha > 1 {
43+
alpha = 1
44+
} else if alpha < 0 {
45+
alpha = 0
46+
}
47+
48+
let rect: CGRect = CGRect(x: CGFloat(i) * w, y: CGFloat(j) * h, width: w, height: h)
49+
50+
// color
51+
let hue: CGFloat = (1.0-alpha) * (240.0 / 360.0)
52+
let color: UIColor = UIColor(hue: hue, saturation: 1, brightness: 1, alpha: 0.94)
53+
54+
// gray
55+
// let color: UIColor = UIColor(white: 1-alpha, alpha: 1)
56+
57+
let bpath: UIBezierPath = UIBezierPath(rect: rect)
58+
59+
color.set()
60+
bpath.fill()
61+
}
62+
}
63+
}
64+
} // end of draw(rect:)
65+
66+
}

0 commit comments

Comments
 (0)