Today I would like to share with you my first article which I wrote after finish work on interesting project for company which is produces acoustic furnitures. I hope that this article will be quite interesting for most of you because most of us are programmers.
"Have you ever thought that as a web developer you would have to deal with sound analysis? Neither have I but I faced it during a project for a company which is the leading Polish acoustic furniture producer. The case was a web app with a solution to record sound, analyse it and calculate reverberation time. It sounds like a nightmare for a programmer without specific knowledge about sound and acoustics, doesn't it? Fortunately, we have Python and a lot of Python libraries which come in handy!
So let me walk you through the whole process. Would you like to learn how to analyse sound?
Read a wav file to measure reverberation time
First of all, you have to read a wav file and find out some details from it. In Python it is a simple task because you only need a library called the SciPy, which is quite extensive. But let's focus only on the package scipy.io.wavfile. You can download a sample sound file with strong signal (which is used to measure reverberation time) to compare your results with mine: download (watch out for level of your volume).
The method above returns two variables: sample rate of wav file and data (an array containing numbers). The numbers can have different ranges and types depending on the different types of wav formats.
In our case we have a int16 NumPy dtype that is simple to check. Let's see what we can
read from our file.
Visualisation of a sound: an amplitude of digital signal
What can you do with such data? You can use another interesting library which is widely used for visualising any kind of data and preparing different kinds of charts: matplotlib. What we want to achieve is a chart which shows us a waveform of a digital signal. You can compare your result with a chart I generated using an audio editor called Audacity.
You have your sound data in a range from -2^15 to 2^15-1 so we can simply scale it to values in a range from -1 to 1 by dividing all the values by 2^15. Then you have to prepare a time array whose length will be the same as the array with our sound data. For that case, you should use a NumPy library, which helps a lot with calculations on arrays. The next step is to use a plot function from matplotlib and, voilá, we have a finished chart.
Visualisation of a sound: a spectrogram
To calculate a reverberation time you need a level of dB in a particular time. You can try to write a code step by step, but I used a specgram method from matplotlib which returns me everything I need. Let's focus on how to get a spectrogram and in the next part, we will go to reverberation time. As in the previous example, you can compare your result with a spectrogram from Audacity.
I won't analyse a parameter for specgram function, but if you want to read more about it I encourage you to check the documentation of matplotlib.pyplot.specgram. Let’s see what this function returns.
Decibel values for a specific frequency
Reverberation time of room
We should be conscious that reverberation time is an important issue because it's really inconvenient to stay in a room where it is too high. Now you will be able to check it yourself following these steps:
1. Find a maximum value of dB's in our array
2. Slice data and time arrays to the point where you found a maximum value
3. Find a value which will be equal to max-5dB
4. Slice the data and time arrays to the point where you found a max-5dB
5. Find a value which will be equal to max-25dB
6. Calculate the time which takes amplitude to drop from max less 5dB to max less 25dB (RT20)
7. Multiply your time by 3 which gives you a RT60
Does it sound a bit tricky and hard to understand? Let's have a look on a code below.
green oval: max
yellow oval: max - 5dB
red oval: max - 25dB
Now you have an algorithm which works on a raw signal. To improve our results we should calculate a reverberation time on data, which was normalized in some way. The best choice for that problem is a linear regression.
Linear regression
An improved algorithm to calculate a reverberation time of room
The following step is changing your algorithm to calculate the same way as before but on a linear regression. Let's look at a code below. You almost finished!
Ok, that’s it! You just wrote a script which allows you to check reverberation time in your room. The solution in this article was a bit simplified compared to the solution in the project for our client, but it is enough for checking reverberation time in your own room. Python libraries give us a possibility to deal with unusual use cases which would be more difficult to implement e.g. in Ruby."
1. Have you ever heard about reverberation time?
2. Do you have any experience with programming in python or ruby language? Maybe can you share with us any interesting libraries or plugins?
3. Do you have an idea which programming language could also deal with case as described in the article? Have you ever had such unusual cases in you job?
Comments
Yes, reverb effect is used quite often on sound samples and voice records while dealing with music making
2. Do you have any experience with programming in python or ruby language? Maybe can you share with us any interesting libraries or plugins?
I write automated tests in python for medical equipment software in my job. You should definitely check out:
tikinter: (native python library for gui programming, its cross-platform, quite old however, but powerful (i wrote my own code editor within it: https://mikazz.github.io/Edit/
however sill under development.
selenium: site automation, web scrapping, perfect for writing bots.
3. Do you have an idea which programming language could also deal with case as described in the article? Have you ever had such unusual cases in you job?
I think there is better way, especially for non-programmers who struggle with putting everything written here together. I recommend audacity plugin or fl studio where I think same results can be achieved.
2. Nowadays python becoming extremely popular language especially in data analysis.
Before I started learning this language I'd been a bit puzzled. Why python ? In most if benchmarks it is much slower than for example Java or C++. All my confusion disappeared after few months, after I reimplemented small neural network form java to python.
I was astonished. In java the whole code was like around 800 lines while in python it took about 70 lines. I guess it is obvious. Most of job was done with the help of early mentioned numpy library just because neural nets are mostly is a set of particular matrices operations. For now, I'm working in AI project where mostly I use python with various libraries for neural networks e.g tensorflow, keras, pytorch, tflear. Also I had a small task in computer vision field where I learned really cool library which is called OpenCV. It's extremely powerful tool for image processing.
3. In my opinion, almost all low level languages are able to deal with such type of tasks. It's just a matter of time and probably mental health )
The case was that this script was implemented in web application and this sound analysis have been doing in the background. I know audacity app and it was used to comparison but it is only good for manual analysis.
Yes, I have programming experience in Python. In fact, I am working as a Python web developer. But I also had the opportunity to work with some popular Python data analysis tools and libraries like numpy, pandas or seaborn. If you want to take a shot at data science then you should probably checkout Jupyter Notebooks, because this tool will make your life much easier while working with numpy or pandas. It's sad that Python didn't receive much recognition in web-development because this language is really powerful. And maybe it's slower than C++ or Java but if you use PyPy interpreter with JIT instead of standard CPython than you will notice major performance improvements in your programs.
I think that R would be useful for this kind of work too because it also has an extensive amount of different libraries created specifically for data analysis.
No, not yet.
Also I think you should post this article on Medium because it's really worth sharing.
I mainly program in Java. From time to time, I will write a python script for web scrapping, for example.
I think Python is great for such scientific things. Similar or even more advanced things can be done in MatLab.
Yes, I have the Python programming experience. One of the Python's charm is the writing of artificial intelligence applications that today have a lot of users in human life and, in my opinion, the future of the programming industry will move to that side.
Yes, I think the C programming language has the ability to analyze audio data.
I have some experience in Python, currently I am writing my engineering thesis in this language. In the beginning it was hard for me get used to it. I was learning Java and C++ before and Python has completely different philosophy. However when I was discovering Python more and more I was astonished how easy I can do things which required tons of code in other languages.
I am not sure which programming language could deal with this case with so little effort. Probably custom solution written in C/C++ would be faster but I am sure it would require enormous work.
Yes, I've heard about it in school.
2. Do you have any experience with programming in python or ruby language? Maybe can you share with us any interesting libraries or plugins?
I do have, but it is only python classes in our university, so that's not much (I can't recommend any libraries or plugins).
3. Do you have an idea which programming language could also deal with case as described in the article? Have you ever had such unusual cases in you job?
I think most of programming languages would deal with those cases, but it is all about how complex solution it would be. I think in C++ you could do it with not so big effort.
1. That's how I heard about the reverberation time in the context of creating sounds and music. Unfortunately, I have never worked on it personally.
2. I have never programmed in either Python or Ruby. I don't like programming and I try to avoid it when I can. I don't think I'll be helpful this time about interesting libraries.
3. In my opinion, all script programming languages can deal with it. The effects are already dependent on the experience of the programmer.
I have a little experience both with programming with python or ruby. As I said my experience is very little so I can’t share with you any interesting libraries or plugins.
Sorry, I have no idea which programming language could also deal with the case as described in the article and I never have had such unusual cases in my job.
I don't have much experience with ruby, but I have done some python. I haven't really done anything very interesting in python, but I can recommend Anaconda - a plugin for sublime text designed to function as an IDE for Python. It's very convenient for people who use Sublime Text and also use Python.
As for which programming languages could deal with cases described in the article, most of them propably could. However, most of them are also propably not very well suited for it, and would require putting in considerable work to achieve what was described above.
2. I have a little experience with Python, but with web development - I've been using Flask and SQLAlchemy
3. I'm not sure - I'm not really into this area of programming
Never heard about that.
2. Do you have any experience with programming in python or ruby language? Maybe can you share with us any interesting libraries or plugins?
Just basic stuff in Python, but I dropped it after 2 weeks, so don't know any intersting to share :D
3. Do you have an idea which programming language could also deal with case as described in the article? Have you ever had such unusual cases in you job?
Sorry sir, but I don't like programming, it's not part of my job or even studies. :/
This will be a short comment, but I'm not really in the programming and it's boring for me :/
No, I not interested at sound so much to know what is reverberation time.
I studied only basics of Python in university when I had classes related to operations on data. It’s interesting language but it’s unusual for me. I'm more used to object-oriented programming languages such as Java or C#. I know that usually Python used for complex math calculations because it has powerful libraries to work with.
No, I hadn’t such unusual cases in my job. I’ve heard about R language, that it also used for math calculations and data analysis, but I don’t know for sure can it handle audio input or no.
2. I haven't any experience with ruby. I'm currently learning python, becouse I need use neural networks in my current project, but I haven't any special plugins to share. I use tensorflow (slim). My main programming languages are PHP and java.
3. I think python is a good choice. If you would like to increase performance, you can try c/c++, but it will probably take longer to write.
It's very interesting article, which shows one of the main adventages of coding for me: you can obtain domain knowledge.
Yes, I did, somewhere on physics classes.
2. Do you have any experience with programming in python or ruby language? Maybe can you share with us any interesting libraries or plugins?
I have experience in both. I started my career programming in ruby for popular framework Ruby on Rails. Also, I wrote my bachelor thesis program, in python. I used it for machine learning, because python is the best language for data analysis.
3. Do you have an idea which programming language could also deal with case as described in the article? Have you ever had such unusual cases in you job?
I think c/c++/java can deal with this task as well.
2. I have some academic experience with Python, during MSc course I have used it for NLP and Big Data projects with aforementioned libraries like Keras, NumPy, Pandas or NLTK.
3. I think there are some libraries suited to this task available in popular programming languages like C++, Java or MATLAB
As a programmer, I am a data scientist and I work with AI libraries day to day. I have experience with programming in python >1 year. Libraries, which I use in everyday tasks are: tensorflow/keras, scikit learn and seaborn(for visualization). Also, I was thinking to make my graduate work dealing with sound analysis, but finally I chose a classification problem.
In my job, I have never deal with music and sound analysis. Mostly I have a classification or predicting problems.
NEMT Dispatch Software