Monday, November 7, 2016

Web Audio API: Add Bandwidth-Friendly Sound to Your Web Page

The Web Audio API allows developers to leverage powerful audio processing techniques in the browser using JavaScript, without the need for plugins. As well as defining and processing file-based audio sources in real-time, it can synthesize sounds based upon various waveforms; this can be useful for web apps that are often consumed over low-bandwidth networks.

In this tutorial, I'm going to introduce you to the Web Audio API by presenting some of its more useful methods. I'll demonstrate how it can be used to load and play an mp3 file, as well as to add notification sounds to a user interface (demo).

If you like this article and want to go into this topic in more depth, I'm producing a 5-part screencast series for SitePoint Premium named You Ain't Heard Nothing Yet!

What Can I Do With the Web Audio API?

The use cases for the API in production are diverse, but some of the most common include:

  • Real-time audio processing e.g. adding reverb to a user's voice
  • Generating sound effects for games
  • Adding notification sounds to user interfaces

In this article, we'll ultimately write some code to implement the third use case.

Is it Well Supported by Browsers?

Web Audio is supported by Chrome, Edge, Firefox, Opera, and Safari. That said, at the time of writing Safari considers this browser feature experimental and requires a webkit prefix.

Can I Use audio-api? Data on support for the audio-api feature across the major browsers from caniuse.com.

Using the API

The entry point of the Web Audio API is a global constructor called AudioContext. When instantiated, it provides methods for defining various nodes that conform to the AudioNode interface. These can be split into three groups:

  • Source nodes - e.g. MP3 source, synthesised source
  • Effect nodes - e.g. Panning
  • Destination nodes - exposed by an AudioContext instance as destination; this represents a user's default output device, such as speakers or headphones

These nodes can be chained in a variety of combinations using the connect method. Here's the general idea of an audio graph build with the Web Audio API.

Building an audio graph with AudioContext
Source: MDN

Here's an example of converting an MP3 file to an AudioBufferSourceNode and playing it via the AudioContext instance's destination node:

See the Pen Playing an MP3 file with the Web Audio API by SitePoint (@SitePoint) on CodePen.

Generating Audio

As well as supporting recorded audio via AudioBufferSourceNode, the Web Audio API provides another source node called OscillatorNode. It allows frequencies to be generated against a specified waveform. But what does that actually mean?

At a high level, frequency determines the pitch of the sound measured in Hz. The higher the frequency, the higher the pitch will be. As well as custom waves, OscillatorNode provides some predefined waveforms, which can be specified via an instance's type property:

Built-in waveforms supported by OscillatorNode
Source: Omegatron/Wikipedia

  • 'sine' - sounds similar to whistling
  • 'square' - this was often used for synthesizing sounds with old video game consoles
  • 'triangle' - almost a hybrid of a sine and square wave
  • 'sawtooth' - generates a strong, buzzing sound

Here's an example of how OscillatorNode can be used to synthesise sound in real-time:

See the Pen Generating sound with OscillatorNode by SitePoint (@SitePoint) on CodePen.

Continue reading %Web Audio API: Add Bandwidth-Friendly Sound to Your Web Page%


by James Wright via SitePoint

Web Design Weekly #258

Headlines

JavaScript Frameworks: Distribution Channels for Good Ideas

Tom Dale, a core creator of Ember.js shares some thoughts about getting new web platform features into the real world quicker. He puts forward the idea that browser makers should work more closely with framework makers to make this happen. Great article. (medium.com)

Improving Perceived Performance with Multiple Background Images (csswizardry.com)

Side Project Accelerator

An 8 week course where we teach you everything we know. Continue with a lifetime access to the mastermind group and monthly Q&A sessions with the world’s most successful media entrepreneurs.

Articles

Web fonts, boy, I don’t know

Monica Dinculescu reminds us how to lazy load web fonts whilst giving users on slow connections a better experience. (meowni.ca)

Conditions for CSS Variables

Roman Komarov puts together a nice argument that conditions within CSS would be extremely helpful. He also showcases a few interesting workarounds using his powerful CSS knowledge. (kizu.ru)

Introduction to SVG animation

Learn why animating SVG is different from animating in CSS, and work around some common hiccups you might experience along the way. (oreilly.com)

Git from the inside out

This long article explains how Git works. If you are keen to learn more about the inner workings of Git, this is it. (codewords.recurse.com)

Tools / Resources

Abstract App

An amazing team of talented people have been bunkered down working on a new design version controlled workflow built on Git. It is currently in a limited preview release but probably worth keeping an eye on as I’m sure it will be awesome. (abstractapp.com)

Getting Started with Webpack 2

Webpack 2 will be released very soon but that doesn’t mean you can’t start using version 2 now. This post explains how to get set things up and explore the power of verion 2. (blog.madewithenvy.com)

CSS Literal Loader

A webpack loader for extracting and processing CSS defined in other files. (github.com)

Kap – Capture your screen

An open-source screen recorder built with web technology. (getkap.co)

structure.exposed

A good way to check how your site will behave if styles fail to load. (structure.exposed)

The Service Worker Lifecycle (developers.google.com)

Design Prototyping with Origami Studio (origami.design)

Yarn vs. npm: Installation Time (triplet.fi)

Inspiration

The Zendesk rebrand (zendesk.com)

Design Details podcast with Maykel Loomans (spec.fm)

Between the Wires – Guillermo Rauch (betweenthewires.org)

Jobs

Head of Front-End Development at Shopify

As the Head of Front-End Development for Shopify’s product, you’ll shape our core FED team’s medium- and long-term strategic vision and own its implementation. You’ll be responsible for setting direction for our component-driven design and development system, front-end tooling, and future architecture decisions. (shopify.com)

UI/UX Designer at Status

Status is looking for a creative UX designer to come up with new ways to interact with messengers and the blockchain. Status is an open source project that aims to bring Ethereum anywhere. (status.im)

Need to find passionate developers or designers? Why not advertise in the next newsletter

Last but not least…

Font Awesome 5 (kickstarter.com)

The post Web Design Weekly #258 appeared first on Web Design Weekly.


by Jake Bresnehan via Web Design Weekly

Easy Tree – Basic Tree View Plugin with jQuery and Bootstrap

Easy Tree is a jQuery plugin that can convert an un-order list to a tree easily with Bootstrap 3 support. Tree items can be selectable, addable, editable and deletable.


by via jQuery-Plugins.net RSS Feed

Real-Time Messaging with Rails and ActionCable

In the previous article I showed how to create a custom messaging application allowing one-to-one chats between users. We’ve integrated Devise, set up proper associations, added support for Emoji, and made everything work in a synchronous fashion. However, I would really like this app to be more modern. Therefore today we will enhance it further […]

Continue reading %Real-Time Messaging with Rails and ActionCable%


by Ilya Bodrov-Krukowski via SitePoint

Model T Digital

Model T Digital

Long scrolling One Page website for Model T Digital. Nice consistent elements (header image, icons, footer, logo) tying in with the classic Ford Model T to strengthen their branding.

by Rob Hope via One Page Love

Is Figma a Serious Option for Sketch Designers?

Figma

If you follow the Design blogs, you might have seen the social buzz around ‘Figma’, a new UI Design program. Product designers in SF have been raving about it and it’s not difficult to see the appeal. Figma takes the best of Sketch, adds a team-friendly workflow and puts the whole thing into your web browser.

  • But does it work?
  • How does it compare to Sketch?
  • Should Bohemian Coding (the folks behind Sketch) be worried?
  • Should your incorporate Figma into your workflow?

I put Figma through its paces and here’s what I came up with.

How does Figma differ from Sketch?

Figma lives in your Browser (it’s a Chrome App)

Figma is an in-browser software, making it cross-platform. That means that as long as you have Google Chrome, you can use it on Mac, Windows, and even Linux.

Multiplayer mode

Figma has a feature called Multiplayer mode, which allows multiple people to create on the same document at the same time in a live collaboration environment in a manner very similar to collaborating in Google Docs.

Typography Tools

Figma’s typography tools are outstanding. With type features like paragraph spacing, first line indentation, and vertical alignment, they definitely got it right with this one. Sketch is notorious for bugs when using the type tool, but you don’t need to worry about them with Figma.

[caption id="attachment_142939" align="aligncenter" width="2030"]Figma Typography Tools Figma Typography Tools[/caption]

Re-imagined Vector Tools

Figma has an amazing vector tool that allows you to easily create icons with overlapping vector points, which makes icon design a breeze.

[caption id="attachment_142935" align="aligncenter" width="1199"]Figma pen tool in action Figma pen tool in action[/caption]

Version Control

That’s right, Figma has a built-in version control system. Version history allows you to keep a history of all your design revisions. If you’ve used GIT for your code, you’ll be familiar with this concept.

[caption id="attachment_142942" align="aligncenter" width="900"]Version Control in Figma Version Control in Figma[/caption]

Import Your Sketch Files (and SVGs)

Figma makes it simple to instantly import your sketch files and start editing the document right away. Simply drag in your Sketch file into figma, and it will start the import process automatically.

Figma Sketch Import

Collaboration Made Simple

Figma is designed around the idea of easy and effective collaboration. If your design is in Figma, your clients and developers only need a web browser to view your project. A great feature that helps collaboration is the comments tool, which is similar to comments on Zeplin or InVision, developers and clients can comments on specific parts of the document, making project changes a breeze. Figma brings this features in-house and natively allows people you’ve shared the document with easily create comments without having to sign up to anything.

[caption id="attachment_142940" align="aligncenter" width="1890"]Figma commenting system Figma commenting system[/caption]

What does Sketch have that Figma doesn’t?

Sketch has a much bigger market share for both individual designers and design teams, as well as the support of huge tech companies such as Apple, Google, and Facebook. That makes it less likely to disappear in a year or two.

Sketch has a Bigger Pool of Free Tutorials and Resource

There are a lot of Sketch tutorials on Youtube as well as complete courses available here on SitePoint Premium created by your’s truly. Sketch also has a much larger community so you can reach out to other designers for help in Sketch. Figma is quite new for now, but I imagine a lot of designers creating ebooks and video tutorials in the coming months.

[caption id="attachment_142936" align="aligncenter" width="1418"]Sketch tutorials on YouTube Sketch tutorials on YouTube[/caption]

Continue reading %Is Figma a Serious Option for Sketch Designers?%


by Adam Rasheed via SitePoint

Getting Started With the Asset Pipeline, Part 2

By the end of this article you should have a good understanding of the features of the Asset Pipeline in Rails that beginners usually have a tricky time with. You will also have a good introductory grasp of compilation, fingerprinting, caching, minification, and compression. The pipeline gives you a lot of mileage, and a good understanding of why is as important as knowing its features.

Topics

  • Sprockets?
  • Precompiling Assets
  • MD5 Fingerprinting
  • Asset Links
  • Pimped Styles

Sprockets?

The Asset Pipeline in Rails is handled by the sprockets-rails gem. By default, it is enabled when you create a new Rails application. Sprockets is a Ruby library and helps you manage your JavaScript and CSS assets. 

On top of that, it takes care of compiling as well as preprocessing higher-level languages like Sass, SCSS, and CoffeeScript. Preprocessing simply means that your styles, HTML or JavaScript get generated after you write code in another language. For example, your Sass stylesheets get precompiled into valid CSS. Since browsers can’t comprehend stuff like CoffeeScript, we have to preprocess it first.

There are three gems that play a vital role in all of this:

  • coffee-rails
  • uglifier
  • sass-rails

sass-rails lets you write your Sass flavor of choice for creating your CSS, coffee-rails gives you a nicer syntax for writing your JavaScript, and uglifier in turn compresses these assets. By the way, sass-rails is in charge of compressing CSS files itself. All three gems will be added to your Gemfile automatically when you create a new Rails app. If you have a reason not to use Sprockets with Rails, you can skip it from the command line when you initiate your app:

Terminal

This command prevents the above mentioned gems being added to your Gemfile and gives you a different version of the config/application.rb file. Now you need to set up your own solution for handling your assets.

Processing

When you preprocess something like CoffeeScript into valid JS, or Sass into CSS, you also have the option to process these files some more. Minfication and compression are the two big ones. Minification gets rid of stuff the browser does not care about—white space and comments are good candidates, for example. Sass can also deal directly with minification as well. Or you simply use the Rails default of the YUI compressor when dealing with stylesheets. Just as an aside, Rails even lets you write and configure your own custom compressor.

I should mention that Sass has a compressed version of outputting styles. This is actually confusing since it is minifying your stylesheets. Compression is a different process and reduces your file size significantly more. Compression, or gzipping, targets repetitive bits of the code and replaces them with pointers that take up less space. So the more repetitive your asset files, the more they are compressed and reduced in size. 

Minification is quite nice, but you will see the biggest file-size reductions when you use gzipping. It’s not rare to see files shrink to 14% of their original size if you both minify and compress them. When you think about downloading tons of assets over slower networks, this can be of tremendous benefit.

Precompiling Assets

For production, your assets need to be compiled first. Files that you place in app/assets usually need to be preprocessed before they can be served. What are we talking about here? Let’s say you have worked on some new app that works on your local server. Your Sass files and your CoffeeScript flavored JavaScript work magically out of the box. Cool, but what happens if you want to push this to a production server? Such a server, responsible for delivering this content to a possibly much larger audience, has a few different demands than your local server.

As a default, Rails will look for files that are named application and try to precompile them for you. In this step, assets are compiled from high-level languages like Sass into CSS, and they are concatenated together—from multiple files into fewer bundles of files. Having as few files as possible is beneficial for performance and speed. Compressing their size to their bare minimum is also of tremendous importance—especially for bigger applications. On top of all that, files are also cached. That means you only load new assets when they have been changed on your end.

Compilation

You have two options of where you want to compile your assets. You compile on your production server or locally. Local compilation means that you execute this process on your own machine first and then push it to production. This has the advantages that you don’t need write access to the file system on a production server, and if you deploy to multiple servers you can do this process only once. Not needing to precompile your assets on the server if deployed changes do not include asset changes is another benefit of precompiling locally.

Precompiling assets is one of these things we need to do before we send them our “pure”, compiled CSS, HTML, and JS. Servers should probably not need to know about how to deal with high-level languages like Sass, Slim, and whatnot. They have enough responsibilities already. For that to work, you are responsible for having the compression and minifying gems ready on your machine. You can put these compiled assets in your Git repo—or whatever version control tool you prefer—and deploy only these final assets to production.

Rails offers you a Rake task that takes care of precompiling assets, though. Such a task is simply a series of predefined steps that are executed in order to achieve a specific goal. Using the Rake build tool for such things is very common in Ruby land. You can easily write your own tasks in Rake yourself. Rails makes this very easy. Out of the box, Rails comes with the lib/tasks directory where you can conveniently park your Rake tasks. No further setup necessary. So when you run:

Terminal

Sprockets will take the assets it can find in its search path and preprocess or compile them into public/assets. The output will look something like this:

Terminal

When you want to compile locally, it is recommended to change the location where Rails outputs your assets. If you don’t do that, you would need to recompile assets for development because you wouldn’t see local changes without it. The changed URL will be used by Sprockets to serve your assets in development mode.

config/environments/development.rb

For production, the compiled files will still be placed into an /assets directory by default.

You will end up with single assets, aka manifest files like application.js and application.css. That way you don’t need to manually link to all your assets by hand in your markup and avoid having to load multiple asset files instead of one for each category. 

The long number you see that is attached is called the fingerprint. It is used to compare files and see if their contents might have changed before they need to be cached again. What you can also see is that you get two versions of the same file. The one with the .gz file extension is the gzipped version of the assets. gzip is used for file compression and decompression and to cut away a bit of the fat that we don’t want to have sent over the wire. Another improvement to increase speed, basically.

In case you feel the need to precompile your assets on a production server, the following command below will create the assets directly on your server(s). However, I only add this for the sake of completeness. I'm not sure that you will have much need for this as a beginner right now.

Terminal

Manifest Files & Directives

These manifest files like application.css include the logic to import all the files in its search path. Rails imports these partials first and then compiles them into a single authoritative file that the browser will use. That’s just a default, though, and you can change that of course.

Every manifest file has directives, which are instructions that determine which files need to be required to build up these manifest files. The order in which they are imported is determined in there as well. 

The final result contains all the contents of all the files the directives have access to. Sprockets loads these files, does the necessary preprocessing, and rounds the whole thing off by compressing the result. Pretty darn useful!

For your CSS manifest file, app/assets/stylesheets/application.css, this looks like the following:

The JavaScript equivalent, app/assets/javascripts/application.js, looks similar:

As you can see from this example, requiring jQuery first is a must if you rely on it within your JavaScript code.

MD5 Fingerprinting?

By default, Rails creates a fingerprint for each filename during the precompilation process. More specifically, Sprockets creates an MD5 hash from your files’ contents. The resulting 32-character hexadecimal string, aka a digest, is then attached to the filenames of your assets. 

That means that if the contents of your files change, your filenames, the MD5 hash part of it, will change as well. Why is it called fingerprinting? Such hashes have a very high probability of being unique and can therefore be used to identify the uniqueness of a file—just like fingerprints.

Filename Example

We are not talking about a randomized hexadecimal string. The contents of files are pushed through a mathematical function that converts it into a unique 32-character sequence. That means that you get the same hashed result when you apply the function to the same content twice—or however often you like.

Through that clever mechanism, it is possible to check for changes and only update files that would result in a different MD5 hash. For caching purposes, this is golden. If nothing has changed, it is cached by the web browser for future requests. In that context, cache busting simply means that remote clients will request a new copy of a file because the fingerprint has changed. Of course, a new fingerprint will created and added to the filename of your asset.

Output

You can disable fingerprinting both for production and development:

config/environments/production.rb 

config/environments/development.rb

Asset Links

Let’s not forget why it’s nice to have the Asset Pipeline. It aims at making it easy for you to deal with assets. Writing the styles and behaviors for apps has become increasingly more nuanced and complex. Some of the tools also have become more joyful to work with. Preparing assets for production and serving them should be at least a bit more trivial and save you some time.

Having a bit of automation and conventions to organize assets is truly nice because it makes your actual job easy along the way. The Asset Pipeline even sweetens the deal and rounds things off with a few sugary assets links. This makes it a blast to deal with assets in your code. Let’s look at a few of the usual suspects that hopefully increase your happiness level even more: 

  • javascript_include_tag
  • stylesheet_link_tag
  • image_tag

Since its extraction, Sprockets did not change the way you can link your assets; it still works the same as before. The examples above are convenience methods that take the name of your assets as arguments. They then figure out the extension names for correlated files themselves. These helper methods not only create the necessary tags for the proper HTML but also take care of linking to the asset files. They are not mandatory, of course, but still nice to have and very readable too. There is a bit less clutter in your markup if you make use of them.

Some View

In your global layout file, Rails gives you three of them out of the box.

app/views/layouts/application.html.erb

This results in the following output in the rendered HTML:

Let’s look more closely at how to handle image assets.

image_tag

Images placed in the public/assets/images directory can be accessed via this convenience method—no need to fiddle around with path names yourself. This is a good example of “convention over configuration” at work.

some.html.erb file

That would result in the following:

If activated, Sprockets will serve such files if found. When a file like some-image.png is fingerprinted, like some-image-9e107d9d372bb6826bd81d3542a419d6.png, it will be treated the same way.

If you need other directories within public/assets/images or within app/assets/images to organize your images, maybe something extra for icons or svg files, Rails will have no problem finding them. You simply need to add the directory’s name first:

See, no rocket science, and the other asset helpers are handled the same way.

Efficient Styles

CSS & ERB

The Asset Pipeline is set up to evaluate ERB code from the get go. All you need to do is add the .erb extension to a file and you are good to go—Sprockets will take care of the rest. When you generate controllers, it will also create views that already have the .erb extension. The same goes for scaffolds.

But this works for stylesheets as well. You can enhance them by using ERB in them. You simply create something like example.css.erb files. I’m not sure if it is a widely used technique, though. It can be very handy, but I would probably be cautious about overusing this since you can take it very far. These dynamic CSS files should probably not contain too much logic. It feels like a code smell, but the damage seems to be contained if you rely on ERB helpers mostly.

This image will be found if you have it in one of the load paths of the Asset Pipeline—usually somewhere under app/assets/images. The cool thing is, if this file has already been processed and fingerprinted, then Sprockets will use the path public/assets and find it there. The same goes for other types of assets as well, of course. Don’t forget to use <%= %>, <% %> to interpolate Ruby code in there. It won’t work without them. During precompilation, Sprockets will interpolate the code used in the CSS or Sass files and output plain .css files again—with or without a fingerprint according to your settings, of course. 

Below are a few more options that might come in handy for linking to various asset categories:

  • asset_path
  • asset_url
  • image_path
  • image_url
  • audio_path
  • audio_url
  • font_path
  • font_url
  • video_path
  • video_url

The difference between these siblings is that the _url version gives you the full path, like http://ift.tt/2frNG8X, while the _path version translates to the relative path to an asset, like /assets/application.css.

Sass Asset Helpers

When you use the sass-rails gem, you can also make use of the path and url helpers for your assets. They are a no-brainer really. It’s as simple as this:

some-stylesheet.css.erb

Notice that these helpers use a hyphen ( - ) and not an underscore ( _ ).

image-path("some-image.png") translates to "/assets/some-image.png". image-url("some-image.png") will expand into url(/assets/some-image.png)—which in turn translates to the full URL, like "http://ift.tt/2frFJRc". The same goes for asset-path, of course.

Interestingly, this gem also offers its own flavor of other asset helpers from Rails. That means you don’t have to use .erb files and do <%= %> interpolations in your stylesheets. You simply use these asset helpers from sass-rails, which feels a bit more elegant in my opinion. Also, less code-smelly.

  • asset-path
  • asset-url
  • image-path
  • image-url
  • audio-path
  • audio-url
  • font-path
  • font-url
  • video-path
  • video-url

These helper methods know exactly where to find your assets—if you put them in the conventional directory, the search path basically. The difference between path and url is that you have relative paths and one absolute path. A quick reminder: a relative path is the path to a certain file destination from some other file location. Absolute paths give you the location in reference to the root directory.

Final Thoughts

The Asset Pipeline was extracted since Rails 4 and is not a core functionality anymore. It is enabled by default, but Sprockets is now in charge of it. You are free to skip it when you initiate a project.

Using the pipeline makes asset management and compilation a breeze. You don’t need to set up anything and can focus just on working with these assets. The cherry on top is that you get a lot of handy convenience methods as well.

Your files for your CSS, JS, CoffeeScript, Sass, Haml, Slim, and so on are neatly organized in one central place, under app/assets. Sprockets is responsible for serving files from this directory. Files in there usually need some preprocessing, like turning Sass files into their equivalent CSS files.

By now you know most of the Asset Pipeline features that beginners usually have a tricky time wrapping their heads around. More important than knowing its functionality alone, you are now familiar with the why as well. You should have a good introductory grasp of compilation, fingerprinting, caching, minification, and compression. I hope you will appreciate how much this pipeline does for you in order to make your developer life a little bit more hassle-free.


by Ed Wassermann via Envato Tuts+ Code