Skip to content

Commit

Permalink
Implemented DataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
Ibrahimhass committed Nov 21, 2017
1 parent 5ad1267 commit 194bfc3
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 94 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,4 @@
<Bucket
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "IHWaveformView/IHWaveFormView.swift"
timestampString = "532931083.779678"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "134"
endingLineNumber = "134"
landmarkName = "getPath(url:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "IHWaveformView/IHWaveFormView.swift"
timestampString = "532931083.780408"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "153"
endingLineNumber = "153"
landmarkName = "getPath(url:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "IHWaveformView/IHWaveFormView.swift"
timestampString = "532931083.780963"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "147"
endingLineNumber = "147"
landmarkName = "getPath(url:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
71 changes: 36 additions & 35 deletions IHWaveformView/IHWaveFormView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,28 @@ protocol IHWaveFormViewDelegate : class {

class IHWaveFormView: UIView, AVAudioPlayerDelegate {
private var player: AVAudioPlayer!
fileprivate let centerLineView = UIView()
private var dataArray : [Float] = []
private var totalCount : Int = 0
private var xPoint : CGFloat = 0.0
private var gameTimer: Timer!
private var internalLineWidth : CGFloat!
private var internalLineSeperation : CGFloat!
weak var delegate : IHWaveFormViewDelegate?
weak var dataSource : IHWaveFormViewDataSource?
private var internalLineWidth : CGFloat = 2.0
private var internalLineSeperation : CGFloat = 1.0
fileprivate var width : CGFloat?

private var urlToPlay : URL {
return (dataSource?.urlToPlay())!
}

weak var delegate : IHWaveFormViewDelegate?
weak var dataSource : IHWaveFormViewDataSource? {
didSet {
if (dataSource?.urlToPlay() != nil) {
self.getPath(url: (dataSource?.urlToPlay())!)
}
}
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
xPoint = 0.0
Expand All @@ -55,12 +68,9 @@ class IHWaveFormView: UIView, AVAudioPlayerDelegate {
xPoint = 0.0
}

fileprivate var width : CGFloat?

override func draw(_ rect: CGRect) {
super.draw(rect)
self.commonInit()
self.getPath(url: urlToPlay!)
commonInit()
}

var orientationChangesCount : Int = 0
Expand All @@ -72,61 +82,53 @@ class IHWaveFormView: UIView, AVAudioPlayerDelegate {
redrawView()
}

func eraseView() {
private func eraseView() {
for views in subviews {
views.removeFromSuperview()
}
self.layer.sublayers?.forEach { $0.removeFromSuperlayer() }
}

internal func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
self.gameTimer.invalidate()
delegate?.didFinishPlayBack()
}

var centerLineView : UIView?

func addCentreLine(){
centerLineView = UIView.init(frame: CGRect.init(x: 0, y: self.frame.size.height / 2.0, width: self.frame.size.width, height: 1))
centerLineView?.backgroundColor = .black
self.addSubview(centerLineView!)
private func addCentreLine(){
centerLineView.frame = CGRect.init(x: 0, y: self.frame.size.height / 2.0, width: self.frame.size.width, height: 1)
centerLineView.backgroundColor = .black
self.addSubview(centerLineView)
}

func invertColor(_ color : UIColor) -> UIColor {
private func invertColor(_ color : UIColor) -> UIColor {
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
self.backgroundColor?.getRed(&r, green: &g, blue: &b, alpha: &a)
let lineColor = UIColor.init(red: self.invertB(r), green: self.invertB(g), blue: self.invert(b), alpha: a)
return lineColor
}

func invertB(_ val : CGFloat) -> CGFloat{
private func invertB(_ val : CGFloat) -> CGFloat{
return (1.0 - val) * 0.9
}

func invert(_ val : CGFloat) -> CGFloat{
private func invert(_ val : CGFloat) -> CGFloat{
if (val > 0.6 && val < 0.90){
return 0.6 + 0.1
}
return (1.0 - val)
}

private var urlToPlay : URL?

func setUpView(urlToPlay : URL, lineWith : CGFloat?, lineSeperation : CGFloat?) {
if (lineWith != nil){
internalLineWidth = lineWith
}
if (lineSeperation != nil){
internalLineSeperation = lineSeperation
}
self.urlToPlay = urlToPlay
}

func commonInit(){
internalLineWidth = 2.0
internalLineSeperation = 1.0
// self.addOverlayLabels()
self.addCentreLine()
guard let lineWidth = dataSource?.lineWidth?() else {
return
}
guard let lineSeperation = dataSource?.lineSeperation?() else {
return
}
internalLineWidth = lineWidth
internalLineSeperation = lineSeperation
}

private func getPath(url : URL){
Expand Down Expand Up @@ -207,10 +209,9 @@ class IHWaveFormView: UIView, AVAudioPlayerDelegate {
private func redrawView() {
eraseView()
self.commonInit()
self.getPath(url: urlToPlay!)
self.getPath(url: urlToPlay)
}


private func generatePoints1(dBVal : String){
let aPath = UIBezierPath()
let floatVal : Float = Float(dBVal)!
Expand Down
2 changes: 2 additions & 0 deletions IHWaveformView/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
Expand Down
32 changes: 23 additions & 9 deletions IHWaveformView/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,39 @@
//SOFTWARE.

import UIKit
class ViewController: UIViewController, IHWaveFormViewDelegate {
class ViewController: UIViewController {

@IBOutlet var musicView: IHWaveFormView!
override func viewDidLoad() {
super.viewDidLoad()

// self.musicView.setUpView(urlToPlay: url!, lineWith: 2.0, lineSeperation: 1.0)
self.musicView.backgroundColor = #colorLiteral(red: 0.9607843137, green: 0.9607843137, blue: 0.8509803922, alpha: 1)
self.musicView.delegate = self
self.musicView.dataSource = self

}

}

extension ViewController: IHWaveFormViewDelegate {
func didFinishPlayBack() {
print ("playBack Finished")
}

func didStartPlayingWithSuccess() {
"Playback started successfully"
print ("Playback started successfully")
}
}

extension ViewController: IHWaveFormViewDataSource {

@IBOutlet var musicView: IHWaveFormView!
override func viewDidLoad() {
super.viewDidLoad()
func urlToPlay() -> URL {
var url : URL?
let path = Bundle.main.path(forResource: "bensound-sunny.mp3", ofType:nil)!
url = URL(fileURLWithPath: path)
self.musicView.setUpView(urlToPlay: url!, lineWith: 2.0, lineSeperation: 1.0)
self.musicView.backgroundColor = #colorLiteral(red: 0.9607843137, green: 0.9607843137, blue: 0.8509803922, alpha: 1)
self.musicView.delegate = self

return url!
}


}

0 comments on commit 194bfc3

Please sign in to comment.