Coder Social home page Coder Social logo

keith-43 / swiftbassdemo Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 0.0 8.38 MB

This Swift app uses the BASS audio library to render a real-time spectrum of a music file or live microphone input.

Swift 12.02% C 87.98%
audio audio-processing bass ios ios-swift macos macos-swift swift xcode

swiftbassdemo's Introduction

SwiftBassDemo

This app uses the BASS audio library to render a real-time spectrum of a music file or live microphone input.

This is a Swift Xcode project incorporating the BASS audio library. It is multiplatform (meaning it works on both macOS and iOS devices). The app has BASS code to play an included mp3 music file and render it's real-time spectrum. (The license-free music file is "The Elevator Bossa Nova" from Bensound.com ). Changing the MicOn variable to true enables live microphone input.

The BASS audio library is free for non-commercial usage. For commercial usage, see https://www.un4seen.com for licensing information.


Here's how I created this project. (I used the newest versions of macOS 13.2.1, iOS 16.3.1, Xcode 14.2 and BASS 2.4.17 on 7 March 2023.)

Step 1:
In Xcode, create a new Multiplatform App named SwiftBassDemo. Build and run it to get a window displaying "Hello, world!". This creates a SwiftBassDemo project which contains (in the Finder window used by Xcode) the file structure:

SwiftBassDemo (project folder)
	SwiftBassDemo (app folder)
		Assets.xcassets
		ContentView.swift
		Preview Content (folder)
		SwiftBassDemo.entitlements
		SwiftBassDemoApp.swift
	SwiftBassDemo.xcodeproj

Step 2:
Go to https://www.un4seen.com and download the macOS and iOS versions of the BASS audio library. This puts two folders named bass24-osx and bass24-ios into your Downloads folder. From the former, copy the two files named bass.h and libbass.dylib into your SwiftBassDemo project folder. From the latter, copy the bass.xcframework folder into your SwiftBassDemo project folder. (Remember to tell Xcode about these new files by using the File | Add Files to "SwiftBassDemo" command.)

Step 3:
In Xcode's Project Navigator pane (typically on the far left of the Xcode window), select the SwiftBassDemo project at the top. In the pane to the immediate right-hand side of the Project Navigator pane, select the SwiftBassDemo under TARGETS. In the General section, there is a category labelled "Frameworks, Libraries, and Embedded Content”. Confirm that the bass.xcframework and libbass.dylib items were added there (automatically done by Step 2 above.) If not, add them. For both items, in the Embed subcolumn, change the option from "Do Not Embed" to "Embed & Sign". For the bass.xcframework item, in the Filters subcolumn change the "Always Used" to "iOS". For the libbass.dylib item, in the Filters subcolumn, change the "Always Used" to "macOS".

Step 4:
Create a text file named bridging-header.h with the following text:
#ifndef bridging_header_h
#define bridging_header_h
#include "bass.h"
#endif /* bridging_header_h */
Place this file into your project folder. (Remember to tell Xcode about this new file by using the File | Add Files to "SwiftBassDemo" command.)

The project folder in your Finder window should now look like:

SwiftBassDemo (project folder)
	bass.h
	bass.xcframework (folder)
	bridging-header.h
	libbass.dylib
	SwiftBassDemo (app folder)
	SwiftBassDemo.xcodeproj

Step 5:
In the SwiftBassDemo PROJECT | Build Settings | Swift Compiler - General, set "Objective-C Bridging Header" to "bridging-header.h" in both the Debug and Release subheadings.

Step 6:
In Xcode's Project Navigator pane (typically on the far left of the Xcode window), select the SwiftBassDemo project at the top. In the pane to the immediate right-hand side of the Project Navigator pane, select the SwiftBassDemo under TARGETS. In the Build Phases section, there is a category labelled "Embedded Frameworks”. Within this category, confirm that both the bass.xcframework and libbass.dylib are named. If not, add them. In the Filters subcolumn, confirm that "Allow any platform" has been changed to "iOS" for the former item and to "macOS" for the latter item. Confirm that the "Code Sign On Co..." box is checked for both items.

Step 7: Following the above steps will produce an app that works as desired. However, if you want to distribute your app on Apple's App Store, you will need to remove the i386 32-bit code from the libbass.dylib file so that your app passes Apple's verification test. To do this, use the Terminal app in your Applications | Utilities folder, change it's target directory (using the "cd" command) to your bass24-osx folder (from steps 1 and 2), and enter the command "make 64bit". This creates a folder named "64bit" which contains a new libbass.dylib file. Replace the file in your SwiftBassDemo project folder with this new one. In Xcode, run your SwiftBassDemo project again, and it should now pass Apple's verification test.

Step 8:
To enable live microphone input, check the "Resource Access: Audio Input" box in the SwiftBassDemo TARGET, "Signing & Capabilites" heading, "All" subheading, "Hardened Runtime" section.
Also, you must add a "Privacy - Microphone Usage Description" (such as "This app analyzes audio from the microphone." Add this in the SwiftBassDemo TARGET, Info heading, "Custom Application Target Properties" section.

swiftbassdemo's People

Contributors

keith-43 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

swiftbassdemo's Issues

Stream failing with multiple errors

I've used your demo to test bass, but whenever I try to add my stream something weirds happens and I get error (not the only type, I also get "Left channel deserializing error")

2023-04-17 10:46:58.034224+0200 SwiftBassDemo[12842:130232] AACDecoder.cpp:200 Unmatched number of channel elements in payload
2023-04-17 10:46:58.036185+0200 SwiftBassDemo[12842:130232] AACDecoder.cpp:228 Error deserializing packet
2023-04-17 10:46:58.036515+0200 SwiftBassDemo[12842:130232] [ac] ACMP4AACBaseDecoder.cpp:1438 (0x157037840) Error decoding packet 1647: err = -1, packet length: 565
2023-04-17 10:46:58.036755+0200 SwiftBassDemo[12842:130232] [ac] ACMP4AACBaseDecoder.cpp:1442 '9E C7 42 30 50 8C 14 18 0C D0 4A A5 AA A6 40 BC 5E 5D D5 04 CB 0B A1 72 09 9D 1D D4 24 A9 CF 95 D4 4C C5 FF 3F 79 CB 63 C2 72 6A 08 4C 17 8C 47 30 98 E5 B7 39 74 0B 5B 88 9C 46 BF F4 63 A6 36 BF 0D 1F D4 E3 38 57 EA AE 8F 13 8A 29 EF 78 EA 3E 51 95 BD 2E 6B 65 05 DD 4A F5 C8 24 D1 52 5A 2E 1A 80 6A 65 30 1C 68 A7 67 93 22 2D 42 75 CB 97 47 C8 A9 BA 6D CA 7F D6 D0 9B 69 A6 34 8C 6D 05 F6 15 5C 89 E9 83 37 7D 1C F2 D0 10 9D 33 05 85 5E C2 DA A6 96 58 C2 02 70 0E 94 22 21 01 6C CE 50 5C EA 84 BF 39 4A E9 E2 E8 24 EF F2 F5 65 13 CE 87 2C BA 5E EF DB CB 3A 16 41 9E 79 D5 E5 65 29 E2 76 EC FF 92 92 22 31 A5 D3 80 41 12 25 F2 AD 92 C4 20 D8 4B 76 CA BA 0B A0 28 CF 60 4F C3 EA C9 CE 75 82 C5 ED DC 6E 5C B8 74 D3 DE 23 A7 CE 68 B4 D9 87 52 52 75 8F B3 0B 1D 11 4B 23 95 33 AA EF 2B 67 29 6C C0 E9 08 1C 51 F6 84 FD 3D 21 0D 40 3E 1C E8 3B 40 17 56 E8 23 C3 36 31 00 70 0F 19 4A 32 35 5C E5 50 2B B2 9E 36 EA 3D A1 F6 8F 8D ED 0F 7B 60 FF 80 C5 35 D8 01 FC 82 9E 8E D5 45 64 A1 D4 28 52 3B 04 42 03 2D 53 D9 5D E7 0E 69 14 21 A1 4B CB A0 81 74 B9 04 CE 6B F7 E9 D6 AA 96 0D B7 78 9B 31 F8 85 C9 C4 21 AD EE 23 11 86 51 BB E1 24 87 5A 98 69 B8 6A 7C D2 F4 88 A5 E8 A7 D8 DA 4D D1 39 F7 DE 4F 8D AB 4E E5 2E D2 94 96 BE C2 4A 67 97 C0 50 CB F3 52 03 99 55 00 07 16 38 DA EC 55 92 6E 59 29 DD 57 81 AA 63 B2 91 3C 1B 84 C8 9F 3B 2C 66 0A 25 C3 C3 D7 79 56 C8 F4 00 1D 39 D0 F4 E7 FB CE 91 66 07 29 CC 47 7F 31 08 E1 B4 37 B8 EA 75 FD 8B A1 00 14 00 07 A4 A9 7F ED E0 1F 4F F2 90 79 7D 4B AA E5 30 FF 59 B3 82 6B F9 4F A9 CB A7 78 2E FB A2'
2023-04-17 10:46:58.048406+0200 SwiftBassDemo[12842:130232] [ac] ACMP4AACBaseDecoder.cpp:1447 'EF 8D 60 7A 3F ED FE C8 DF 76 F5 DD 1F FD 71 46 5C 1F A5 07 5B 11 D9 80 D4 CB 00 F2 54 80 01 2D 9E 5F 3D E3 1F 9F 81 C8 31 12 7C CF F6 CE 59 56 CF 1F 19 55 55'

I've changed the following in the AudioManager.swift

            var channel = HSTREAM()
            channel = BASS_StreamCreateURL("https://stream10.xdevel.com/audio12s976748-1798/stream/icecast.audio", 0, 0, nil, nil);
            /*
            // Create a sample stream from our MP3 song file:
            stream = BASS_StreamCreateFile( BOOL32(truncating: false),  // mem: false = stream the file from a filename
                                            filePath,                   // file:
                                            0,                          // offset:
                                            0,                          // length: 0 = use all data up to end of file
                                            0)                          // flags:
             */
            BASS_ChannelPlay(channel, -1) // starts the output

Another problem is that this is the only stream that's giving trouble, why is this problem happening?

EDIT: forgot to mention that the stream correctly starts, but fails after a while

Two questions

First of all, thanks Keith for this demo app and the clear instructions.

I've followed them to install Bass in my own project.

I just have two questions:

  1. I want to be able to use Bass to play to whatever audio device is selected in my app by a user from a menu.
  2. Can you explain how I can create a queue of urls for playback?

For the first question, it appears that Bass doesn't identify Audio devices in the same way as MacOS. For example the built in speakers have an AudioDeviceID on my Mac Air of 64. Let's imagine for a moment this is not the default audio device. So I replace -1 in the Bass init with 64 (Converting first to the expected Int32). Nothing plays, so this obviously not how Bass recognises the device when it is not the default.

There is a BASS_GetInfos function; but I can't figure out how to call it form Swift. If I could get an array of devices as recognised by Bass, I could check the correspondence with the AudioDeviceID I want to use from MacOS.

For the second question I'm sure this must be possible. I'm just wondering if you know if anything is already built into Bass to facilitate this?

Thanks in advance for any advice you can give.

Geoff

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.