Author: Miguel Saldaña

New location

Finally, I moved to Addison, TX and so far everything is going great with my job. The food in the cafeteria is delicious and cheap, I don’t have to pay $125 for parking, the gym is not a franchise(crowded) and traffic is not bad at all! My work seems to have a lot of flexibility, so that will give me a great opportunity to get in great shape and save a lot of money. I feel more happy and energetic, I felt I was dying inside at my job in Fort Worth. I feel I can learn more rewarding things at this new job which is RF stuff, I always wanted to learn all these antennas stuff.

At the same time, I almost lost my momentum programming audio, since I didn’t program in 4 days while I was moving to Addison. It’ amazing how quick can you lose something that it took you a lot of time to build such as good habits, but it feels good to be back on the grind. Currently I’m trying to build an audio editor app and I learned that simple things take so much time to build or maybe I’m a slow programmer.

I shouldn’t be so hard on myself because I still need to get used to this new environment, but at the same time I can’t rest on my laurels. What matters is always to strive for a better life and say no to a lot distractions. The only thing I really struggle to stop doing is going out with friends and socialize on Saturday night since it gets very lonely at programming, just you and your computer painting pixels on the screen. It can feel that everything you’re doing is a waste of time and some dude in China who has no life already did it better and for free, but I have to trust the process.

Things…. take time! MASSIVE Action and MASSIVE Patience.

On another note, something that I’m trying to improve consistently is my people skills. Sometimes I can be very introverted person, but when I’m happy I’m very extroverted. It’s a weird dynamic that I must become an expert in, people skills are everything. Specially that English is my second language, I want to be able to articulate better sentences, to say things quickly and to kill my inner shyness.

How to create a volume meter in Swift 3

We’re going to create this easy volume meter with AVFoundation and a progress view

You can also add this to incoming audio(microphone), add audio effects, etc… but for now a player will do the job.

-First we start by adding a Progress view to the storyboard

Screen Shot 2017-03-18 at 3.05.36 PM.png

 

-Create an outlet in the Viewcontroller of the progress view and name it volumeMeter.

-Import AVFoundation

import AVFoundation

 

-include an audio File to your project navigator, you can use mine if you want

Click save link as…

-add these properties on the class  ViewController

    var engine: AVAudioEngine!      //The Audio Engine
    var player: AVAudioPlayerNode!  //The Player of the audiofile
    var file = AVAudioFile()        //Where we're going to store the audio file
    var timer: Timer?               //Timer to update the meter every 0.1 ms
    var volumeFloat:Float = 0.0     //Where We're going to store the volume float

 

-On the ViewDidLoad() add this code that will Initialize the engine and load the .m4a audio into an audiofile.

Note: replace the parameter strings for your audio file name “forResource” and “ofTYpe”.

        //init engine and player
        engine = AVAudioEngine()
        player = AVAudioPlayerNode()

        //Look for the audiofile on the project
        let path = Bundle.main.path(forResource: "Electronic", ofType: "m4a")!
        let url = NSURL.fileURL(withPath: path)

        //create the AVAudioFile
        let file = try? AVAudioFile(forReading: url)
        let buffer = AVAudioPCMBuffer(pcmFormat: file!.processingFormat, frameCapacity: AVAudioFrameCount(file!.length))
        do {
            //Do it
            try file!.read(into: buffer)
        } catch _ {
        }

 

-Next we’re going to connect the player and the engine together

        engine.attach(player)
        engine.connect(player, to: engine.mainMixerNode, format: buffer.format)

 

-We’re going to installTap on the mainMixerNode to extract that juicy float


        //installTap with a bufferSize of 1024 with the processingFormat of the current audioFile on bus 0
        engine.mainMixerNode.installTap(onBus:0, bufferSize: 1024, format: file?.processingFormat) {
            (buffer : AVAudioPCMBuffer!, time : AVAudioTime!) in

            let dataptrptr = buffer.floatChannelData!           //Get buffer of floats
            let dataptr = dataptrptr.pointee
            let datum = dataptr[Int(buffer.frameLength) - 1]    //Get a single float to read

            //store the float on the variable
            self.volumeFloat = fabs((datum))

        }

 

-Next we’re going to start the engine and play file.


        //Loop the audio file for demo purposes
        player.scheduleBuffer(buffer, at: nil, options: AVAudioPlayerNodeBufferOptions.loops, completionHandler: nil)

        engine.prepare()
        do {
            try engine.start()
        } catch _ {
        }

        player.play()

 

-Create a timer to update the volumeMeter every 0.1 miliseconds

        //start timer to update the meter
        timer = Timer.scheduledTimer(timeInterval: 0.1 , target: self, selector: #selector(updateMeter), userInfo: nil, repeats: true)

 

-Finally we’re going to create a new function named updateMeter() that’s going to be called by the timer every 0.1 ms to update the progressView with the new float value.


     func updateMeter() {
        self.volumeMeter.setProgress(volumeFloat, animated: false)

        if volumeMeter.progress > 0.8{//turn red if the volume is LOUD
            volumeMeter.tintColor = UIColor.red

        }else{//else green
            volumeMeter.tintColor = UIColor.green
        }

    }

 

Conclusion:
This is not a perfect volume meter, but it’s a start for more advanced meters.

Github or die

volume meter 2

How to create a SoundCloud like waveform in Swift 3

 

The first thing you need to get is the array of floats from the audio file and we’re going to use AVFoundation for that.

import AVFoundation

create a struct outside the class to store the audio information.

struct ReadFile {
    static var arrayFloatValues:[Float] = []
    static var points:[CGFloat] = []

}

-Add the audio file you want to analyze to the project navigator
-Add this code in ViewDidLoad()
Note: Make sure to change the forResource and withExtension parameters.

override func viewDidLoad() {
        super.viewDidLoad()

        //Look for sample2.m4a audio file
        let url = Bundle.main.url(forResource: "sample2", withExtension: "m4a")
        let file = try! AVAudioFile(forReading: url!)//Read File into AVAudioFile
        let format = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: file.fileFormat.sampleRate, channels: file.fileFormat.channelCount, interleaved: false)//Format of the file

        let buf = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: UInt32(file.length))//Buffer
        try! file.read(into: buf)//Read Floats
        //Store the array of floats in the struct
         readFile.arrayFloatValues = Array(UnsafeBufferPointer(start: buf.floatChannelData?[0], count:Int(buf.frameLength)))

    }

-Create new swift file and name it DrawWaveform
-subclass UIView and import UIKit and Accelerate frameworks

import UIKit
import Accelerate

class DrawWaveform: UIView {

    //This is where we're going to draw the waveform
    override func draw(_ rect: CGRect) {

     }

}

But before drawing we need to downsample the array of floats, because we don’t need to draw ALL of the floats.
To do this we’re going to use Accelerate framework

-create a new function in the new swift file ‘convertToPoints()’

-Copy this code inside the function convertToPoints(). I will explain this code in detail in another post, but it just reduces the size of the array and converts it to CGFloat.


func convertToPoints() {
        var processingBuffer = [Float](repeating: 0.0,
                                       count: Int(readFile.arrayFloatValues.count))
        let sampleCount = vDSP_Length(readFile.arrayFloatValues.count)
        //print(sampleCount)
        vDSP_vabs(readFile.arrayFloatValues, 1, &processingBuffer, 1, sampleCount);
        // print(processingBuffer)

        //THIS IS OPTIONAL
        // convert do dB
        //    var zero:Float = 1;
        //    vDSP_vdbcon(floatArrPtr, 1, &zero, floatArrPtr, 1, sampleCount, 1);
        //    //print(floatArr)
        //
        //    // clip to [noiseFloor, 0]
        //    var noiseFloor:Float = -50.0
        //    var ceil:Float = 0.0
        //    vDSP_vclip(floatArrPtr, 1, &noiseFloor, &ceil,
        //                   floatArrPtr, 1, sampleCount);
        //print(floatArr)

        var multiplier = 1.0
        print(multiplier)
        if multiplier < 1{
            multiplier = 1.0

        }

        let samplesPerPixel = Int(150 * multiplier)
        let filter = [Float](repeating: 1.0 / Float(samplesPerPixel),
                             count: Int(samplesPerPixel))
        let downSampledLength = Int(readFile.arrayFloatValues.count / samplesPerPixel)
        var downSampledData = [Float](repeating:0.0,
                                      count:downSampledLength)
        vDSP_desamp(processingBuffer,
                    vDSP_Stride(samplesPerPixel),
                    filter, &downSampledData,
                    vDSP_Length(downSampledLength),
                    vDSP_Length(samplesPerPixel))

        // print(" DOWNSAMPLEDDATA: \(downSampledData.count)")

        //convert [Float] to [CGFloat] array
        readFile.points = downSampledData.map{CGFloat($0)}

    }

    

Now we’re going to draw the waveform with the downsampled array.
copy this code inside the draw function in the subview.

This uses UIBezierPath and loops thru the array of floats to draw and at the apply stroke to fill it with color!

Where x is the distance between squares and y is the amplitude of the square.

    override func draw(_ rect: CGRect) {

 //downsample and convert to [CGFloat]
        self.convertToPoints()

        var f = 0
        //the waveform on top
        let aPath = UIBezierPath()
        //the waveform on the bottom
        let aPath2 = UIBezierPath()

        //lineWidth
        aPath.lineWidth = 2.0
        aPath2.lineWidth = 2.0

        //start drawing at:
        aPath.move(to: CGPoint(x:0.0 , y:rect.height/2 ))
        aPath2.move(to: CGPoint(x:0.0 , y:rect.height ))

        //Loop the array
        for _ in readFile.points{
                //Distance between points
                var x:CGFloat = 2.5
                //next location to draw
                aPath.move(to: CGPoint(x:aPath.currentPoint.x + x , y:aPath.currentPoint.y ))

                //y is the amplitude of each square
                aPath.addLine(to: CGPoint(x:aPath.currentPoint.x  , y:aPath.currentPoint.y - (readFile.points[f] * 70) - 1.0))

                aPath.close()

                x += 1
                f += 1
        }

        //If you want to stroke it with a Orange color
        UIColor.orange.set()
        aPath.stroke()
        //If you want to fill it as well
        aPath.fill()

        f = 0
        aPath2.move(to: CGPoint(x:0.0 , y:rect.height/2 ))

        //Reflection of waveform
        for _ in readFile.points{
            var x:CGFloat = 2.5
            aPath2.move(to: CGPoint(x:aPath2.currentPoint.x + x , y:aPath2.currentPoint.y ))

            //y is the amplitude of each square
            aPath2.addLine(to: CGPoint(x:aPath2.currentPoint.x  , y:aPath2.currentPoint.y - ((-1.0 * readFile.points[f]) * 50)))

            // aPath.close()
            aPath2.close()

            //print(aPath.currentPoint.x)
            x += 1
            f += 1
        }

        //If you want to stroke it with a Orange color
        UIColor.orange.set()
        //Reflection and make it transparent
        aPath2.stroke(with: CGBlendMode.normal, alpha: 0.5)

        //If you want to fill it as well
        aPath2.fill()

}
 

Next step is go to the storyboard and create a UIVIEW and change the class to ‘DrawWaveform’

Build and Run.

Screen Shot 2017-03-13 at 8.38.38 PM

Here’s the Github Link

APPSTORE release and first sale!

Finally, my app ‘Loop maker’ it’s in the app store new and alive! It took me long time to build it since I got distracted with objective c and another framework that later was deprecated. It was a long process but I learned a lot from ux to low level core audio and dealing with the tedious appstore. I’m getting downloads but not in app purchases, maybe I should get better at marketing or make a bigger product. Anyway I feel great, proud, and energetic! I also I made my first sale, can I call myself an entrepreneur?

link for app:https://goo.gl/0kjbAzScreen Shot 2017-03-07 at 4.07.17 PM.png

I presented my app to app developers meetup group and met many developers, it was nice to network. I noticed that I suck at presenting my own projects so I should work on my public speaking skills and my English.

Change of plans now! Haha wow life doesn’t go as planned isn’t… I had to give my parents money and I got offered an interesting job for my next rotation “Radio access network engineer”. The reason I say it’s a good option it’s because I’m moving to an apartment that its 680/month instead of 1200/month which aligns to my goals of being an entrepreneur. I’m not going to have to pay $125 for parking and $30 for the gym which brings my cost of living way low. I also finished paying off my car loan(12k) and next it’s my student loan(4k). 

My studio apartment will look like an office and I can already see friends saying “this is where you live?”, but I don’t care. I don’t care about traveling. I don’t care about having a beer after work. I don’t care about watching Netflix. I don’t care about politics. I don’t care about Religion. I don’t care about kids. I don’t care about the news. I don’t care about mortgages. I don’t care about the new Nintendo. I care about changing my life and I’m willing to work for it.

I’m already thinking on my next app idea…

Birthday month

November 12 was 26th birthday  and remember that drum machine I was trying to build in the beginning of 2016? I’m almost done with all the processing. I committed myself to learn some parts of core audio and the amazing audio engine 2(deprecated). I hired a graphic designer in upwork to do the user interface for me, focus on your strengths right? I still need to create the waveform logic, sequencer UI, menu integration and load audio from sample kits.

Creating apps is a very laborious work and it takes as 10x time to build if you don’t know what it takes. Sometimes it feels that I am going too slow and I haven’t release an app in a year, but I have to be patient as Gary Vee says.

Machine Learning

Another very interesting area  that I had been looking into for some time is Machine learning with audio or music also called Music Information Retrieval. After I completed and marketed my app I will study more of this subject and try to build a useful app with it. Everyone is talking about how robots are learning, going to take over the world and I see some great innovations with machine learning.

Digital signal processing helps in identifying features of a song such as pitch, tempo, etc… I feel that in Audio digital signal processing all the problems are already solved, but with machine learning I see how can we improve audio or music apps to be more intelligent and expressive. My dream is to have a profitable audio app and become an expert on my subject. I will do whatever it takes to keep pursuing it!

Life

So as I’m writing this in the bus I had an interview yesterday to work from home in my next rotation with the current company I am. I honestly don’t care what kind of work I do as long I can optimize my life and save time working from home. My plan is to workout at lunch and live a more healthy life as I think going to a cubicle drains my energy.

April 1st is my next rotation to be uncomfortable again and meet new people that will push me to be better. An area that I’m lacking is going out on weekends, I just cant give up my social life, it’s a struggle to just be at home not meeting new people. Maybe going out isn’t the problem and drinking is since that takes a lot of my energy the next day. I need to solve that asap…

I’m obsessed with having a better life.

 

 

 

 

 

 

 

 

 

 

Climbing Core Audio

mordor.png

Core Audio

I encountered a huge roadblock to finish my toy app and that its ‘offline rendering’, I can’t do that with AVFoundation. So I had to revisit the book “Learning core audio” by Chris Adamson which I tried to read a while ago to only don’t understand anything. Now I was able to understand the book and create cool stuff such as reading Floats from an audio file and plot a waveform  happy. I think I’m barely starting to get momentum and that helps with learning concepts faster.

waveform

It’s very easily to get discouraged to know all the time/effort it takes to build something simple and see all the people that have done it already. The secret is to only focus on yourself and DO IT ANYWAY, you learn as you go. Another thing is that swift 3 is a new language and it adds excitement because something hasn’t been done in swift.

I code every day and try to go to bed knowing a little bit more about swift or audio. In my mind I keep telling that is like going to the gym/dieting, the more work you put in the more you get out. Even though I’m not a fitness enthusiast I’m an audio maniac!!!!!! I would love to just take a year off and do work every hour but I have loans to pay  .

Fulfillment

There’s a of strong feeling of fulfillment when I’m doing my own thing, a feeling that I felt every day when I was making music/DJing when I was a teenager. Today that feeling is there when I put in work and I make a code work, or learned something that I can apply. Maybe that’s the reason people say “it’s about the journey, not the destination”.  You have to like what you’re doing and willing to do it every god damn day (notice I didn’t say passion).

I have a lot of fuel to change my life for the better, even though going to college and getting a good job already has provided me with %100 better conditions of living and opportunities… I want MORE. I want to be able to an expert in my field and be able to help people.

You have to do it and do it and do it and do it and do it and do it and do it and do it until the job gets done.

First year

Life by design

Today it’s been a year since I started blogging about learning audio programming and I did learned a lot about audio. This year I learned a lot about distractions and the resistance to grow, I can say it was a very productive year with some mistakes of course. This year I absorbed all information I could about audio and it wasn’t easy because a lot of the stuff was way over my head, but some was easy to understand thanks to my previous experience as music producer and college education. Even though I feel that I barely scratched the surface about audio I should celebrate the small wins and never quit. I started by taking online classes on audio, DSP and programming in IOS.

Currently I’m making a ‘toy app’ which simply morphs your voice, but the first idea was to make that as an app extension for the imessage app until I discovered that I couldn’t use the microphone for extensions, so I had to change. Even though it’s a very simple app and there’s tons in the appstore like it, I just want to finish it and have something out there. The app it’s almost finished and I just need to do the tedious work of the app (variations of voice effects) and final details. I used swift 3.0 to program the app and it wasn’t very hard to make since it’s a high level language, but I learned a lot and hopefully someone will download it and like it, if I can make a couple bucks out of it I will consider that a success. I’m going to use search ads in the appstore to learn more about marketing (you pay to learn), and get instant feedback. “You can read all you want about swimming, but until you jump into the pool you will learn how to swim” that’s my motto for anything, we get stuck reading about everything and never making our own conclusions. TRY IT FOR YOURSELF AND MAKE YOUR OWN CONCLUSIONS!

Focus to finish

I need to focus more on one thing at a time, finish it and stick to it till completion or death. That’s something very important I learned as a music producer, I would start dozens of cool music loops but lost interest when expanding that loop into a full song. I learned that the completed songs stuck to me forever even if no one heard some of the songs, I felt proud and felt good. I don’t remember all the hundreds loops I did and I became so good at making badass loops but not badass songs, so the same thing we can say about building apps, or any other endeavor. Just finish it and get it out there!!!!

One of the biggest enemies of success is our own resistance to simply sit down and do work as perfectly stated in the book “war of art” a great book that I gave as a gift to my brother. One of my struggles are doing work in the weekends, I just can’t dedicate my weekend nights to work, when I want to be out and be social. During the weekdays I have no problem coming from work, microwave my already prepared chicken and focus on work on my side project until 10pm(sleep time), I guess it’s a great habit I built. On weekends it’s another story and its when I wash clothes, clean, meal prep, work out (frid,sat,sun) and I do some distracted work (by work I don’t mean my full time job) .

When I go out with friends I try to do it the “smart” way, by going out late 11pm-12pm and only if I know it’s going to be a fun night. I don’t go to the bar to just have a beer or hang out to have a beer or come home after work to have a beer and watch the game. Time is the most important thing in my life and I want to spend it wisely, that’s why I stopped going out on Thursday, Sunday and sometimes Friday when the newness of moving to a new city wears off.

My biggest focus is my dreams.

Things to be thankful about

Sometimes we compare ourselves to the top 1% of successful people and we don’t feel that we accomplished anything in life. I think it’s a very painful way to live and always comparing yourself to others and we forget our own achievements even if they’re small. I’m thankful I have health, I’m thankful I’m months away from paying my student loans and car loan, I’m thankful I have a cushy mid ~70k job (40hr), I’m thankful I have my parents, I’m thankful I’m moving to Dallas (I liked ft worth) in 7 months, I’m thankful I’m dating someone cool, I’m thankful I live alone and I’m thankful that I’m pursuing my dreams (at least half time). I’m thankful but not complacent, I want more of the good stuff and more requires hard work.

 

 

 

 

 

DSP class

Screen Shot 2016-07-11 at 7.22.49 PM.png

 

It’s been a while but I’m still in the struggle to learn audio DSP and last week I started my online DSP certification from UCSD and its three classes: DSP 1, Applied DSP and Wireless DSP. I’m currently taking DSP 1 and there’ s a lot of stuff that I already know from my signals classes from college, but this time is different because I have a purpose. When I was in college I was too distracted by girls, stress from other classes and my internships, but this time I have more time to dedicate most of my time to fully learn the concepts. Hopefully in the future I am able to write my own audio effects algorithms or at least to understand what’s under the hood. Sometimes I feel I’m not going fast enough, but I have to understand that things that are worth it takes YEARS. This past month I failed a lot to wake up at 5am, I was feeling too tired, but I am still on the game. “You only fail if you quit.”

In another note, I been learning more about core audio and I found a way to create a sequencer with musicSequence and AuGraph. Also I been playing with the new messages extension(ios 10) and I want to create a simple app that morphs your voice and sends it quick through the imessage app. It seems that the msmessage framework doesn’t work well right now, so I will wait until it’s released to the public and create that app.

Another goal that I want to achieve is to stop going out on Fridays and only go out on Saturdays, the problem about moving to a big city is distractions. Friends always invite me to places and since I live close to the bars and stuff I give up easily. My next apartment will be in a more calm area but close to downtown, because I’m moving in March 2017. Other thing that I’m happy with is about my debt is going down quick and I predict I will be debt free by April 2017. When I was in college I bought a bunch of stuff I didn’t need and now I’m paying for it plus interest. After being debt free I think I will have more confidence to start a risky idea and don’t be afraid of losing my job.

When you’re pursuing your dream you’re living the dream, enjoy the process.

 

Great introduction on how apple handles audio with core audio

https://www.mikeash.com/pyblog/friday-qa-2012-10-12-obtaining-and-interpreting-audio-data.html

Moving to more advance stuff

2-11

April was a creative month and I half finished the app for the ipad.I added some delay knobs to the beat maker and I found a deal breaker. I was using audiokit framework for swift and I found sound latency. Now I’m going to try the amazing audio engine 2 in objective c and try to do a better app with low latency. Maybe it was the way I coded the app i don’t know and I want to know about what’s under the hood.

Currently I’m learning the amazing audio engine 2 in objective c, so its a big learning curve to learn objective c and the way the engine works. I have a feeling that I have a better understanding on how the audio works now. I’m trying to understand more about the buffer and how it stores and send the audio, maybe im making more complex things that are simple stuff I don’t know. I read the first chapters of “The Audio programming book” and “learning core audio”, but the books need way more time because everything is in C, but soon I will get there. I took a class in college “software design 1” in 2011 which I got a B and it was taught by a very challenging professor. In conclusion I will try and create the beatmaker with the amazing audio engine 2 with better functions.

I’m thinking of doing a certification in DSP from univ of San Diego which consists of three classes: DSP 1, Applied DSP and wireless DSP. It sounds fun and challenging but it will require a lot of focus, I think these classes will help me have better understanding in Digital signal processing. I’m not excited about the math and paying $750 per class, but I really want to learn this stuff. That certification may bring better employment opportunities, I don’t really want a job but to build audio products.Maybe in the future that certification will give make me seem more valuable if I ever do any consulting type of work, being the type of person that charges $500 an hour thats the dream! I’m blessed that I have a low stress/boring job which pays great, so I have the time to learn the things I want to learn.

In terms of fitness I’m still consistent and feel healthy, I go to the gym three times a week and eat relatively good.

I’m not going to give up on my dreams! 

I will build GREAT audio products and sell a lot of them.

In another note, I had great experiences when I go out partying and I try to do it the smart way(not wasting time ). I kind of miss playing video games and chilling, but I can’t I must sacrifice If I want to pursue my dream and have some social life. I keep myself busy all the time learning and building, I really want to be an entrepreneur.

On being alone

I don’t feel alone because I surround myself with friends and like minded people, I love living alone. People are scared of living alone and they never give themselves the opportunity to know themselves and I really recommend that everyone live alone for a couple years. People go from mommy to move in with in with a girlfriend, but I understand its scary and you feel you will never be with someone, but thats not true it just takes time to get used to it. One day I will look back and I will cherish the moments when I lived alone, I don’t know or maybe I will be alone forever. The only think that should be in my mind is to push forward everyday!

Sketch 3, Paintcode and audio UI/UX mysteries

When I was around 13-18 I taught myself how to use fireworks and Photoshop to create my own party flyers, forum signatures and photo editing. I never thought that those beginner skills would helped learn Sketch 3 and paintcode fast. I bought for $39 a course on Udemy to learn from A to Z sketch 3, I gave that course 5/5 star review because it was very well explained and fast paced.

image1(1)

figure 1

Something that was a mystery for me for years was HOW on earth do you design and program user interface on music software. There’s no straight answer on the internet(idk why) but people use software like Photoshop and fireworks to create the design, look and software like paint code to program the movement of the button. You create each part of the user interface and export each part so it can become animated and programmed such as sliders, knobs, led buttons, buttons, etc.. YIKES! Another piece of the puzzle.

That course gave me the tools to design a better UI for the beat maker app as you can see in figure 1. I created the pads with some texture to look more real and this is the first draft.