September 8, 2014

How To Embed Code Into A Blog Post.



The code you will need:





 
 



This code needs to be copied and pasted just after the <head> in the template of your blog. When looking at the overview of your blog click on 'Template' then 'Edit HTML' then find the first <head> (you can use Ctrl+F to find it very easily).

As you can see from the code above the 'Brushes' Cpp, Css, and Java are included, however, you may add any of these as well: Other Brushes

Thanks to Alex Gorbatchev for making and hosting the files to make this work.
Some credit goes to Craftyfella as he has instructions however I was not able to copy and paste his code and get it to work (I needed to delete a line or two).

September 7, 2014

About Me:

Reginald A. Aryee
Electrical and Computer Engineer
Graduate of Norwich University

Linkedin Profile 

Technical Interests:
Programming
GPU computation
Desktop and networking hardware
Tinkering with OS settings.

General Interests:
Baking cookies and desserts
Running
Podcasts
Economics
Logistics

Quote's and sayings that describe me:
Life is too short, so why can't we all just get along?

Perfection is impossible to achieve, but that doesn't mean we all can't strive for it.

"Higher standards!" - Something that I started as a joke to a friend who loved collecting old computer parts, but has now become a personal motto.


September 5, 2014

The Podside Project: The Inevitable Roadblocks

This ended up being a very long post (I was on a roll) so there is a TL;DR at the bottom. PS sorry for the word wrapping issues, I don't know what's going on.

I named this blog Learning Through Failure which can mean a couple different things depending on one's perspective. I always defined 'Failure' in this context as hitting a roadblock which usually, in my experience, happens preventing any and all progress for a project, not necessarily a design flaw which would mean the existing progress should literally all be scrapped, but instead a vital feature, function, or step in the design cannot be completed due to lack of information, experience, patient issues, etc.

Right now I am facing three key roadblocks which are: audio device selection, multiple audio device selection, and MP3 encoding.

The main feature of this project is suppose to be capturing the audio from a microphone and the PC audio output to combine them into one audio stream, however, the first problem I encountered was of course one of the things I would have thought to be the easiest, capturing the PC audio output (link). What is very interesting about Java's handling of audio is although you can very easily find and display all of the PC's audio input and outputs (VSJQueryMixer.java code), you can only capture a single generic device of a specific type. As Matthias Pfisterer puts it, “Asking for a line is a rather tricky thing...first, we have to say which kind of line we want. The possibilities are: SourceDataLine (for playback), Clip (for repeated playback) and TargetDataLine (for recording)” (Source). The next step is to grab the audio from the TargetDataLine by calling:

TargetDataLine line;
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);  // format is an AudioFormat object
if (!AudioSystem.isLineSupported(info)) {
   // Handle the error.
   }
// Obtain and open the line.
try {
    line = (TargetDataLine) AudioSystem.getLine(info);
    line.open(format);
    } catch (LineUnavailableException ex) {
            // Handle the error.
            }
Although this code came from Java's tutorial site and is indeed a simple implementation it is the same implementation I continually find while looking for how to record audio with Java.  The problem with this is there is no way to specify a specific audio input or output to listen to or record from (aka 'info' contains the information of all audio I/O and using 'info[2]' to specify a specific I/O to listen/record from did not work for me, more below). This implementation confuses the hell out of me especially when you can use the code:
for(Mixer.Info mixInfo: mixInfos) {
    Mixer mixer = AudioSystem.getMixer(mixInfo);
    support = “, supports “; 
    if (mixer.isLineSupported(sourceDLInfo))
       support += “SourceDataLine “;
    if (mixer.isLineSupported(clipInfo))
       support += “Clip “;
    if (mixer.isLineSupported(targetDLInfo))
       support += “TargetDataLine “;
    if (mixer.isLineSupported(portInfo))
       support += “Port “;
    System.out.println(“Mixer: “ + mixInfo.getName() + support + “, “ + mixInfo.getDescription());
    }
(Source)
to clearly show all of the devices which in turn means Java can see them and yet you cannot pick a specific one. This is not an issue at all for most or at least simple programs which just want the microphone audio (the default input device) or the speakers for example, but this issue leads to the enviable problem for those more complex programs which want access to multiple inputs (which is what I am trying to do) or multiple outputs, or to record a specific device like the PC's audio output (again what I am trying to do). (I did something no one else has, link)
The third roadblock of MP3 encoding is a first for me. The MP3 codec is patented and therefore requires permission (aka $$$, MP3 Licensing Site *Side note: This site is pretty sketchy looking like it's from the 1990's.) to incorporate in a complied program. As most people (who know anything about audio file formats) would say the MP3 format is not only the most used format but also very space efficient, especially when compared to the WAVE format. However, because of the rules put in place by the patent holder Oracle is not allowed to include native MP3 support into Java unless they pay some form of fee. This of course  is good for the owner but of course makes it harder for developers and also results in less  code being shared due to avoiding patent lawsuits and people leaning toward free alternatives.
So basically I have had a very hard time finding a way to encode audio to the MP3 format but also legally implement MP3 encoding into my program, but it can be done.
TL;DR
Roadblocks can not only be speed bumps but obstacles which make us realize fundamental flaws in our logic or code design. For this project I have encountered 3 so far: audio device selection, multiple audio device selection, and MP3 encoding. The first two I never dreamed would be any kind of issue and MP3 licensing is a pain because it means Java does not have native support for it thus there is barely any example code and if you wish to use MP3 encoding in a commercial program you have to be creative to avoid patent infringement.

Epilogue: Stuff I found while writing this post which might be helpful in the near future.

The Podside Project: The Design

     I started my design the same way I always have (and how most of people my experience level would) by determining the different phases that the program would need to go through which meant my code was looking something like this:

import _____;
Main{
void Main(str() args()){
     // Find the audio I/O.
     .
     .
     // Select the audio inputs.
     .
     .
     // Record input streams.
     .
     .
     // Collate audio.
     .
     .
     // Convert to MP3.
     .
     .
     // Save to hard drive.
     .
     .
     // Close broadcast stream and shutdown.
     .
     .
     }
}

     This is the way I was taught to code and there is not anything wrong with it, however, it is obvious once the code becomes long enough all of the sections start to appear to 'blend together' just because there is just so much code to look at.
During a recent trip I got to speak with a cousin of mine whom I rarely get to speak to and he, being a programmer himself, suggested an alternative method which blew my mind. He first stated programming would be much more simplified (in terms of readability for updating somebody else's code) if the code were to read like a English sentences in somewhat of a bullet-point format. To which of course I agreed. He then blew my mind when he said that it is actually possible and actually rather simple and easy to do. He then gave an example of a program/script he was writing to filter email which he described as:

        English                      Main File                        Function Definition File(s)
        Get mail.                         Get_mail();                                 Get_mail(){
        Check mail for ____.      Check_mail_for(____);               _______;
        Move to ____.                 Move_to(____);                         }
                                                                                                  Check_mail_for(string a){
                                                                                                  _______;
                                                                                                  }
                                                                                                  Move_to(string b){
                                                                                                  _______;
                                                                                                  }

     In school of course I was taught how to make functions/methods but I never was I told you could use them to (1) simplify my code nor (2) by using them to simplify and and naming them in a clear way resembling a clear and concise thought or action (not just Check_mail for example) I could increase the readability of my code exponentially.
This of course inspired me to redesign the layout and functionality of my code to look like this:

 import ____;
 Main{
     Display and select I/O();
     Capture inputs(input1, input2);
     Collate audio(input1, input2);
     Convert to MP3(Collated audio);
     Save audio(Collated audio, filename);
     Push to server(Collated audio, web address);
     }

     Each function is defined in its own separate file which helps with readability and what I did not realize at first was because all of the specific tasks are defined as functions I can fully test the code by simply commenting out any unwritten or unfinished functions! Of course if you just have everything in one large file to test specific parts you would need to comment out entire sections of code instead of just one line.

August 20, 2014

The Podside Project: Extras

     As always, when I code or think of software design I come up with interesting ideas which could be implemented as part of the project to add more features, quality of life changes, performance enhancements, and overall value of the software. 

For this project I thought of:
  • Adaptive/Smart MP3 file saving
  • Podcast XML file updater
  • iOS/Android/Windows App (custom or generic apps)
  • Push notifications
    • Live in 5
    • We Are Live!
    • Broadcast finished, check iTunes soonTM
Smart Saving:
     There is an issue which occurs every once in a while where a podcaster is recording an episode and then all of a sudden their recording program or computer crashes which usually results in the recording being completely lost or corrupted. The idea of a Smart Saving feature is the program will store/cache the audio in memory and can save the file every X minutes to the hard drive. 
     This can be implemented in one of 2 ways. (1) X minutes of audio is stored in a large buffer until X time passes and then the audio from the buffer is saved to a file with more audio added to the file every X minutes. (2) All audio is saved to the hard drive in X minute segments (AudioFile1.mp3, AudioFile2.mp3, etc) and then once the recoding is stopped the program then writes a the audio segments to the one file (Podcast Episode #.mp3).

(1) Pros: This allows for the file to be written in the background when the recording is still going on and so there is minimal interruption. If a crash occurs less than X minutes of audio will be lost.
Cons: This can still be susceptible to file corruption if the program or computer crashes during the writing/saving of the audio once the X minutes fills the buffer. However, this is still significantly better than continually writing data to a file in real-time.
2) Pros: This allows for the entire recording process to be completed before the audio is combined. If there is some form of crash during the recording only X minutes or less would be lost.
When combining the audio files together it can be done in such a way that all the different audio segments will remain intact until all the audio is combined and saved to the new file. This guarantees the process will complete successfully (ideally anyway).

Cons: This method requires the recording process to be completely finished and then it can combine the audio files together, thus resulting in additional time to the process.

Podcast XML Updater:
     All podcasts (at least the ones on iTunes) use an xml file to store the podcast's metadata (show notes, title, episode number, recording/release date, etc). I would think most podcasters would like it if the program could find and update this file instead of editing the file manually after every episode.

iOS/Android/Windows Apps:
     I can dream, can't I?

I Have Done Something No One Else Has!

     As you probably know a lot of news articles tend to use over exaggerated titles to gain attention, and the title of this post is almost no different. I say almost because this post is meant to share long sought after information instead of generating views. PS I bet someone has done this before, but I might be the first to openly post it to the web.

So what did I do?

     I have been looking to be able to use Java to record/capture the audio output by the OS for awhile now. It is a concept which sounds straight forward, should be straight forward, but is not straight forward. In my research I have found forum posts from as far back as 2011 of people looking for how to go about this with native Java code and all posts I have found are still without an explicit solution.

Setup Instructions and What I Learned:

     To get this code to work (on Windows 7) I had to open the sound options menu (Start Menu → search for 'Sound') and after opening the Sound configuration window select the 'Recording' tab then right click in the white space and select both 'Show Disabled Devices' and 'Show Disconnected Devices'. This caused 'Stereo Mix' to appear (grayed out) as a recording device and to enable it right click and selected 'Enable'.

Important Note: 
     As you can see from my code and from what my research has shown me, when recording audio with native Java there is NO WAY TO SELECT A DIFFERENT INPUT, that is, you can only grab/record/capture the OS's default device. This makes no sense to me, but in all the example code I have looked at to capture audio from a device I have seen NO WAY to select a secondary or tertiary device.

     Knowing this, I had to then set the 'Stereo Mix' in the Sound configuration window as the default recording device by right clicking it and selecting 'Set as Default'. This will allow you to record the audio output by your OS, HOWEVER YOU NEED TO KNOW this will very likely stop your microphone from working with all applications (even if the program is set to use a specific device, as opposed to the OS default device). If this is a problem (if you use Skype, Teamspeak 3, Mumble, or some other VOIP program) you can circumvent this by right clicking on the microphone in the recording tab and select 'Set as Default Communications Device'. Configuring the devices in this way has allowed me to still use VOIP programs and record the audio from the Stereo Mix.

Finally The Code: 
/*
 * This program records audio from a microphone for a period of X seconds specified by the user and saves it to a Record.wav file.
 * This code maybe freely used for any and all purposes (Reginald Aryee).
 * 
 * Parts of this code I got form: http://www.youtube.com/watch?v=GVtl19L9GxU&list=UUaKrpNOAP7pmHk4XyCY_AuQ&index=4
 * The rest of the code is from myself Reginald Aryee.
 */

import java.io.*;
import java.util.Scanner;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;

public class Recorder 
{
 
 public static void main(String[] args)
 {
  System.out.println("Program started. ");
  System.out.println("How many seconds would you like the recording to last?  ");
  
  Scanner scan = new Scanner(System.in);
  int UserInput = scan.nextInt();     // String s = scan.next();   // User input to find out how long to record for.
  scan.close();
  
  try
  {
   AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 16, 2, 4, 44100, false);  //Defining the properties of the .WAV file
            // 44,100hz sample rate, 16 bit sample size, 2 audio channels (stereo), frame size?, big-endian or little-endian)
   
   DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);  // Specifying the format to put the audio in.
   System.out.println(info);    // Extra info
   
   if(!AudioSystem.isLineSupported(info)) 
   { 
    System.err.println("Line not supported"); 
   }
   
   final TargetDataLine targetLine = (TargetDataLine)AudioSystem.getLine(info);   // Looks at the default audio recording device.
   System.out.println((TargetDataLine)AudioSystem.getLine(info));  // Extra info
   targetLine.open(); // The program can now have access to the live audio feed.
   
   System.out.println("Starting Recording... ");
   targetLine.start();  // Starts listening to the device.
   Thread thread = new Thread()
   {
    @Override public void run()
    {
     AudioInputStream audioStream =  new AudioInputStream(targetLine);  // Data Stream
     System.out.println("AudioInputStream(targetLine) = " + audioStream); // Extra info
     System.out.println("targetLine = " + targetLine);                    // Extra info

     File audioFile = new File("Record.wav");
     try // When data is coming through the data line/stream...
     {
      AudioSystem.write(audioStream, AudioFileFormat.Type.WAVE, audioFile); // Recording the audio to a file.
     }
     catch(IOException ioe) {ioe.printStackTrace();}
     System.out.println("Stopped Recording");
    }
   };
   
   thread.start();
   Thread.sleep(UserInput * 1000); // Main Thread sleeps to allow the recording to finish, otherwise it would cut off the recording.
   targetLine.stop();  // Stops the data flow from the device.
   targetLine.close(); // Release the resource
   System.out.println("Program ended.");
  }
  catch(LineUnavailableException lue) { lue.printStackTrace(); }
  catch(InterruptedException ie) {ie.printStackTrace();}
 }
}

(Because this appears to be a long unanswered question I am tagging this to hell.)

Tags: record PC audio output, capture PC audio output, record audio output, capture audio output, Java audio, Java audio capture, Java audio record, Java audio output) 

Site Index

In The Beginning:

The Podside Project:

Lesson Learned:
About Me